Time for action – moving the pirate ship
Let's follow these steps to move the ship:
Open our game project file if it's not already open.
Add an instance variable called
_pirateShip
of the typeSPImage
, as shown in following line of code:SPImage* _pirateShip;
Update the references from
pirateShip
to_pirateShip
inBattlefield.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;
Add a method called
onBackgroundTouch
in theBattlefield.m
file, as shown in the following line of code:-(void) onBackgroundTouch: (SPTouchEvent*) event
Within this method, get the touch itself:
SPTouch* touch = [[event touchesWithTarget:self andPhase:SPTouchPhaseBegan] anyObject];
Complete the
onBackgroundTouch
method with the following piece of code:if (touch) { SPTween* tweenX = [SPTween tweenWithTarget:_pirateShip time:2.0f]; SPTween* tweenY = [SPTween tweenWithTarget...