2014-10-12 54 views
1

我在尝试确定幻灯片发生的原因。我创建了2个节点,其中一个正在使用SKAction进行移动。但是当其中一个在上面时,它不会随着它们下面的节点移动。SpriteKit SKSpriteNode slide

这是一个有点难以解释,所以我创建了视频: https://www.youtube.com/watch?v=F4OuTBVd5sM&feature=youtube_gdata_player

感谢您的答复!

零件代码:

CGPoint positionPoint = [valuePoint CGPointValue]; 
    SKSpriteNode *slab = [SKSpriteNode spriteNodeWithTexture:plitaTexture]; 
    slab.name = @"plita"; 
    slab.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:plitaTexture.size]; 
    slab.position = positionPoint; 
    slab.physicsBody.dynamic = NO; 
    CGFloat duration = 3.0; 
    CGFloat firstDuration = duration/(self.size.width/slab.position.x); 
    SKAction *moveLeftFirstTime = [SKAction moveTo:CGPointMake(0.0 + plitaTexture.size.width/2, positionPoint.y) duration:firstDuration]; 
    SKAction *moveLeft = [SKAction moveTo:CGPointMake(0.0 + plitaTexture.size.width/2, positionPoint.y) duration:5]; 
    SKAction *moveRight = [SKAction moveTo:CGPointMake(self.size.width - plitaTexture.size.width/2, positionPoint.y) duration:5]; 
    SKAction *movingRightLeft = [SKAction sequence:@[moveRight, moveLeft]]; 
    SKAction *moving = [SKAction sequence:@[moveLeftFirstTime,[SKAction repeatActionForever:movingRightLeft]]]; 
    [slab runAction:moving]; 
    [self addChild:slab]; 

兔初始化代码:

-(instancetype)init { 
    if (self = [super initWithImageNamed:@"eating-rabbit1"]) { 
     self.physicsBody = [SKPhysicsBody bodyWithTexture:self.texture size:self.texture.size]; 
     self.physicsBody.allowsRotation = NO; 
     self.physicsBody.dynamic = YES; 
     self.physicsBody.friction = 1; 
    } 
    return self; 
} 

回答

2

主角是滑动的,因为您通过随时间改变位置来移动板。想象一下,你正站在一列正在移动的火车上,摩擦会让你沿着火车的方向前进。但是,如果火车从A点到B点消失并重新出现1厘米,那么您将停留在A点。这就是您的主角所发生的情况。

要解决此问题,您需要使用物理学来移动平板。这里是一个如何做到这一点的例子:

slab.physicsBody.affectedByGravity = NO; 
slab.physicsBody.dynamic = YES; 

// These will help prevent the slab from twisting due to the weight of the main character 
slab.physicsBody.angularDamping = 1.0; 
slab.physicsBody.mass = 1e6; 

// Air resistance will slow slab's movement, set the damping to 0 
slab.physicsBody.linearDamping = 0; 

定义一组SKActions将左右移动板。调整速度和持续时间变量以实现所需的运动。

SKAction *moveLeft = [SKAction customActionWithDuration:0 actionBlock:^(SKNode *node, CGFloat elapsedTime){ 
    node.physicsBody.velocity = CGVectorMake(-speed, 0); 
}]; 
SKAction *moveRight = [SKAction customActionWithDuration:0 actionBlock:^(SKNode *node, CGFloat elapsedTime){ 
    node.physicsBody.velocity = CGVectorMake(speed, 0); 
}]; 

SKAction *wait = [SKAction waitForDuration:duration]; 

SKAction *action = [SKAction sequence:@[moveRight, wait, moveLeft, wait]]; 

[slab runAction:[SKAction repeatActionForever:action]]; 
+0

你是摇滚!非常感谢你! – Stillfinder 2014-10-13 09:02:11

0

您需要设置板坯的ph​​ysicsBody是动态的。

slab.physicsBody.dynamic = YES; 
slab.physicsBody.affectedByGravity = NO; 

一个非动态的physicsBody被渲染为静态的并忽略所有施加在它上的力和冲动。在文档here中查找该房产。

+0

谢谢,但在这种情况下,我在视频中得到的东西,如:https://www.youtube.com/watch?v=ewbTiUTm8cw&feature=youtube_gdata_player – Stillfinder 2014-10-12 12:57:52