Time for Action – handling touches

We need to implement ccTouchesBegan, ccTouchesMoved, and ccTouchesEnded:

  1. In GameLayer.cpp, inside ccTouchesBegan add these lines:
    if (!_running) return;
    
    CCTouch *touch = (CCTouch *)pTouches->anyObject();
    
    if (touch) {
    
        CCPoint tap = touch->getLocation();
    
        //track if tapping on ship
        float dx = _rocket->getPositionX() - tap.x;
        float dy = _rocket->getPositionY() - tap.y;
    
        if (dx * dx + dy * dy <= pow(_rocket->getRadius() * 1.2, 2) ) {
           //clear lines 
           _lineContainer->setLineType ( LINE_NONE );
            _rocket->setRotationOrientation ( ROTATE_NONE );
            _drawing = true;
        }
    }

    When a touch begins, we only need to determine if it's touching the ship, if it is we set our _drawing property to true. This will indicate that we have a valid point ...

Get Cocos2d-x by Example Beginner's Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.