0

I have a small and simple SpriteKit Game that is based on a little animation. Basically, every time the screen gets tapped I want a circular shape to grow out of the place being tapped. I now have the following code:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

      NSLog(@"Touched the screen");

      for (UITouch *touch in touches) {
           CGPoint location = [touch locationInNode:self];

            ///NSLog(@"Location X is %f", location.x);
            //NSLog(@"Location Y is %f", location.y);

           SKSpriteNode*  sprite = [SKSpriteNode spriteNodeWithColor:[SKColor purpleColor] size:CGSizeMake(25, 25)];

           sprite.position = location;

           SKAction *action = [SKAction scaleBy:10 duration:0.5];
           SKAction *newaction = [SKAction fadeAlphaTo:0 duration:0.5];

           [sprite runAction:action];
           [sprite runAction:newaction];

           [self addChild:sprite];
      }
}

The thing is that the shape that gets created when tapping is a square. Is there any way I could make it a circle?

Also another thing I am dealing with is the color. I set the color to purple. What would be perfect is the color of the shapes appearing being a random color out of a group but I do not know how to do that either.

  • Try an [SKShapeNode](https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKShapeNode_Ref/Reference/Reference.html) instead of a SKSpriteNode. And see [this](http://stackoverflow.com/questions/21130433/generate-a-random-uicolor) about the colors. – ahruss Jul 24 '14 at 22:00
  • @ahruss i have spent some time reading about shapenode and there is no simple way of creating a circle at all and from what i have read you cannot fill a shapenode with color because of a bug. I am not sure if any of these are correct, am just saying. – Stefan Gergely Jul 24 '14 at 22:39

1 Answers1

3

As I mentioned in the comment, this is possible with SKShapeNode.

    UIBezierPath* path = [UIBezierPath bezierPathWithArcCenter:CGPointZero
                                                        radius: 50
                                                    startAngle: 0
                                                      endAngle: M_PI * 2
                                                     clockwise: true];
    SKShapeNode* circle = [SKShapeNode node];
    circle.path = path.CGPath;
    circle.lineWidth = 0;
    circle.fillColor = [UIColor redColor];
ahruss
  • 2,070
  • 16
  • 21
  • Thank you very much. It does create a cirlce and it is great.. the only thing i noticed now is that when i tap the screen the circle expands and its alpha fades to zero but when that happens the node stays there unlike with the rectangle when the second it became fully transparent the node would disappear also. Now the game runs at 3 fps because of these not going away.... is there any fix to this? – Stefan Gergely Jul 25 '14 at 08:53
  • Maybe make your action `[SKAction sequence:@[[SKAction group: @[action1, action2]], [SKAction removeFromParent]]` – ahruss Jul 25 '14 at 13:13