Game Development Reference
In-Depth Information
and a float factor, for the joypad velocity to have a noticeable effect. The scale factor is
arbitrary; it's just a value that feels good for this game.
To ensure smooth movement even if the update method is called at uneven intervals,
factor in the update method's delta parameter as well. The delta parameter is
passed by cocos2d and contains the time elapsed since the update method was last
called. This isn't strictly necessary, but it's good practice. If you don't do it, you run the
risk that the ship will move more slowly whenever the framerate drops below 60 fps.
Tiny things like these can be pretty annoying to players, and as a game developer your
goal is the exact opposite of annoying players.
At this point, moving the ship outside the screen area is still possible. I bet you'd like
for the ship to stay on the screen as much as I do. And you may be tempted to add this
code directly to the InputLayer where the ship's position is updated. That brings up
a question: do you want to prevent the joystick input from moving the ship outside the
screen, or do you want to prevent the ship from ever being able to move outside the
screen, regardless of who or what changes its position? The latter is the more general
solution and preferable in this case. To do so, you only need to override the setPos-
ition method in the Ship class by adding the code shown in Listing 7-15 to the Ship
Listing 7-15. Overriding theShip's setPosition Method
// Override setPosition to keep the ship within bounds
-(void) setPosition:(CGPoint)pos
CGSize screenSize = [CCDirector sharedDirector].winSize;
float halfWidth = contentSize_.width * 0.5f;
float halfHeight = contentSize_.height * 0.5f;
// Cap the position so the ship's sprite stays on the screen
if (pos.x < halfWidth)
pos.x = halfWidth;
else if (pos.x > (screenSize.width - halfWidth))
pos.x = screenSize.width - halfWidth;
Search Nedrilad ::

Custom Search