Cocos2d-x touch screen events are divided into multi-point touch screen and single point touch screen, and the use of multi-point touch screen is not a lot, so the first major record the single point touch screen usage and basic principles.
There are two types of touch screens that are commonly used: one is that the Layer receives touch screen messages in a unified manner, and then the program distributes the messages to the genie in different locations as needed; another scenario is to customize a Sprite that can receive touch screen messages, such as a Sprite similar to the Button function. This requires you to define the operations triggered by the touch screen when defining the Sprite!
Below are two basic usage records:
1. Layer receives touch screen messages
The usage is very simple. You only need to override the four methods of the parent class (as needed, but ccTouchBegan () must be overwritten, and its return value has an impact on the transmission of touch screen events, and add it to CCTouchDispacher in the init () method. The Code is as follows:
[Cpp]
<Span style = "font-size: 12px;"> class TouchableLayer: public CCLayer
{
Public:
Virtual bool init ();
LAYER_NODE_FUNC (TouchableLayer );
Virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent); </span>
<Span style = "font-size: 12px;"> class TouchableLayer: public CCLayer
{
Public:
Virtual bool init ();
LAYER_NODE_FUNC (TouchableLayer );
Virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent); </span> [cpp] view plaincopyprint? <Span style = "font-size: 12px;">
Virtual void ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent );
Virtual void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent );
Virtual void ccTouchCancelled (CCTouch * pTouch, CCEvent * pEvent );
}; </Span>
<Span style = "font-size: 12px;">
Virtual void ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent );
Virtual void ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent );
Virtual void ccTouchCancelled (CCTouch * pTouch, CCEvent * pEvent );
}; </Span> Add the following code to the init () of the Layer to receive the touch screen message.
[Cpp]
<Span style = "font-size: 12px;"> CCTouchDispatcher: sharedDispatcher ()-> addTargetedDelegate (this, 0, true); </span>
<Span style = "font-size: 12px;"> CCTouchDispatcher: sharedDispatcher ()-> addTargetedDelegate (this, 0, true); </span>
If you want to determine the Sprite or region information of a specific touch in this way, you need to write your own code for judging and distributing messages!
2. Customize Sprite that can receive touch-screen messages
This is a little more complex, but it is better to understand. First, we must inherit CCSprite or its parent class to meet the requirements of Sprite shapes, positions, and other information, in addition, you also need to inherit the touch-screen event Delegate CCTargetedTouchDelegate. CCTargetedTouchDelegate defines the callback virtual functions for receiving touch-screen information. These virtual functions are exactly what we need to override. The Code is as follows:
[Cpp]
<Span style = "font-size: 12px;"> class TouchableSprite: public CCSprite, public CCTargetedTouchDelegate
{
Public:
TouchableSprite ();
Virtual ~ TouchableSprite ();
Static TouchableSprite * touchSpriteWithFile (const char * file );
Bool initWithFile (const char * file );
Virtual void onEnter ();
Virtual void onExit ();
CCRect rect ();
Bool containsTouchLocation (CCTouch * touch );
Virtual bool ccTouchBegan (CCTouch * touch, CCEvent * event );
Virtual void ccTouchMoved (CCTouch * touch, CCEvent * event );
Virtual void ccTouchEnded (CCTouch * touch, CCEvent * event );
};
</Span>
<Span style = "font-size: 12px;"> class TouchableSprite: public CCSprite, public CCTargetedTouchDelegate
{
Public:
TouchableSprite ();
Virtual ~ TouchableSprite ();
Static TouchableSprite * touchSpriteWithFile (const char * file );
Bool initWithFile (const char * file );
Virtual void onEnter ();
Virtual void onExit ();
CCRect rect ();
Bool containsTouchLocation (CCTouch * touch );
Virtual bool ccTouchBegan (CCTouch * touch, CCEvent * event );
Virtual void ccTouchMoved (CCTouch * touch, CCEvent * event );
Virtual void ccTouchEnded (CCTouch * touch, CCEvent * event );
};
</Span> the key is to determine whether a custom Sprite is clicked. In this case, you need to obtain the rectangle where the Sprite is located. There are two ways to determine whether a custom Sprite is clicked.
(1) obtain the position of the touch screen, and then determine whether to include it based on the rectangular area where the genie is located and the point of the touch screen. If so, the Sprite is touched. Here we write a method to get the current rectangle of the genie.
[Cpp]
<Span style = "font-size: 12px;"> CCRect TouchableSprite: rect ()
{
CCSize size = getContentSize ();
CCPoint pos = getPosition ();
Return CCRectMake (pos. x-size. width/2, pos. y-size. height/2, size. width, size. height );
} </Span>
<Span style = "font-size: 12px;"> CCRect TouchableSprite: rect ()
{
CCSize size = getContentSize ();
CCPoint pos = getPosition ();
Return CCRectMake (pos. x-size. width/2, pos. y-size. height/2, size. width, size. height );
} </Span> then, you need to convert the current touch screen position to the GL Coordinate Position each time you click, and then make a inclusion judgment with the rectangle where Sprite is located.
[Cpp]
<Span style = "font-size: 12px;"> bool TouchableSprite: containsTouchLocation (cocos2d: CCTouch * touch)
{
CCPoint touchPoint = touch-> locationInView (touch-> view ());
TouchPoint = CCDirector: sharedDirector ()-> convertToGL (touchPoint );
Return CCRect: CCRectContainsPoint (rect (), touchPoint );
} </Span>
<Span style = "font-size: 12px;"> bool TouchableSprite: containsTouchLocation (cocos2d: CCTouch * touch)
{
CCPoint touchPoint = touch-> locationInView (touch-> view ());
TouchPoint = CCDirector: sharedDirector ()-> convertToGL (touchPoint );
Return CCRect: CCRectContainsPoint (rect (), touchPoint );
} </Span>
(2) cocos2d provides us with a relatively simple method, but the principle is similar. Call the convertTouchToNodeSpaceAR () method defined in CCNode to convert the touch screen point to the relative coordinate of the node.
(Ps: Because the default anchor point is the midpoint, the relative coordinate system on each Sprite is from (-width/2,-height/2) to the coordinate system at the upper left corner ), so the above two methods need to be modified www.2cto.com
[Cpp]
<Span style = "font-size: 12px;"> CCRect TouchableSprite: rect ()
{
CCSize size = getTexture ()-> getContentSize ();
Return CCRectMake (-size. width/2,-size. height/2, size. width, size. height );
} </Span>
<Span style = "font-size: 12px;"> CCRect TouchableSprite: rect ()
{
CCSize size = getTexture ()-> getContentSize ();
Return CCRectMake (-size. width/2,-size. height/2, size. width, size. height );
} </Span> [cpp] view plaincopyprint? <Span style = "font-size: 12px;"> bool TouchableSprite: containsTouchLocation (cocos2d: CCTouch * touch)
{
Return CCRect: CCRectContainsPoint (rect (), convertTouchToNodeSpaceAR (touch ));
} </Span>
<Span style = "font-size: 12px;"> bool TouchableSprite: containsTouchLocation (cocos2d: CCTouch * touch)
{
Return CCRect: CCRectContainsPoint (rect (), convertTouchToNodeSpaceAR (touch ));
} </Span>
3. Touch Screen Transmission sequence
What's important?
[Cpp]
<Span style = "font-size: 12px;"> virtual bool ccTouchBegan (CCTouch * touch, CCEvent * event); </span>
<Span style = "font-size: 12px;"> virtual bool ccTouchBegan (CCTouch * touch, CCEvent * event); </span> method, the returned value affects the touch-screen message. Simply put, if false is returned, the ccTouchMoved (), ccTouchEnded (), and ccTouchCanceld () methods are not processed, it is processed by the object that receives the touch screen message. If true is returned, the ccTouchMoved (), ccTouchEnded (), and ccTouchCanceld () methods will be processed, and the touch screen message will be consumed.
Summary:
1. The CCLayer has only one layer:
Virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent );
A. if false is returned, the ccTouchMoved () and ccTouchEnded () messages will not be received.
B. if true is returned, the ccTouchMoved () and ccTouchEnded () messages can be received.
2. The CCLayer has multiple layers:
Virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent );
A. if false is returned, the ccTouchMoved () and ccTouchEnded () of the current layer will no longer receive the message, but other layers under this layer will receive the message.
B. if true is returned, the ccTouchMoved () and ccTouchEnded () of this layer can receive messages, but other layers under this layer cannot receive messages.
3. If you have a custom genie for receiving and touching messages:
Virtual bool ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent );
A. if false is returned, ccTouchMoved () and ccTouchEnded () of this genie will no longer receive messages, the layer in which this genie is located receives the touch message (if the layer in which the genie is located does not set to receive the touch message, it will be passed down)
B. if true is returned, ccTouchMoved () and ccTouchEnded () of this genie will continue to receive messages and consume the messages (that is, they will no longer be transmitted to the layer and other layers)
Author: oneRain88