SpriteKit applyforce不起作用

时间:2014-07-21 00:28:36

标签: ios xcode sprite-kit

我的场景中有player个物体,并试图对它施加一些力量。您可以看到我对update:文件上的MyScene.h方法施加了强制力。但是,player根本没有移动。发生了什么?

以下是播放器SKSpriteNode。

#import "Player.h"
#import "common.h"
@implementation Player
- (instancetype)init {
    self = [super initWithImageNamed:@"player.png"];
    self.name = @"player";
    self.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(22, 50)];
    self.physicsBody.mass = 10;
    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;

    return self;
}
@end

接下来,它是我使用的背景。

#import "Background.h"
#import "common.h"
@implementation Background
+ (Background *) generateBackground {
    Background *background = [[Background alloc] initWithImageNamed:@"RPBackground.png"];
    background.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(-100, 100) toPoint:CGPointMake(400, 100)];
    background.physicsBody.categoryBitMask = groundCategory;
    background.physicsBody.collisionBitMask = poopCategory | playerCategory;
    background.physicsBody.contactTestBitMask = groundCategory;
    background.anchorPoint = CGPointMake(0, 0);
    background.position = CGPointMake(0, 0);
    background.zPosition = 0;
    background.name = @"ground";
    return background;
}
@end

现在,这是主要场景。

#import "MyScene.h"

@implementation MyScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */
        Background *b = [Background generateBackground];
        [self addChild:b];
        self.physicsWorld.gravity = CGVectorMake(0, -9.8);


        Player *player = [[Player alloc]init];
        player.size = CGSizeMake(30, 60);
        player.position = CGPointMake([UIScreen mainScreen].applicationFrame.size.width/2, groundLevel+300);

        [self addChild:player];
        self.player = player;
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */


}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
    [self.player.physicsBody applyForce:CGVectorMake(30, 0)]; // <=------------
}

@end

我觉得我在某个地方犯了一些愚蠢的错误......但我似乎无法发现它。有人可以帮忙吗?感谢。

1 个答案:

答案 0 :(得分:3)

你的问题是SpriteKit会自动施加摩擦,你的力量对于你为节点设置的质量来说非常小。您需要设置较低的质量,较低的摩擦力(例如0.0),较低的重力或施加更大的力。代码没有什么特别的错误,因为默认的摩擦力和重力与质量和力的大小相结合只会导致一个静止的节点。