Not able to apply impulse to SKSpriteNode physics body

大城市里の小女人 提交于 2019-12-23 09:38:07

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/21163641/not-able-to-apply-impulse-to-skspritenode-physics-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!