使用SKAction动画时更新物理主体move

时间:2015-06-02 21:37:43

标签: sprite-kit skspritenode skaction

我有一个' bar'来回移动,我已经正确地让它从右向左移动精灵。然而,似乎物理体仍然与原来的位置相同。我可以联系酒吧'即使它已经动画了。

有谁知道如何解决这个问题?

- (void)setupBars
{
    leftBar = [BarNode new];
    leftBar.anchorPoint = CGPointMake(0, 0);
    leftBar.name = @"leftBar";
    leftBar.size = CGSizeMake(CGRectGetMidX(self.frame), 50);
    [leftBar setPosition:CGPointMake(0, CGRectGetMidY(self.frame) + 275)];
    leftBar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:leftBar.size];
    leftBar.physicsBody.dynamic = NO;
    leftBar.physicsBody.contactTestBitMask = nodeBitMask;
    [self addChild:leftBar];


    rightBar = [BarNode new];
    rightBar.anchorPoint = CGPointMake(0, 0);
    rightBar.name = @"rightBar";
    rightBar.size = CGSizeMake(CGRectGetMidX(self.frame), 50);
    [rightBar setPosition:CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) + 275)];
    rightBar.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rightBar.size];
    rightBar.physicsBody.dynamic = NO;
    rightBar.physicsBody.contactTestBitMask = nodeBitMask;
    [self addChild:rightBar];

    SKAction *moveLeftBar = [SKAction moveTo:CGPointMake(-160, leftBar.position.y) duration:1];
    SKAction *back = [SKAction moveTo:CGPointMake(0, leftBar.position.y) duration:1];
    SKAction *all = [SKAction sequence:@[moveLeftBar, back]];
    SKAction *repeat = [SKAction repeatActionForever:all];
    [leftBar runAction:repeat];

    moveLeftBar = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame) + 160, rightBar.position.y) duration:1];
    back = [SKAction moveTo:CGPointMake(CGRectGetMidX(self.frame), rightBar.position.y) duration:1];
    all = [SKAction sequence:@[moveLeftBar, back]];
    repeat = [SKAction repeatActionForever:all];
    [rightBar runAction:repeat];


}

1 个答案:

答案 0 :(得分:3)

你几乎已经回答了自己的问题。只是想让你自己到达那里。 SpriteKit物理机构的问题在于它们对锚点的变化没有反应。无论您将节点的锚点设置为什么,它们始终都基于0.5,0.5。

您可以选择修改节点以使用默认锚点(0.5,0.5)或使用(SKPhysicsBody *)bodyWithRectangleOfSize:(CGSize)s center:(CGPoint)center来设置中心坐标。