Game Development Reference
In-Depth Information
On the third line of this method, we create a new array for animation frames.
Then, we load the texture atlas named run . In the for loop, we create a new
name for the texture and load it with that name into the frames array. We need
to check for nil, as adding nil objects to an array raises an exception, and we
don't want that. The format specifier, @"run%.3d" , might have caught your
attention. It means that we want a string starting with run and ending with a
decimal number .3d means that the number has to be at least 3 digits long;
if it is smaller, replace the missing digits with zeroes. The names that this code
generates will look like run000 , run001 , and so on. If you are using a retina
device, they will automatically be adjusted to run000@2x and run001@2x .
You don't need to specify the file extension. You can read more about format
specifiers in the NSString class reference.
Build and run the project in a simulator. As you can see, our character has gained
nice animation. Try adjusting the scroll speed and animation speed to fit each other
better. There are some other adjustments we can do, which are as follows:
If the player is already running, we don't want to start the animation again
We may want a way to stop an animation
To accomplish these tasks, add the following two methods to ERGPlayer.m :
- (void) startRunningAnimation
{
if (![self actionForKey:@"running"]) {
[self runAction:[SKAction repeatActionForever:[SKAction
animateWithTextures:self.runFrames timePerFrame:0.05 resize:YES
restore:NO]] withKey:@"running"];
}
}
- (void) stopRunningAnimation
{
[self removeActionForKey:@"running"];
}
 
Search WWH ::




Custom Search