Time for action – putting buttons on the screen
To add our first buttons, follow these steps:
Open the
Battlefield.h
file.Add one instance variable for each button. We will use the
SPButton
type, as shown in the following code:SPButton *_buttonPause; SPButton *_buttonResume;
Switch to the
Battlefield.m
file.Construct the instances for our two instance variables, as shown in the following code:
_buttonPause = [SPButton buttonWithUpState:[[Assets textureAtlas:@"ui.xml"] textureByName:@"button_pause"]]; _buttonResume = [SPButton buttonWithUpState:[[Assets textureAtlas:@"ui.xml"] textureByName:@"button_play"]];
Set the position of both the pause and resume buttons to the top-right corner of the screen using the following code:
_buttonPause.x = Sparrow.stage.width - _buttonPause.width - 4.0f; _buttonPause.y = 4.0f; _buttonResume.x = _buttonPause.x; _buttonResume.y = _buttonPause.y;
Hide the resume button using the following line of code:
_buttonResume.visible = NO;
For later usage, create methods to...