SpriteKit applyForce不起作用

时间:2014-07-08 04:33:52

标签: ios sprite-kit physics

我希望我的角色强行向右移动。但它不适用于下面的代码。我不知道我在这里错过了什么,但是球员在没有任何动作的情况下仍然站在底部。任何线索?

// MyScene.h
-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */

    [self.player.physicsBody applyForce: CGVectorMake(100, 100)];
}


-(void)createSceneContents {
    self.currentBackground = [Background generateBackground];
    [self addChild: self.currentBackground];
    self.physicsWorld.gravity = CGVectorMake(0, _gravity);
    self.physicsWorld.contactDelegate = self;

    Player *player = [[Player alloc]init];
    player.size = CGSizeMake(80, 70);
    player.position = CGPointMake([UIScreen mainScreen].applicationFrame.size.width/2, 40);
    [self addChild:player];
}

//Player.h

#import "Player.h"
#import "common.h"
@implementation Player
- (instancetype)init {
    self = [super initWithImageNamed:@"player.png"];
    self.name = @"player";
    NSLog(@"%f %f", self.size.width, self.size.height);
    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(20, 70)];
    self.physicsBody.dynamic = YES;
    self.physicsBody.allowsRotation = NO;
    self.physicsBody.affectedByGravity = YES;
    self.physicsBody.collisionBitMask = ~poopCategory & groundCategory;
    self.physicsBody.categoryBitMask = playerCategory;
    self.physicsBody.contactTestBitMask = poopCategory;
    self.zPosition = 100;
    //self.anchorPoint = CGPointMake(0.5, 0);

    return self;
}

1 个答案:

答案 0 :(得分:2)

看起来你在场景中使用了一个名为player的属性,但是你正在初始化一个名为player的精灵,但从未将它存储在场景的属性中。 NSLog self.player,你会发现它没有。