在精灵图像中移动SKPhysicsBody

时间:2015-03-05 19:09:12

标签: iphone ios8 sprite-kit skphysicsbody skaction

我有一个叫做MyScene(主游戏场景)的鸟类。这只鸟是随机移动的NPC。它有一个左右看的图像。当图像向右飞行时,图像会水平翻转。它没有向上或向下飞行的图像。鸟类有圆形和圆形。非常小的SKPhysicsBody与整个身体相比。这是因为后来我想使用SKPhysicsJointPin将其他小型SKPhysicsBodies附加到它 - 它们将是小蠕虫。我把鸟SKPhysicsBody做成了一个粗糙的喙(保持圆形)&将它放在图像上绘制喙的位置。我是这样做的:

minDiam = MIN(self.size.width, self.size.height);
minDiam = MAX(minDiam - 16, 4);
self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:minDiam/3.0 center:CGPointMake(-9, -2)];

在我的MyScene课程中,在游戏过程中,我试图改变物理位置,当鸟的左侧面向图像向右飞行时向右翻转。当鸟再次飞行时,还要将它改回CGPointMake(-9,-2)。物理坐标不是从CGPointMake(-9,-2)改为让CGPointMake(9,2)更改,但实际上可能会移动/移位,这一点非常重要类似于[SKAction moveToX:9持续时间:0.01]。我将来会有一个游戏开发原因。我无法做到这一点。请帮忙。

编辑:在MyScene中使用函数/方法调用bird类:

-(void)addBirdy
{
    _birdy = [Birdy node];
    _birdy.position = CGPointMake(self.size.width/2, self.size.height/2);
    [self addChild:_birdy];
}

编辑2:这是面向方向代码:

if (_birdy.physicsBody.velocity.dx < fX)
{
    BIRDYASFacingDirection facingDir = _birdy.facingDirection;
    facingDir = BIRDYfacingRight;
    _birdy.facingDirection = facingDir;
    //PhysicsBody movement to the right.
    self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:/*HOW TO RETAIN ORIGINAL VALUE?*/ center:/*HOW TO MOVE TO CGPointMake(9, 2)*/];
 }
 else
 {
     BIRDYASFacingDirection facingDir = _birdy.facingDirection;
     facingDir = BIRDYfacingLeft;
     _birdy.facingDirection = facingDir;
     //PhysicsBody movement back to the left.
     self.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:/*HOW TO RETAIN ORIGINAL VALUE?*/ center:/*HOW TO MOVE TO CGPointMake(-9, -2)*/];
 }

1 个答案:

答案 0 :(得分:1)

PhysicsBody没有位置属性,因此无法像节点那样移动。您可以使用块来模拟当前physicsBody的运动,并逐步或一次性创建一个新的。

SKAction *wait0 = [SKAction waitForDuration:0.5]; // set to whatever time required

SKAction *block0 = [SKAction runBlock:^{
    myNode.physicsBody = nil;
    myNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(10, 10) center:CGPointMake(0, 0)];
    // complete physicsBody setup
}];

SKAction *block1 = [SKAction runBlock:^{
    myNode.physicsBody = nil;
    myNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(10, 10) center:CGPointMake(2, 0)];
    // complete physicsBody setup
}];
[self runAction:[SKAction sequence:@[wait0, block0, wait0, block1]]];