【问题标题】:SpriteKit SKSpriteNode slideSpriteKit SKSpriteNode 幻灯片
【发布时间】:2014-10-12 12:00:37
【问题描述】:

我正在尝试确定为什么会发生幻灯片。我创建了 2 个节点,其中一个正在使用 SKAction 移动。但是当其中一个在另一个之上时,它不会随着它们下面的节点一起移动。

这有点难以解释,所以我制作了视频: 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;
}

【问题讨论】:

    标签: objective-c sprite-kit skspritenode


    【解决方案1】:

    主角正在滑动,因为您通过随着时间的推移改变其位置来移动平板。想象一下,你站在一列正在行驶的火车上,摩擦力会使你沿着火车的方向移动。但是,如果火车神奇地从 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;
    

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

    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]];
    

    【讨论】:

    • 你太棒了!非常感谢!
    【解决方案2】:

    你需要将slab的physicsBody设置为动态的。

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

    一个非动态的physicsBody 被渲染成静态的并且忽略任何施加在它上面的力和冲量。在文档here 中查找该属性。

    【讨论】:

    • 谢谢,但在这种情况下,我会在视频中看到类似的内容:youtube.com/…
    猜你喜欢
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多