Cocos2d-x character Action class instance _c language

Source: Internet
Author: User
Tags addchild

We play the game can generally see the spirit of the movement, the World of Games is a movement of the world, and all of these actions can be divided into a number of basic movements and movements of the combination, today to learn the action class Ccaction, first look at the inheritance relationship between classes.

The Ccaction class derives three action classes, and the class that executes the action is Ccnode and its subclasses, which perform the action through the function runaction (), where the instantaneous and deferred actions are commonly used under ccfinitetimeaction. Action in essence is to change the attributes of the node, the instantaneous action is to change these properties do not need time, instantaneous is completed, and the delay action to change these properties need some time, you can set this time by parameters, the following is instantaneous action and delay action examples, interpretation of the source code. Click the image below to see the effect.

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

		Cclayer::init ());
		Create an elf Ccsprite * sprite = ccsprite::create ("Image.png");
		Sprite->setposition (CCP (240,160));

		The last parameter is the wizard's tag, so that the node This->addchild (sprite,0,0) is found in many subnodes through Getchildbytag ().
		Create Menu Ccmenuitemfont * fontMenu1 = ccmenuitemfont::create ("Start");
		Ccmenuitemfont * fontMenu2 = ccmenuitemfont::create ("Stop"); And move functions are bound ccmenuitemtoggle * Togglemenu = Ccmenuitemtoggle::createwithtarget (This,menu_selector (HelloWorld::
		Move), fontmenu1,fontmenu2,null);
		Ccmenu * menu = ccmenu::create (togglemenu,null);
		Menu->setposition (CCP (420,40));

    This->addchild (menu);
  BRet = true;

  while (0);
