For games, touch events are the most important game method. Basically all games are completed based on touch events, but touch events are also the most basic event feedback for games. Cocos2d-x basically followed the cocos2d touch event processing method, the following is a brief introduction.
Generally, each game scene is composed of one or more layers. The scenario is divided into layers to facilitate the management of games when the number of game elements increases. In this way, the game is highly scalable, if you process the entire game element in a single Scene, it will be very redundant, and the scalability is much worse. Of course, sometimes some very simple interfaces can still not be used for Layer direct processing, of course, this is not recommended. If the class does not inherit from the CCLayer, it is easy to accept and process touch events. First, set this class to receive touch, add SetIsTouchedEnable (true) to the init () function of the class, and then rewrite the processing function of the touch event.
There are actually four touch event functions.
The method called when the finger first touches the screen:
-(Void) ccTouchesBegan (CCSet * touches, CCEvent * pEvent );
Method called when the finger moves on the screen:
-(Void) ccTouchesMoved (CCSet * touches, CCEvent * pEvent );
Method called when your finger is lifted from the screen:
-(Void) ccTouchesEnded (CCSet * touches, CCEvent * pEvent );
Method called when a touch event is canceled (for example, slide your finger out of the screen when you cut the fruit ):
-(Void) ccTouchesCancelled (CCSet * touches, CCEvent * pEvent );
-(Void) ccTouchesCancelled (CCSet * touches, CCEvent * pEvent );
The ccTouchesCancelled () method is generally not rewritten because it is useless unless required by the game. The parameters of these functions are the touch vertex set and the touch RESPONSE event. CCTouch * touch is used for single-point touch, and CCSet * touches is used for multi-point touch. Generally, the response event must retrieve the points from the vertex set, then obtain the coordinates on the corresponding screen and convert them to OpenGl coordinates. For multi-point touch, you can retrieve any point from the vertex set.
The two steps in the middle are very important. CCPoint location = touch-> locationInView (); location = CCDirector: sharedDirector ()-> convertToGL (location );, these two functions have completed coordinate conversion, and the concept of Coordinate System in the game is very important, especially when there are many types of games in the future, in map games, concepts such as NodeSpace and WorldSpace are involved. If you can fully understand the meaning of these concepts, if you encounter some strange problems when writing a game, it can be well solved.
If it is a Layer Touch, you need to register CCTouchDispatcher to let the Layer handle Touch events.
In general, write registration in init, unregister in dealloc, register in OnEnter, and unregister in OnExit. For details, refer to relevant documents.
When writing and cutting fruit games, it is particularly necessary to deal with touch events in depth, in order to draw better-looking knife marks, my knife marks are two layers of knife marks, the lower layer is a white layer with a pink layer on it, which limits the length of the knife marks. An 8-point connection marks, the mechanical connections at the locations connected by the knife marks are processed to make the turns more coherent. However, there are some good ways on the Internet, that is, to write them completely using trigonometric functions. Ah, good math! If you want my source code, I can share it with you. You are also welcome to share your source code. This is the benefit of open source!
Author: John_cdy