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 – moving the pirate ship


Let's follow these steps to move the ship:

  1. Open our game project file if it's not already open.

  2. Add an instance variable called _pirateShip of the type SPImage, as shown in following line of code:

    SPImage* _pirateShip;
  3. Update the references from pirateShip to _pirateShip in Battlefield.m:

    _pirateShip = [SPImage imageWithTexture:[Assets texture:@"ship_pirate.png"]];
    _pirateShip.x = (Sparrow.stage.width - _pirateShip.width) / 2;
    _pirateShip.y = (Sparrow.stage.height - _pirateShip.height) / 2;
  4. Add a method called onBackgroundTouch in the Battlefield.m file, as shown in the following line of code:

    -(void) onBackgroundTouch: (SPTouchEvent*) event
  5. Within this method, get the touch itself:

    SPTouch* touch = [[event touchesWithTarget:self andPhase:SPTouchPhaseBegan] anyObject];
  6. Complete the onBackgroundTouch method with the following piece of code:

    if (touch) {
      SPTween* tweenX = [SPTween tweenWithTarget:_pirateShip time:2.0f];
      SPTween* tweenY = [SPTween tweenWithTarget...