Book Image

Sparrow iOS Game Framework Beginner's Guide

By : Johannes Stein
Book Image

Sparrow iOS Game Framework Beginner's Guide

By: Johannes Stein

Overview of this book

Table of Contents (20 chapters)
Sparrow iOS Game Framework Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Afterword
Index

Time for action – putting buttons on the screen


To add our first buttons, follow these steps:

  1. Open the Battlefield.h file.

  2. Add one instance variable for each button. We will use the SPButton type, as shown in the following code:

    SPButton *_buttonPause;
    SPButton *_buttonResume;
  3. Switch to the Battlefield.m file.

  4. 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"]];
  5. 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;
  6. Hide the resume button using the following line of code:

    _buttonResume.visible = NO;
  7. For later usage, create methods to...