Cocos2d-x Touch Event Instance _c language

Source: Internet
Author: User
Tags addchild touch

When playing mobile phone games, the screen receives our touch message is essential, according to our touch events, to achieve the corresponding functions, here we will learn how to cocos2d-x in the touch is how to achieve. Touch is divided into a single point of touch and multi-touch, first to see the single point of touch, is to receive a point of touch. The code clearly writes down the implementation process and analyzes the code carefully.

BOOL Helloworld::init () {bool BRet = false; do {cc_break_if (!

	Cclayer::init ());

    Open Touch this->settouchenabled (true);
  BRet = true;

  while (0);
return bRet; //Open touch must register touch event, tell engine you support single touch or multi-touch void Helloworld::registerwithtouchdispatcher () {//addtargeteddelegate Registration single point touch, The first parameter represents which object registers a touch event. The second represents the priority, the number is small, the higher the priority, the third parameter represents whether to swallow the message, if true this node received the message, processing, priority than its small node//will receive no message Ccdirector::
Shareddirector ()->gettouchdispatcher ()->addtargeteddelegate () (this,0,true); //cctouchbegan is a function that must be implemented, and is also the case where the only function//return value of the four protocol method that returns the bool type is true.
will then respond to the cctouchmoved and cctouchended and cctouchcancelled functions. Cctouch encapsulates some of the properties of the touch point, such as coordinate information, ccevent is not used in bool Helloworld::cctouchbegan (Cctouch * ptouch,ccevent * pevent) {//

	Obtain the point coordinates ccpoint points = ptouch->getlocation () in OpenGL coordinates;
	Ccsprite * Sprite = ccsprite::create ("Image.png");
	This->addchild (sprite);

	Sprite->setposition (point);
return true; A method that is constantly invoked when the finger moves on the screen void helloworld::cctouchmoved (Cctouch * touch,ccevent * pEvent) {//Get the point coordinates ccpoint points = touch->getlocation () in OpenGL coordinates;
	Ccsprite * Sprite = ccsprite::create ("Image.png");
	This->addchild (sprite);
Sprite->setposition (point); method void Helloworld::cctouchended (Cctouch * ptouch,ccevent * pevent) {this->removeallchildrenwithcl When the finger is lifted
Eanup (TRUE); //There is also a cctouchcancelled function that is called when the Touch event is canceled, such as when we suddenly call when we touch the screen.

The next step is to use the knowledge we have just learned to achieve the drag-and-drop sprite movement effect.

BOOL Helloworld::init () {bool BRet = false; do {cc_break_if (!

		Cclayer::init ());
		Implement drag-and-drop sprite moving effect ccsprite * Sprite = ccsprite::create ("Image2.png");
		Sprite->setposition (CCP (240,180));

		This->addchild (sprite,0,0);

    Open Touch this->settouchenabled (true);
  BRet = true;

  while (0);
return bRet; //Open touch must register touch event, tell engine you support single touch or multi-touch void Helloworld::registerwithtouchdispatcher () {//addtargeteddelegate Registration single point touch, The first parameter represents which object registers a touch event. The second represents the priority, the number is small, the higher the priority, the third parameter represents whether to swallow the message, if true this node received the message, processing, priority than its small node//will receive no message Ccdirector::
Shareddirector ()->gettouchdispatcher ()->addtargeteddelegate () (this,0,true); BOOL Helloworld::cctouchbegan (Cctouch * touch,ccevent * pevent) {Ccsprite * sprite = (Ccsprite *) this->getchildbyt
	AG (0);
	Get the coordinates ccpoint point = Touch->getlocation () of the points clicked by the finger;

	Get the area where the elves are, ccrect including x,y,width,height ccrect rect = Sprite->boundingbox ();
	Determine if the point of the finger click on the Sprite if (Rect.containspoint (point)) {//returns True will accept the other protocol message return true;

}	return false; } void Helloworld::cctouchmoved (Cctouch * touch,ccevent * pevent) {/* Here you can set the wizard's coordinates directly to the coordinates of the point where the finger is located, but this will produce a jump effect, visually unattractive, This is because the elves are using their own anchor points to occupy the coordinates we set, not the point where we click on the sprite and place our fingers in the position of the finger, and/or the point where the finger is now clicked and the point where the finger was last clicked ccpoint points = Touch->getlo
	cation ();
	Ccpoint Pointpre = Touch->getpreviouslocation ();

	The ccpsub subtracts the two dots to obtain a vector ccpoint direction = ccpsub (Point,pointpre) in a moving direction;
	Ccsprite * Sprite = (Ccsprite *) this->getchildbytag (0);
	Ccpoint spritepoint = Sprite->getposition ();
	Ccpadd adds the current position of the sprite to the vector in the direction of the move, obtains the point where the sprite will move to ccpoint spritedirection = Ccpadd (spritepoint,direction);
Sprite->setposition (spritedirection); }

Next to learn more touch, multi-touch and single Touch is the difference is that its priority is less than a single point of touch, regardless of the registration of the time the incoming number is, of course, there are some other differences, let us see the code. The following are the effects that are demonstrated on windows, and there is no way to achieve multi-touch on Windows.

BOOL Helloworld::init () {bool BRet = false; do {cc_break_if (!

		Cclayer::init ());
		Implement drag-and-drop sprite moving effect ccsprite * Sprite = ccsprite::create ("Image2.png");
		Sprite->setposition (CCP (240,180));

		This->addchild (sprite,0,0);

    Open Touch this->settouchenabled (true);
  BRet = true;

  while (0);
return bRet; } void Helloworld::registerwithtouchdispatcher () {//register multi-touch with only two parameters Ccdirector::shareddirector ()->
Gettouchdispatcher ()->addstandarddelegate (this,0); //In Multi-Touch, these four protocol functions are added with ES in the back of the touch, and each protocol function does not need to be implemented, all return values are void//ccset are cctouch set void Helloworld::cctouchesbegan (
	Ccset * set,ccevent * pevent) {//ccsetiterator is an iterator ccsetiterator iterator;
		The following methods are methods for obtaining objects from Ccset for (iterator = Set->begin (); iterator!= set->end (); iterator++) {//convert elements to Cctouch * type
		Cctouch * Touch = (Cctouch *) (*iterator);

		Ccpoint point = Touch->getlocation ();
		Ccsprite * Sprite = ccsprite::create ("Image.png");
		Sprite->setposition (point); This->addchild (SPRITE); }
}

Next, using the above multi-touch message to achieve the sprite magnification and scaling effect, you can see the photo album image magnification and reduction of the effect is also achieved, but Windows does not support more points, so here is only the principle of explanation.
View Source code printing help

BOOL Helloworld::init () {bool BRet = false; do {cc_break_if (!

		Cclayer::init ());
		Implement drag-and-drop sprite moving effect ccsprite * Sprite = ccsprite::create ("Image2.png");
		Sprite->setposition (CCP (240,180));

		This->addchild (sprite,0,0);

    Open Touch this->settouchenabled (true);
  BRet = true;

  while (0);
return bRet; } void Helloworld::registerwithtouchdispatcher () {//register multi-touch with only two parameters Ccdirector::shareddirector ()->
Gettouchdispatcher ()->addstandarddelegate (this,0);
	} void Helloworld::cctouchesbegan (Ccset * set,ccevent * pevent) {Ccsetiterator iterator = Set->begin ();
	Get the first touch point Cctouch * touch0 = (Cctouch *) (*iterator);
	iterator++;
	Program execution here will die because Windows only supports single touch and does not support multiple points, so here is the cctouch * touch1 = (Cctouch *) (*iterator) that will not get the second point;
Length = Ccpdistance (Touch0->getlocation (), touch1->getlocation ());
	} void Helloworld::cctouchesmoved (Ccset * set,ccevent * pevent) {Ccsetiterator iterator = Set->begin ();
	Cctouch * Touch0 = (Cctouch *) (*iterator); Iterator++;
	Cctouch * Touch1 = (Cctouch *) (*iterator);
	float length2 = ccpdistance (Touch0->getlocation (), touch1->getlocation ());
	float times = length2/length;
	Ccsprite * Sprite = (Ccsprite *) this->getchildbytag (0);
Sprite->setscale (times); }

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.