I'm trying to make a visual element (a type of progress bar), in a Sprite Kit game, that represents the "strength" with which an object is thrown. So when touching the screen, the meter begins to build-up. Holding too long results in the bar reseting & doing so indefinitely until the user releases their finger from the screen. Upon which the corresponding position of the meter strength will result in the distance the object will be thrown. The only elements I know how to do is working with the touchesBegan, touchesEnded. Please help - can't find anything online for objective-c & sprite kit on this matter (also checked github).
Lots of ways of doing something like this. You need to have 2 key things. 1) Something to keep track of whether or not the user is touching. 2) Something to keep track of how long the user is continuing the touch.
To keep track of whether the user is actually touching, you can use a BOOL which you would set to true in the touchesBegan method.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
myBool = true;
}
In the touchesEnded method you set your BOOL again once a touch has ended.
-(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.
}
The second issue is to keep track of the touch duration. There are lots of ways to do that. One of them is to use the update method.
-(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;
}
}
}
I used 60 as an example. Remember that SK runs at 60 FPS default which means 600 equals 10 seconds.
The above is very generic code and should serve you as a primer on what can be done. There are for example no allowances for any other touches aside from the throw feature. You will probably want to use a throw button instead the whole screen. The rest is for you to figure out.