我的场景上有player对象,并试图对其施加一些力。您可以看到我正在对update:文件上的MyScene.h方法施加作用力。但是,我的player根本没有动。发生了什么?

下面是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

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

最佳答案

您的问题是SpriteKit会自动施加摩擦,并且对于为Node设置的质量,力很小。您需要设置较低的质量,较低的摩擦力(例如0.0),较低的重力或施加更大的力。该代码没有什么特别的错误,因为默认的摩擦和重力与质量和力的大小共同导致一个固定节点。

关于ios - SpriteKit applyforce不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24856232/

10-12 14:39