Time for action – handling touches
We need to implement onTouchBegan
, onTouchMoved
, and onTouchEnded
.
Now in
GameLayer.cpp
, insideonTouchBegan
, add the following lines:if (!_running) return true; Point tap = touch->getLocation(); float dx = _rocket->getPositionX() - tap.x; float dy = _rocket->getPositionY() - tap.y; if (dx * dx + dy * dy <= pow(_rocket->getRadius(), 2) ) { _lineContainer->setLineType ( LINE_NONE ); _rocket->setRotationOrientation ( ROTATE_NONE ); _drawing = true; } return true;
When a touch begins, we only need to determine whether it's touching the ship. If it is, we set our
_drawing
property totrue
. This will indicate we have a valid point (one that began by touching the_rocket
sprite).We clear any lines we may be currently drawing in
_lineContainer
by callingsetLineType( LINE_NONE )
, and we make sure_rocket
will not rotate until we have a pivot point by releasing_rocket (setRotationOrientation ( ROTATE_NONE ))
, so it will continue to move...