return bRet;
	} void Helloworld::move (ccobject* psender) {//via tag to add Wizard Ccsprite * Sprite = (Ccsprite *) this->getchildbytag (0);

	Ccmenuitemtoggle * Togglemenu = (Ccmenuitemtoggle *) Psender; The instantaneous action Ccplace, changed the elf coordinates, may use Sprite->setposition (CCP (60,160)) to replace, but writes the movement may add to the movementSequence of ccplace * Action1 = Ccplace::create (CCP (60,160));

	Instantaneous action, so that the elves do the x-axis Flip ccflipx * Action2 = Ccflipx::create (true);
	if (togglemenu->getselectedindex () = = 1) {//Runaction Wizard performs action sprite->runaction (ACTION2); else if (togglemenu->getselectedindex () = = 0) {}}

void Helloworld::move (ccobject* psender)
{
	//Added wizard via tag
	ccsprite * Sprite = (Ccsprite *) this-> Getchildbytag (0);
	Ccmenuitemtoggle * Togglemenu = (Ccmenuitemtoggle *) Psender;

	Delay action, the first parameter is the time required to perform the action, MoveTo is absolute, is moving to which coordinates, and Moveby is the relative
	//Incoming CCP (10,0) is a vector, the wizard moved along the x axis of 10 pixels
	Ccmoveto * MoveTo = Ccmoveto::create (2.0,CCP (240,180));
	Ccmoveby * Moveby = Ccmoveby::create (2.0,CCP (10,0));
	Rotate according to a certain angle of rotation, to emphasize the result, is now 0 degrees, will rotate to 90 degrees, if it is now 90 degrees, or will be in 90 degrees
	ccrotateto * Rotateto = ccrotateto::create (2.0,90);
	By emphasizing the relative angle of rotation, no matter how many degrees now will rotate 90 degrees, to and by the end of the action class are the same reason
	Ccrotateby * Rotateby = ccrotateby::create (2.0,90);

	if (togglemenu->getselectedindex () = = 1)
	{
		//Runaction Wizard performs action
		//sprite->runaction (moveTo);
		Sprite->runaction (Moveby);
		Sprite->runaction (Rotateby);
	}
	else if (togglemenu->getselectedindex () = = 0)
	{

	}
}

A deferred action by the end of by-ends can be obtained by reverse ().

void Helloworld::move (ccobject* psender)
{
	//Added wizard via tag
	ccsprite * Sprite = (Ccsprite *) this-> Getchildbytag (0);
	Ccmenuitemtoggle * Togglemenu = (Ccmenuitemtoggle *) Psender;

	The second parameter is the jump vector, the third parameter is the height of the jump, and the fourth parameter is the number of jumps
	ccjumpby * jumpby = Ccjumpby::create (2.0,CCP (100,30), 50,4);
	By the end of the delay action can be reverse () to obtain its reactionary
	ccactioninterval * jumpback = Jumpby->reverse ();

	if (togglemenu->getselectedindex () = = 1)
	{
		//execute action
		sprite->runaction (JUMPBY) through the Runaction Wizard;
	}
	Else if (togglemenu->getselectedindex () = = 0)
	{
		sprite->runaction (jumpback);
	}
}

There are two repetitive actions in the delay action function, that is, repeated the execution of a certain action, look at their inheritance relationship.

The following is the source code for the implementation.

 void Helloworld::move (ccobject* psender) {//Added wizard Ccsprite * Sprite = (ccsprite
	*) This->getchildbytag (0);

	Ccmenuitemtoggle * Togglemenu = (Ccmenuitemtoggle *) Psender;
	The first parameter passes in the repeated action, and the second parameter passes in the number of repetitions ccrepeat * repeat = ccrepeat::create (rotateby,4);

	Always repeat an action ccrepeatforever * repeatforever = ccrepeatforever::create (Rotateby);
	if (togglemenu->getselectedindex () = = 1) {//Runaction Wizard performs action sprite->runaction (RepeatForever); else if (togglemenu->getselectedindex () = = 0) {}} 


The following is the basic common instantaneous action and continuation action.
Instantaneous action:
Place: effect is similar to SetPosition = CCP (x, y).
Hide the Hide: The effect is similar to Setvisible:false.
Display Show: The effect is similar to setvisible:true.
Visible Toggle: togglevisibility.
Delay Action:
Move to Ccmoveto
Mobile Ccmoveby
Jump to Ccjumpto set the end position and the height and number of jumps.
Jump Ccjumpby Sets the end position and the height and number of jumps.
Bezier Ccbezierby supports 3 Bezier curves: p0-starting point, p1-tangent direction, p2-endpoint tangent direction, and p3-endpoint.
Enlarge to Ccscaleto set magnification, is a floating-point type.
Enlarge Ccscaleby
Rotate to Ccrotateto
Rotating Ccrotateby
Blinking Ccblink set blink times
Tonal changes to Cctintto
Tonal Transformation Cctintby
Darken to Ccfadeto
Ccfadein by No brightening
From Bright to No ccfadeout
The specific use of each function please test yourself.
Next, let's talk about synchronized action sequences and sequential sequences of actions. A sequence of synchronized actions is the execution of several actions at the same time, sequence of sequences of actions, in which several actions are passed in sequential order. The following is their inheritance diagram.

Next look at the implementation of the source code

void Helloworld::move (ccobject* psender) {//is added by tag to the wizard Ccsprite * Sprite = (Ccsprite *) this->getchildbytag (0);

	Ccmenuitemtoggle * Togglemenu = (Ccmenuitemtoggle *) Psender;
	Create the following several actions Ccmoveby * move = Ccmoveby::create (2.0,CCP (100,0));

	Ccrotateby * rotate = ccrotateby::create (2.0,720);
	Ccfinitetimeaction * Moveback = Move->reverse ();

	Ccfinitetimeaction * Rotateback = Rotate->reverse ();

	ccflipy * Flip = Ccflipy::create (true); Synchronous action sequence, several incoming actions are executed at the same time, the whole time of execution is the longest execution time of an action, the parameter type is ccfinitetimeaction//incoming action (the essence of the action is to change the attributes of the node). No Conflict ccspawn *
	Spawn = Ccspawn::create (move,rotate,null);

	Ccspawn * Spawnback = ccspawn::create (moveback,rotateback,null);
	Sequential sequence of actions, in which several incoming actions are executed in the order in which they are passed in, and the overall time of execution is the time of all actions and ccsequence * sequence = Ccsequence::create (move,rotate,flip,null);

	Ccsequence * Sequenceback = Ccsequence::create (Moveback,rotateback,flip->reverse (), NULL);
		if (togglemenu->getselectedindex () = = 1) {sprite->runaction (spawn); Sprite->runaction (Sequence);
		else if (togglemenu->getselectedindex () = = 0) {sprite->runaction (spawnback);
	Sprite->runaction (Sequenceback); }
}

Next to introduce Ccaction, ccfollow to achieve the effect of lens follow, similar to our horizontal version of the game to see the characters forever in the middle of the screen, and the background is moving, but their implementation is not rely on this class do not know. Ccspeed can achieve fast forward and slow-release effect, that is, change the speed of execution. The following is the source code.


void Helloworld::move (ccobject* psender) {//is added by tag to the wizard Ccsprite * Sprite = (Ccsprite *) this->getchildbytag (0);

	Ccmenuitemtoggle * Togglemenu = (Ccmenuitemtoggle *) Psender;
	Create the following several actions Ccmoveby * move = Ccmoveby::create (2.0,CCP (100,0));

	Ccrotateby * rotate = ccrotateby::create (2.0,720);
	Ccfinitetimeaction * Moveback = Move->reverse ();

	Ccfinitetimeaction * Rotateback = Rotate->reverse ();

	ccflipy * Flip = Ccflipy::create (true); Synchronous action sequence, several incoming actions are executed at the same time, the whole time of execution is the longest execution time of an action, the parameter type is ccfinitetimeaction//incoming action (the essence of the action is to change the attributes of the node). No Conflict ccspawn *
	Spawn = Ccspawn::create (move,rotate,null);

	Ccspawn * Spawnback = ccspawn::create (moveback,rotateback,null);
	Sequential sequence of actions, in which several incoming actions are executed in the order in which they are passed in, and the overall time of execution is the time of all actions and ccsequence * sequence = Ccsequence::create (move,rotate,flip,null);

	Ccsequence * Sequenceback = Ccsequence::create (Moveback,rotateback,flip->reverse (), NULL);
	Ccfollow implements a node to follow a node movement, the incoming parameter is to follow the node Ccfollow * follow = Ccfollow::create (sprite); To perform this action is to follow the node, generally the layer, the effectLifetimes in the horizontal version of the game Scene this->runaction (follow);
		if (togglemenu->getselectedindex () = = 1) {sprite->runaction (spawn);
	Sprite->runaction (sequence);
		else if (togglemenu->getselectedindex () = = 0) {sprite->runaction (spawnback);
	Sprite->runaction (Sequenceback); }
}

The following is the implementation of the Ccspeed, and a little modification is made on the basis of the above code.

void Helloworld::move (ccobject* psender) {//is added by tag to the wizard Ccsprite * Sprite = (Ccsprite *) this->getchildbytag (0);

	Ccmenuitemtoggle * Togglemenu = (Ccmenuitemtoggle *) Psender;
	Create the following several actions Ccmoveby * move = Ccmoveby::create (2.0,CCP (100,0));

	Ccrotateby * rotate = ccrotateby::create (2.0,720);
	Ccfinitetimeaction * Moveback = Move->reverse ();

	Ccfinitetimeaction * Rotateback = Rotate->reverse ();

	ccflipy * Flip = Ccflipy::create (true); Synchronous action sequence, several incoming actions are executed at the same time, the whole time of execution is the longest execution time of an action, the parameter type is ccfinitetimeaction//incoming action (the essence of the action is to change the attributes of the node). No Conflict ccspawn *
	Spawn = Ccspawn::create (move,rotate,null);

	Ccspawn * Spawnback = ccspawn::create (moveback,rotateback,null);
	Sequential sequence of actions, in which several incoming actions are executed in the order in which they are passed in, and the overall time of execution is the time of all actions and ccsequence * sequence = Ccsequence::create (move,rotate,flip,null);

	Ccsequence * Sequenceback = Ccsequence::create (Moveback,rotateback,flip->reverse (), NULL);
	Ccfollow implements a node to follow a node movement, the incoming parameter is to follow the node Ccfollow * follow = Ccfollow::create (sprite); To perform this action is to follow the node, generally the layer, the effectLifetimes in the horizontal version of the game Scene this->runaction (follow);
	Ccspeed is divided into one action class, the second parameter is a multiple of the speed to be changed ccspeed * speed1 = ccspeed::create (spawn,2.0);

	Ccspeed * speed2 = ccspeed::create (spawnback,2.0);
		if (togglemenu->getselectedindex () = = 1) {sprite->runaction (speed1);
	Sprite->runaction (sequence);
		else if (togglemenu->getselectedindex () = = 0) {sprite->runaction (SPEED2);
	Sprite->runaction (Sequenceback); }
}

The following is a brief introduction to the use of the Cccallfunc family class, which is also an action class, generally used in sequential sequences of action to perform the last action, the purpose is to call a function to complete some functions. The following are inheritance relationships for these classes.

Next, the source code is attached, and the annotation is a detailed explanation of the use of each class.


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

		Cclayer::init ());
		Ccsprite * Sprite = ccsprite::create ("Image.png");
		Sprite->setposition (CCP (240,160));

		This->addchild (sprite,0,0);
		Create a menu, add a Run event Ccmenuitemfont * Fontmenu = ccmenuitemfont::create ("Begin", This,menu_selector (Helloworld::run));
		Ccmenu * menu = ccmenu::create (fontmenu,null);
		Menu->setposition (CCP (400,40));

    This->addchild (menu);
  BRet = true;

  while (0);
return bRet;

	} void Helloworld::run (ccobject* psender) {Ccsprite * sprite = (Ccsprite *) this->getchildbytag (0);
	Create a delay action ccrotateby * rotate = ccrotateby::create (2.0,3*360); Cccallfunc, a function is bound for this action, the function is called when the action is performed, and a different selector is used to create the following four actions, but the name and each action Cccallfunc * func = cccallfunc::create (
	This,callfunc_selector (helloworld::show)); CCCALLFUNCN (n is the meaning of node), unlike the above, the bound function requires a parameter, and the incoming parameter is the node that executes the action CCCALLFUNCN * FuncN = Cccallfuncn::create (This,
	Callfuncn_selector (Helloworld::remove)); int num = 10;
	CCCALLFUNCND (d is the meaning of data), this time the binding function, not only need to bind the action of the node as a parameter passed, but also with a void * Type of parameters, representing can be any type cccallfuncnd *FUNCND = cccallfuncnd
	:: Create (This,callfuncnd_selector (helloworld::showdata), (void *) num);
	Ccsprite * Sprite2 = ccsprite::create ("Image2.png"); Cccallfunco (O is the meaning of object) The parameter to be passed in this time is the ccobject * Type Cccallfunco * Funco = cccallfunco::create (this,callfunco_selector

	(Helloworld::showsprite), sprite2);
	Create sequential action sequence//ccsequence * sequence = Ccsequence::create (rotate,func,funcnd,null);
	Ccsequence * sequence = Ccsequence::create (rotate,func,funcn,null);

	Ccsequence * sequence = Ccsequence::create (rotate,func,funcn,funco,null);
Sprite->runaction (sequence);
	//The following function do not forget to declare in the header file, note that each function's parameter void Helloworld::show () {Cclabelttf * ttf = cclabelttf::create ("Action End", "Arial", 32);
	Ttf->setposition (CCP (240,260));
This->addchild (TTF); } void Helloworld::remove (Ccnode * node) {//Does not get the wizard to perform the action through the Getchildbytag () function, but instead uses the parameters from the Remove to Ccsprite * Sprite = (ccspri
	TE *) node; True indicates that Sprite will not only remove, but thisAll actions and callbacks on the node will delete Sprite->removefromparentandcleanup (true);
The same effect can be achieved through the following methods, except that the object of function execution is different//this->removechild (sprite,true);
	} void Helloworld::showdata (Ccnode * node,void * data) {Ccsprite * sprite = (Ccsprite *) node;
	This->removechild (sprite,true);
Cclog ("num =%d", data);
	} void Helloworld::showsprite (Ccobject * sender) {Ccsprite * sprite = (Ccsprite *) sender;
	Sprite->setposition (CCP (240,160));
This->addchild (sprite); }

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.