Objective c sprite kit 游戏中的进度条

Objective c progress bar in sprite kit game

我正在尝试在 Sprite Kit 游戏中制作一个视觉元素(一种进度条),它代表 "strength" 用于抛出对象的元素。因此,当触摸屏幕时,仪表开始累积。按住太久会导致栏重置并无限期地这样做,直到用户从屏幕上松开手指。仪表强度的相应位置将导致物体被抛出的距离。我知道如何做的唯一元素是使用 touchesBegan、touchesEnded。请帮助 - 在网上找不到关于 objective-c 和 sprite 工具包的任何信息(也检查了 github)。

有很多方法可以做这样的事情。你需要有两个关键的东西。 1)跟踪用户是否正在触摸的东西。 2) 跟踪用户持续触摸多长时间的东西。

要跟踪用户是否实际触摸,您可以使用一个 BOOL,您可以在 touchesBegan 方法中将其设置为 true。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    myBool = true;    
}

在触摸结束后,在 touchesEnded 方法中再次设置 BOOL。

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    myBool = false;

    // your throw the object code.
    // strength based on myInt.
    // do not forget to set the myInt back to 0.
}

第二个问题是跟踪触摸持续时间。有很多方法可以做到这一点。其中之一就是使用update方法。

-(void)update:(CFTimeInterval)currentTime {
    if(myBool) {
        myInt++;
        // code for modifying the running meter bar
        if(myInt > 600) {
            // max time reached. reset the meter bar
            myInt = 0;
        }
    }
}

我以60为例。请记住,SK 默认以 60 FPS 运行,这意味着 600 等于 10 秒。

以上是非常通用的代码,应该可以作为您了解可以做什么的入门。例如,除了投掷功能外,不允许任何其他触摸。您可能想要使用投掷按钮而不是整个屏幕。剩下的就交给你们了