Frage

I started working on my Tower defense project in Xcode with the new Spritekit framework and I wanted to add a visible radius (circle) around ANY tower that the player would create.

So I created the following code:

// Place Towers
-(void)mouseUp:(NSEvent *)theEvent {
    /* Called when a mouse click occurs */

    CGPoint location = [theEvent locationInNode:self];

    SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"tower"];

    sprite.position = location;
    sprite.scale = 1.5;

    CGContextRef myContext = [[NSGraphicsContext currentContext] graphicsPort];
    CGContextAddArc(myContext, location.x, location.y, 10, 0, M_PI*2, YES);
    CGContextSetRGBStrokeColor(myContext, 0, 0, 225, 0);

    [self addChild:sprite];

}

I'm not familiar with the angles and other things because I am doing this for the first time, so I'm just guessing that it should be like that. I'm pretty sure that I missed something.

Thanks for any advice (I await some great criticism).

War es hilfreich?

Lösung

Directly from the SpriteKit programming guide :

SKShapeNode *ball = [[SKShapeNode alloc] init];

CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddArc(myPath, NULL, 0,0, 15, 0, M_PI*2, YES);
ball.path = myPath;

ball.lineWidth = 1.0;
ball.fillColor = [SKColor blueColor];
ball.strokeColor = [SKColor whiteColor];
ball.glowWidth = 0.5;

Andere Tipps

It would probably be easier to use CGContextStrokeEllipseInRect(). So if you wanted a circle of radius 10, centered at (x,y) it would be:

const CGFloat color[] = { 0, 0, 1, 1 }; // Alpha should be 1, not 0!
CGContextSetStrokeColor (myContext, color); 
CGContextStrokeEllipseInRect (myContext, CGRectMake(x - 10, y - 10, 20, 20));
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top