Game Development Reference
In-Depth Information
In the following line, we fetched the sprite, which has been touched:
SKSpriteNode *touchedNode = (SKSpriteNode *)[self
nodeAtPoint:positionInScene];
Now, once you have the sprite object, compare and check whether the select object is the
ant bug. If it's the ant bug, then animate the object by adding the following line of code:
SKAction *sequence = [SKAction sequence:@[[SKAction
rotateByAngle:degreeToRadian(-3.0f) duration:0.2],[SKAction
rotateByAngle:0.0 duration:0.1],[SKAction
rotateByAngle:degreeToRadian(3.0f) duration:0.2]]];
[touchedNode runAction:[SKAction
repeatActionForever:sequence]];
Using the SKAction class, you can execute various sequence of animations such as ro-
tation , moveBy , moveTo , and so on. Also all the rotate methods accept the angle in
radians. So to achieve the rotation, we must convert the degree to radians before passing
to any rotate function.
Now, this code will animate the selected sprite. Build and run the project and you will see
the ant animating on tap.
You will soon notice that on tapping the ant, it starts animating, but there is no way to stop
this. So now let's add a way to stop this animation once you click anywhere on the scene.
Navigate to the - (void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent *) event method, and update it to the following code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent
*)event
{
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
SKSpriteNode *touchedNode = (SKSpriteNode *)[self
nodeAtPoint:positionInScene];
if (touchedNode == self.ant) {
SKAction *sequence = [SKAction sequence:@[[SKAction
rotateByAngle:degreeToRadian(-3.0f)duration:0.2],
[SKAction rotateByAngle:0.0 duration:0.1],
Search WWH ::




Custom Search