Game Development Reference
In-Depth Information
Skinning the Button
Eeeww! No, it's not what you think. Skinning in computer graphics refers to giving an
otherwise featureless object a texture or simply a different look. In this case, you want
to actually see your button, so you need an image for that.
I created four button images that are 100 × 100 pixels in size—twice the final button
radius of 50. The button images come in four variations: Default, Pressed, Activated,
and Disabled. The default state is what the button looks like when it isn't pressed,
which should make it obvious what the Pressed state is. The Activated state comes into
play only for toggle buttons, meaning the toggle button is active , or on . The Disabled
image is used if the button currently has no function. For example, when the ship's
weapons are overheated and you can't shoot for a few seconds, you could disable the
button, and it would show the Disabled image.
For the shoot button, you only need the Default and Pressed images. Listing 7-11
shows the updated addFireButton method of the InputLayer class.
Listing 7-11. Replacing Listing 7-9 with a Skinned Button
float buttonRadius = 50;
CGSize screenSize = [CCDirector sharedDirector].winSize;
CCSprite* idle = [CCSprite spriteWithSpriteFrameName:@"fire-button-idle.png"];
CCSprite* press = [CCSprite spriteWithSpriteFrameName:@"fire-button-pressed.png"];
fireButton = [[SneakyButton alloc] initWithRect:CGRectZero];
fireButton.isHoldable = YES;
SneakyButtonSkinnedBase* skinFireButton = [[SneakyButtonSkinnedBase alloc] init];
skinFireButton.button = fireButton;
skinFireButton.defaultSprite = idle;
skinFireButton.pressSprite = press;
skinFireButton.position = CGPointMake(screenSize.width - buttonRadius, buttonRadius);
[self addChild:skinFireButton];
The code initializes the fireButton as usual except that I made it holdable, which
means you can keep it pressed down for a continuous stream of bullets. It also doesn't
Search Nedrilad ::

Custom Search