【问题标题】:Spritekit rotate a node while touchingSpritekit 在触摸时旋转节点
【发布时间】:2015-04-21 14:29:01
【问题描述】:

如果你愿意的话,我目前正在开发某种自上而下的赛车。这个想法是有两个按钮,每个按钮用于向左或向右转动汽车。这一切都有效,但它只适用于一键式。我想让玩家通过简单地按住按钮来转动汽车。我现在有

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    for (UITouch *touch in touches) {
        touched = YES;
        CGPoint location = [touch locationInNode:self];
        SKNode *node = [self nodeAtPoint:location];

        if ([node.name isEqualToString:@"buttonLeft"]) {
            while (touched == YES) {
                playerRotate = player.zRotation;
                playerRotate += playerAngle;
            }
        }
    }
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
    player.zRotation = playerRotate;
    float dx = playerSpeed * cos(player.zRotation);
    float dy = playerSpeed * sin(player.zRotation);
    player.physicsBody.velocity = CGVectorMake(dx, dy);
}

玩家汽车的速度在更新功能中更新。并且在touchesEnded touches 中设置为NO。问题是这个while循环卡住了(我的手机不再进行任何触摸),但我不知道只要按下按钮就可以继续旋转汽车。如果我删除 while 循环,该代码将起作用。任何帮助表示赞赏!

【问题讨论】:

    标签: objective-c rotation sprite-kit touchesbegan


    【解决方案1】:

    你已经有了正确的想法。而不是将您的 BOOL touch 设置为所有触摸的通用 YES。创建一个 touchLeft 和 touchRight BOOL。

    if([node.name isEqualToString:@"buttonLeft"]) 之后,将 touchLeft BOOL 设置为 YES。创建 if([node.name isEqualToString:@"buttonRight"]) 并设置 touchRight BOOL。

    在您的更新方法中,您可以检查任一 BOOL YES 值并相应地应用代码。

    请记住,您还需要为 touchesEnded 创建相同的代码逻辑,以便在不再存在触摸时将两个 BOOL 设置回 NO。

    【讨论】:

    • 感谢这真的很好用!对于其他有同样问题的人,我偶然发现了另一种连续旋转节点的方法。我创建了旋转节点的SKAction *rotation = [SKAction rotateByAngle: playerAngle duration:0.2];,并在 touches 开始时永远重复该动作,然后在 touchesEnded 中删除了ActionForKey。这也让你可以选择让它在一段时间内更平滑。我不知道这是否会在以后产生错误,但现在它的工作原理!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多