I am new to xcode's sprite kit and I'm an trying to apply an impulse to a SKSpriteNode's
physics body.
Here is how I create the scene:
self.backgroundColor = [SKColor colorWithRed:0 green:0 blue:0 alpha:1.0];
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody.friction = 0.0f;
self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
ballCategory = 1;
wallCategory = 2;
self.physicsBody.categoryBitMask = wallCategory;
Here is how I create the player AND give it its impulse:
player = [SKSpriteNode spriteNodeWithImageNamed:filePath];
player.size = CGSizeMake(100, 100);
player.position = CGPointMake(150, 250);
player.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:player.frame];
player.physicsBody.categoryBitMask = ballCategory;
player.physicsBody.collisionBitMask = wallCategory;
player.physicsBody.friction = 0.0f;
player.physicsBody.restitution = 1.0f;
player.physicsBody.linearDamping = 0.0f;
player.physicsBody.allowsRotation = NO;
[self addChild:player];
CGVector impulse = CGVectorMake(100,100);
[player.physicsBody applyImpulse:impulse];
Is there anything obvious that I am missing because I've been following the iOS Developer Library Sprite Kit Programming Guide perfectly? Thanks in advance for the help!
You are creating the player's physicsBody as an edge body - these are always static (immovable by forces/impulses). You should change that line to:
player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.frame.size];
This example makes it a dynamic volume based on a rectangle which will be affected by physics forces. You can read up on the types of physics bodies here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With