Cocos2d-x Study Notes (10) -- touchEvent

Source: Internet
Author: User

Cocos2d-x Study Notes (9) -- effect (special effects)
This time, we will give a brief introduction to the screen touch events.
This time we will talk about three touch-screen event response functions:
CcTouchesEnded (CCSet * pTouches, CCEvent * pEvent)
CcTouchesMoved (CCSet * pTouches, CCEvent * pEvent)
CcTouchesBegin (CCSet * pTouches, CCEvent * pEvent)
 
Step 1: Create a cocos2d-win32 application and name it touchEvent;
Step 2: add the MyLayer function in HelloWorldScene. h to test the touch event;
Set constants to indicate characters
Const int tagSprite = 1;
[Cpp]
Class MyLayer: public CCLayerColor
{
Public:
MyLayer ();
Virtual void ccTouchesEnded (CCSet * pTouches, CCEvent * pEvent );
 
};
Modify HelloWorldScene: scene () as follows:
[Cpp]
CCScene * HelloWorld: scene ()
{
CCScene * scene = NULL;
Do
{
Scene = CCScene: node ();
CC_BREAK_IF (! Scene );
 
MyLayer * layer = new MyLayer ();
CC_BREAK_IF (! Layer );
Scene-> addChild (layer );
} While (0 );
 
Return scene;
}

 

 
[Cpp]
MyLayer: MyLayer ()
{
SetIsTouchEnabled (true); // sets whether to enable touch screen response.
 
CCSprite * player = CCSprite: spriteWithFile ("player.png ");
AddChild (player, 0, tagSprite );

[Cpp]
// Here, a tag is set for the player genie for later quick retrieval.
// Set the background
CCLayer * layer = CCLayerColor: layerWithColor (ccc4 (255, 0, 0,255 ));
AddChild (layer,-1 );
 
Player-> setPosition (ccp (20,150 ));
Player-> runAction (CCJumpTo: actionWithDuration (4, ccp (300, 48), 100, 4 ));
Layer-> runAction (CCRepeatForever: actionWithAction (CCActionInterval *)
(CCSequence: actions (CCFadeIn: actionWithDuration (1), CCFadeOut: actionWithDuration (1), NULL ))));
// Set the background to fade in and out
 
}
 
