Touch Handling in Cocos2D 3.x( 4)
Create a touch Lifecycle
Let's improve our app. Isn't it better if players can touch the screen and drag and drop a hero to a specified position?
To accomplish this, we must use all the touch events provided by Cocos2d 3.0:
TouchBegan: Call touchMoved when you touch the screen: Call touchEnded when you move your finger on the screen: Call touchCancelled when you stop the screen: it is called when the user still touches the screen but some other problems stop your node from processing the touch event (e.g. when the touch moves beyond the boundary of your node ).
Our new hero placement algorithm will work as follows:
When a user touches the screen, a hero will be generated. When the user moves his finger on the screen, the hero will also move. Once the user's finger leaves the screen, the hero will be placed at the last touch of his finger.