-1

I have two nodes in my scene. One is ball and one is background. Both are added to the scene as childs.

When the ball gets hit to the wall, I am trying to rotate the ball.

 if (!firstNode.physicsBody.resting) {
        //first node gets the reference of ball node
        [firstNode runAction:[SKAction rotateToAngle:degToRad(5.0f) duration:1.0]];
    }

The the debugger throws the error:

SKScene: Animating the rotation of a SKScene has no effect.

I am not trying to rotate SKScene. I am trying to rotate a SKSpriteNode, but why do I get the error?

Surprisingly, I do the same thing for my other node, and it didnt gave any error.

   SKAction *rotateAction = [SKAction rotateByAngle:degToRad(180.0f) duration:2.0];
    [_ringNode runAction:[SKAction repeatActionForever:rotateAction]];

Then why do the error occur in my first case?

I am using the didBeginContact to detect collision between nodes:

and in this method:

 -(void)didBeginContact:(SKPhysicsContact *)contact
{

  SKSpriteNode * firstNode = (SKSpriteNode*)contact.bodyA.node;
  SKSpriteNode *  secondNode = (SKSpriteNode *)contact.bodyB.node;
} 

Here the first node is returned as SKScene though I tried to convert it to SKSpriteNode. Somehow the wall is considered as SKScene. So I am unable to animate the first node as it is SKScene.

I can animate the secondNode which is a SKSpriteNOde.

Teja Nandamuri
  • 11,045
  • 6
  • 57
  • 109

2 Answers2

1

When detecting contact and collisions in SpriteKit there is no guarantee on which object is going to be bodyA and which will be bodyB. You should use the categories that I'm assuming you've assigned to them to determine what is what.

if (contact.bodyA.categoryBitMask == myBallCategoryBitMask) {
    //Do Stuff to the ball
} else {
    //first node is the wall so do something to the secondNode

}
mduttondev
  • 105
  • 2
  • 10
1

When you say "wall" you've probably done something like this:

 self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.frame.size];

self.physicsBody.categoryBitMask = WallCategory

Here, self is a scene (GameScene probably) which is a subclass of a SKScene...
From the docs about physics body's node property:

The node that this body is connected to. (read-only)

And contact.bodyA.node will give you the scene in this case, because physics body that represents the wall is connected to the scene (remember that scene is a node).

Whirlwind
  • 14,286
  • 11
  • 68
  • 157