// Response to multiple click events
Void MyLayer: ccTouchesEnded (CCSet * pTouches, CCEvent * pEvent)
{
CCSetIterator it = pTouches-> begin ();
CCTouch * touch = (CCTouch *) (* it );

CCPoint location = touch-> locationInView (touch-> view ());
CCPoint convertedLoation = CCDirector: sharedDirector ()-> convertToGL (location );
 
CCNode * node = getChildByTag (tagSprite );
[Cpp] view plaincopy
// Quickly obtain the genie pointer by marking
[Cpp]
Node-> stopAllActions ();
Node-> runAction (CCMoveTo: actionWithDuration (1, ccp (convertedLoation. x, convertedLoation. y )));
Float x = convertedLoation. x-node-> getPosition (). x;
Float y = convertedLoation. y-node-> getPosition (). y;
Float = (float) CC_RADIANS_TO_DEGREES (atanf (x/y ));
 
If (y <0)
{
If (x <0)
At = 180 + fabs ();
Else
At = 180-fabs ();
}
Node-> runAction (CCRotateTo: actionWithDuration (1, ));
[Cpp]
Here I just want to give you a rough idea about touch screen events. Next we will explain the usage of the three functions.
Step 4: add four classes:
[Cpp]
Class BaseLayer: public CCLayer
{
Public:
Virtual void onEnter ();
 
Void backCallback (CCObject * pSender );
Void restartCallback (CCObject * pSender );
Void nextCallback (CCObject * pSender );
};
 
Class TouchLayer1: public BaseLayer
{
Protected:
CCSprite * m_gun;
Public:
TouchLayer1 ();
Virtual void ccTouchesEnded (CCSet * pTouches, CCEvent * pEvent );
};
 
 
Class TouchLayer2: public BaseLayer
{
Protected:
CCSprite * m_gun;
Public:
TouchLayer2 ();
Virtual void ccTouchesMoved (CCSet * pTouches, CCEvent * pEvent );
};
 
 
Class TouchLayer3: public BaseLayer
{
Protected:
CCSprite * m_gun;
Public:
TouchLayer3 ();
Virtual void ccTouchesBegan (CCSet * pTouches, CCEvent * pEvent );
};

Define some global variables and functions as before
Static int index = 0;
Const int MAX_INDEX = 2;
Static std: string touches [] =
{
"CcTouchesEnded ",
"CcTouchesMoved ",
"CcTouchesBegin"
};
CCLayer * runThisTest (int index)
{
CCLayer * layer = NULL;
Switch (index)
{
Case 0:
Layer = new TouchLayer1 ();
Return layer;
Case 1:
Layer = new TouchLayer2 ();
Return layer;
Case 2:
Layer = new TouchLayer3 ();
Return layer;
}
Return NULL;
}
 

CCLayer * runThisTest (int index)
{
CCLayer * layer = NULL;
Switch (index)
{
Case 0:
Layer = new TouchLayer1 ();
Return layer;
Case 1:
Layer = new TouchLayer2 ();
Return layer;
Case 2:
Layer = new TouchLayer3 ();
Return layer;
}
Return NULL;
}
 
Finally, add a member function:
[Cpp]
Void BaseLayer: onEnter ()
{
CCLayer: onEnter ();
 
 
CCSize size = CCDirector: shareddire()-> getWinSize ();
 
 
// Add a title
 
 
CCLabelTTF * label = CCLabelTTF: labelWithString (touches [index]. c_str (), "Arial", 28 );
 
Label-> setPosition (ccp (size. width/2, size. height-30 ));
 
AddChild (label );
 
// Add basic button
CCLabelTTF * pNextLabel = CCLabelTTF: labelWithString ("Next", "Arial", 28 );
CCLabelTTF * pBackLabel = CCLabelTTF: labelWithString ("Back", "Arial", 28 );
CCLabelTTF * pRestartLabel = CCLabelTTF: labelWithString ("Restart", "Arial", 28 );
 
CCMenuItemLabel * pNextItem = CCMenuItemLabel: itemWithLabel (
PNextLabel, this, menu_selector (BaseLayer: nextCallback ));
CCMenuItemLabel * pBackItem = CCMenuItemLabel: itemWithLabel (
PBackLabel, this, menu_selector (BaseLayer: backCallback ));
CCMenuItemLabel * pRestartItem = CCMenuItemLabel: itemWithLabel (
PRestartLabel, this, menu_selector (BaseLayer: restartCallback ));
 
CCMenu * pNextMenu = CCMenu: menuWithItem (pNextItem );
CCMenu * pBackMenu = CCMenu: menuWithItem (pBackItem );
CCMenu * pRestartMenu = CCMenu: menuWithItem (pRestartItem );
 
PNextItem-> setPosition (ccp (size. width/2 + 150, 50 ));
PBackItem-> setPosition (ccp (size. width/2-150, 50 ));
PRestartItem-> setPosition (ccp (size. width/2, 50 ));
 
PNextMenu-> setPosition (CCPointZero );
PBackMenu-> setPosition (CCPointZero );
PRestartMenu-> setPosition (CCPointZero );
 
AddChild (pNextMenu, 2 );
AddChild (pBackMenu, 2 );
AddChild (pRestartMenu, 2 );
 
}
 
Void BaseLayer: backCallback (CCObject * pSender)
{
If (index = 0)
Return;
Index --;
CCScene * scene = new CCScene ();
Scene-> addChild (runThisTest (index ));
CCDirector: sharedDirector ()-> replaceScene (scene );
 
Scene-> release ();
}
 
 
Void BaseLayer: restartCallback (CCObject * pSender)
{
 
CCScene * scene = new CCScene ();
Scene-> addChild (runThisTest (index ));
CCDirector: sharedDirector ()-> replaceScene (scene );
 
Scene-> release ();
}
 
 
 
Void BaseLayer: nextCallback (CCObject * pSender)
{
If (index = MAX_INDEX)
Return;
Index ++;
CCScene * scene = new CCScene ();
Scene-> addChild (runThisTest (index ));
CCDirector: sharedDirector ()-> replaceScene (scene );
 
Scene-> release ();
}
 
 
Void TouchLayer1: ccTouchesEnded (CCSet * pTouches, CCEvent * pEvent)
{
CCSetIterator it = pTouches-> begin ();
CCTouch * touch = (CCTouch *) (* it );
 
CCPoint location = touch-> locationInView (touch-> view ());
CCPoint convertedLoation = CCDirector: sharedDirector ()-> convertToGL (location );
 
CCSize size = CCDirector: shareddire()-> getWinSize ();
M_gun = CCSprite: spriteWithFile ("gun.png ");
AddChild (m_gun, 1 );
M_gun-> setPosition (ccp (-10, size. height/2 ));
M_gun-> retain ();

CCActionInterval * action = CCMoveTo: actionWithDuration (1, convertedLoation );
M_gun-> runAction (action );
 
}
 
 
TouchLayer2: TouchLayer2 ()
{
SetIsTouchEnabled (true); // sets whether to enable touch screen response.
}
 
 
Void TouchLayer2: ccTouchesMoved (CCSet * pTouches, CCEvent * pEvent)
{
CCSetIterator it = pTouches-> begin ();
CCTouch * touch = (CCTouch *) (* it );
 
CCPoint location = touch-> locationInView (touch-> view ());
CCPoint convertedLoation = CCDirector: sharedDirector ()-> convertToGL (location );
 
CCSize size = CCDirector: shareddire()-> getWinSize ();
M_gun = CCSprite: spriteWithFile ("gun.png ");
AddChild (m_gun, 1 );
M_gun-> setPosition (ccp (-10, size. height/2 ));
M_gun-> retain ();
 
CCActionInterval * action = CCMoveTo: actionWithDuration (1, convertedLoation );
M_gun-> runAction (action );
 
}
 
 
TouchLayer3: TouchLayer3 ()
{
SetIsTouchEnabled (true); // sets whether to enable touch screen response.
}
 
 
Void TouchLayer3: ccTouchesBegan (CCSet * pTouches, CCEvent * pEvent)
{
CCSetIterator it = pTouches-> begin ();
CCTouch * touch = (CCTouch *) (* it );
 
CCPoint location = touch-> locationInView (touch-> view ());
CCPoint convertedLoation = CCDirector: sharedDirector ()-> convertToGL (location );
 
CCSize size = CCDirector: shareddire()-> getWinSize ();
M_gun = CCSprite: spriteWithFile ("gun.png ");
AddChild (m_gun, 1 );
M_gun-> setPosition (ccp (-10, size. height/2 ));
M_gun-> retain ();
 
CCActionInterval * action = CCMoveTo: actionWithDuration (1, convertedLoation );
M_gun-> runAction (action );
 
}




The ccTouchesEnded function starts to respond to events after you release the mouse.
 
 

The ccTouchesMove operation starts to respond after you click the mouse and ends when you leave the mouse.

 
CcTouchesBegin responds to the event when you click the mouse.
 
 

Author: wen294299195

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.