문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top