Game Development Reference
In-Depth Information
if (pos.y < halfHeight)
pos.y = halfHeight;
else if (pos.y > (screenSize.height - halfHeight))
pos.y = screenSize.height - halfHeight;
// Must call super with the new position
[super setPosition:pos];
Every time the ship's position property changes, the preceding code performs the check
to see whether the ship's sprite is still inside the screen boundaries. If not, then the x or
y coordinate is set to a distance of half the contentSize away from the respective
screen border. This prevents any part of the player ship's sprite from leaving the screen.
Because position is a property, the setPosition method gets called by this
ship.position = CGPointMake(200, 100);
You can override other base class methods in this way to change the behavior of your
game objects. For example, if an object is allowed to be rotated only between 0 and
180 degrees, you'd override the setRotation:(float)rotation method and
add the code to limit the rotation.
Digital Controls
You can turn the SneakyJoystick class into a digital controller as well, often re-
ferred to as a D-pad . The necessary code changes are minimal:
joystick = [SneakyJoystick joystickWithRect:CGRectMake(0, 0, stickRadius, stickRadius)];
joystick.autoCenter = YES;
// Now with fewer directions
Search Nedrilad ::

Custom Search