2

I have two spriteNodes hero and the enemy, both with rectangular physicsBody applied. In the update, when the hero gets to the certain point, e.g hero.position.y <= 300 I want the enemy to rotate and face the the hero as it moves down.

the only sample code I found was the Adventure from Apple which has a faceTo class but I found it very complicated to use. I am looking for a nice and clean solution for it.

Thanks in advance.

adimona
  • 127
  • 7
  • You can use the xScale property of the SKSpriteNode. Setting xScale to -1.0 will make the sprite face the other way. – ZeMoon May 08 '14 at 08:50
  • However, this might cause a problem with the physicsBody of the sprite. – ZeMoon May 08 '14 at 08:51

2 Answers2

2

The most basic implementation should look something like this:

- (void)rotateNode:(SKNode *)nodeA toFaceNode:(SKNode *)nodeB {

    CGFloat angle = atan2f(nodeB.position.y - nodeA.position.y, nodeB.position.x - nodeA.position.x);

    if (nodeA.zRotation < 0) {
        nodeA.zRotation = nodeA.zRotation + M_PI * 2;
    }

    [nodeA runAction:[SKAction rotateToAngle:angle duration:0]];
}

It's important that you understand what is happening. Look up and read about atan2 (http://en.wikipedia.org/wiki/Atan2) and try to understand how the code above works.

henryeverett
  • 813
  • 5
  • 21
  • Wouldn't this make the sprite upside down? – ZeMoon May 08 '14 at 08:54
  • This is assuming that your game is from a top-down perspective. I'm assuming it is since you referenced Apple's Adventure demo. – henryeverett May 08 '14 at 08:55
  • Oh ok. Then this would be suitable. – ZeMoon May 08 '14 at 08:55
  • Thanks henryeverett , a 101 beginner question, how do you then get a use of that code in the update: I have the follwing following ` [self enumerateChildNodesWithName:@"enemy" usingBlock:^(SKNode *enemy, BOOL *stop) { ` if (hero.position.y <= 300 ){ // [enemy toFaceNode:hero ]; No visible @interface for 'SKNode'declares the selector 'toFaceNode:' } }]; – adimona May 08 '14 at 09:43
  • It's telling you that you haven't implemented a method called toFaceNode: in the "enemy" class. Make sure you have implemented the method correctly in the .m file and referenced it in the .h file. https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html – henryeverett May 08 '14 at 10:09
0

You can use the xScale property of SKNode to make the sprite face the other way. This can be done by setting:

sprite.xScale = -1.0;

However, this has been proved to cause problems with the physicsBody, as can be seen from this question:

Flipped x-scale breaks collision handling (SpriteKit 7.1)

The solution for this problem is given there itself by JKallio.

He suggests keeping your sprite as a child of another SKNode (to which you apply the physicsBody) and changing the xScale property of the child node where needed. This is what I am using in my own project as well.

Community
  • 1
  • 1
ZeMoon
  • 20,054
  • 5
  • 57
  • 98