cocos2d-x 3.2 Lua sample actiontest (action test)
2014 Bowen Contest, please cast your precious vote: http://vote.blog.csdn.net/Article/Details?articleid=38272837
Mobile Development Enthusiast Group: 299402133, Welcome to join the vast number of developers
Cocos2d-x official really enough to force, 3.1.1 is not familiar with, 3.2 out, this blog continues to introduce Cocos2d-x Lua example, about 3.2 of the example changes are not very big, a little about the new features of 3.2:
new features in version 3.2
* New 3D animation node Animation3d/animate3d
* Support Fbx-conv tool to generate Sprite3d supported binary format
* Support Game handle
* Support Fast tile map
* Add Utils::cpaturescreen method for screen cutting
* Physics body supports scaling and rotation
* Add Node::enumeratechildren and Utils::findchildren methods, and support C + + 11 regular expression
* Add Node::setnormalizedposition Method, node's position pixel is calculated according to the size of its service node
For more detailed information, this link can be found here: http://cn.cocos2d-x.org/tutorial/show?id=1180
This blog continues with an example of Lua, which basically covers all of Cocos2d-x's basic actions, and in this case Cocos2d-x LUA developers can learn how to use these actions.
Wizard here according to the official website of the document class to everyone to resolve, but also to deepen their impressions:
Note: The author does not put a large piece of code to paste out, paragraph interpretation more comfortable a little
Moveby: Changes the x, y pixels of a node object by modifying the Location property of the Node object. x, y coordinates are relative to the position of the object. Several moveby actions can be called simultaneously, and the final movement is a synthesis of these individual movements
MoveTo: Move the Node object to position x, Y. X, y is an absolute coordinate by modifying its Position property to change its value. Several moveto actions can be called simultaneously, and the final movement is a synthesis of several individual movements.
----------------------------------------
actionmove
--Movement
--------------------------------------
local function Actionmove ()
--Create layer
local layer = CC. Layer:create ()
--Initialization layer
initwithlayer (layer)
centersprites (3)
--moveby action
Local actionby = cc. Moveby:create (2, CC.P ())
--Moveby's rebellious operation
local actionbyback = Actionby:reverse ()
--
Tamara: Runaction (CC. Moveto:create (2, CC.P (size.width-40, size.height-40)))
grossini:runaction (CC. Sequence:create (Actionby, Actionbyback))
kathia:runaction (CC. Moveto:create (1, CC.P (+)))
Helper.subtitleLabel:setString ("Moveto/moveby")
return layer
end
Scaleto: Scaling Action
Scaleby: Scaling action, inheriting from Scaleto, providing reverse method
----------------------------------------
Actionscale
--Action zoom
--------------------------------------
local function Actionscale ()
local layer = CC. Layer:create ()
initwithlayer (layer)
centersprites (3)
--Scaleto, the first parameter is the zoom time, the second parameter is the zoom factor
Local ACTIONTo = cc. Scaleto:create (2.0, 0.5)
--Scaleby, the first parameter is the zoom time, and the second to third parameter is the scaling factor
local Actionby = cc. Scaleby:create (2.0, 1.0, 10.0)
local actionBy2 = cc. Scaleby:create (2.0, 5.0, 1.0)
-Performs action
grossini:runaction (ACTIONTo)-
executes the action sequence, scales normally, and reverses the opposite action
Tamara:runaction (CC. Sequence:create (Actionby, Actionby:reverse ()))
kathia:runaction (CC. Sequence:create (ActionBy2, Actionby2:reverse ()))
Helper.subtitleLabel:setString ("Scaleto/scaleby")
return layer
End
Rotateby class/rotateto: Rotating a node
----------------------------------------
actionrotate
--action rotation
------------------------------------- -
local function actionrotate ()
local layer = CC. Layer:create ()
initwithlayer (layer)
centersprites (3)
--Rotateto, first parameter is duration, second parameter is rotation angle
Local ACTIONTo = cc. Rotateto:create (2, $)
local ACTIONTO2 = cc. Rotateto:create (2, -45)
local actionTo0 = cc. Rotateto:create (2, 0)
-executes the action sequence, selects 45 degrees, and
tamara:runaction (cc). Sequence:create (ACTIONTo, actionTo0))
--Rotateby, lasts 2 seconds, rotates 360 degrees
local Actionby = cc. Rotateby:create (2, round)
local actionbyback = Actionby:reverse ()--opposite Operation
grossini:runaction (CC. Sequence:create (Actionby, actionbyback))
local action0retain = cc. Rotateto:create (2, 0)
kathia:runaction (CC. Sequence:create (ActionTo2, Action0retain))
Helper.subtitleLabel:setString ("Rotateto/rotateby")
return Layer
End
SKEWTO: The node object is tilted to a given angle by modifying the skewx and Skewy properties of the Node object.
Skewby: The Node object is tilted by the degrees of Skewx and Skewy.
----------------------------------------
actionskew
--Oblique action
--------------------------------------
local function Actionskew ()
local layer = CC. Layer:create ()
initwithlayer (layer)
centersprites (3)
--SKEWTO, the first parameter is the duration, the second parameter is the angle of the x-axis tilt, and the third is the y-axis tilt angle
local actionto = cc. Skewto:create (2, 37.2, -37.2)
local actiontoback = cc. Skewto:create (2, 0, 0)--an action returned by
local Actionby = cc. Skewby:create (2, 0.0, -90.0)
local actionBy2 = cc. Skewby:create (2, 45.0, 45.0)
local actionbyback = Actionby:reverse ()
--Three sprites perform the action sequence
tamara:runaction ( Cc. Sequence:create (ACTIONTo, Actiontoback))
grossini:runaction (CC. Sequence:create (Actionby, Actionbyback))
kathia:runaction (CC. Sequence:create (ActionBy2, Actionby2:reverse ()))
Helper.subtitleLabel:setString ("Skewto/skewby")
return layer
End
--actionrotationalskewvsstandardskew--Rotary tilt action and standard tilt action local function Actionrotationalskewvsstandardskew () Local Layer = CC. Layer:create () initwithlayer (layer)--removes a node from the parent node and has a cleanup parameter.
If this node is a lone node, nothing will happen.
Tamara:removefromparent (TRUE);
Grossini:removefromparent (TRUE);
Kathia:removefromparent (TRUE); --Returns the size of the OpenGL view in points local s = cc.
Director:getinstance (): Getwinsize ();
-Box with a width height of 100 local boxsize = Cc.size (100.0, 100.0); --Create a layer color block, C4B, the first parameter is R, represents red, the second argument is G, which is green, the third parameter is B, the blue, and the fourth parameter is a, representing transparency local box = CC.
Layercolor:create (cc.c4b (255,255,0,255));
--Set Anchor point Box:setanchorpoint (CC.P (0.5,0.5));
--Set Box size box:setcontentsize (boxsize);
--Set the anchor point to (0,0) when you place the node. -This is an internal method that is only used by layer and scene. Do not use the frame.
The default value is False, but is true in layer and scene.
Box:ignoreanchorpointforposition (FALSE);
--Set display position box:setposition (CC.P (S.WIDTH/2, S.height-100-box:getcontentsize (). HEIGHT/2));
--Add to the Layer layer:addchild (box); --Create a label local label = CC. Label:createwiThttf ("Standard cocos2d Skew", S_markerfeltfontpath, 16); --set anchor point, here is Center Label:setanchorpoint (CC.P (0.5, 0.5)) label:setposition (CC.P (S.WIDTH/2, s.height-100 + label:getcontent
Size (). height));
Layer:addchild (label); --X-axis tilt 360 degrees Local actionto = CC.
Skewby:create (2, 360, 0); --The action returns local actiontoback = cc.
Skewby:create (2,-360, 0); Local seq = cc.
Sequence:create (ACTIONTo, Actiontoback)--run the action sequence box:runaction (SEQ); --Create a layer of yellow color blocks, C4B, the first parameter is R, which is red, the second parameter is G, which is green, the third parameter is B, blue, and the fourth parameter is a, which represents transparency box = CC.
Layercolor:create (cc.c4b (255,255,0,255));
Box:setanchorpoint (CC.P (0.5,0.5));
Box:setcontentsize (boxsize);
Box:ignoreanchorpointforposition (FALSE);
Box:setposition (CC.P (S.WIDTH/2, S.height-250-box:getcontentsize (). HEIGHT/2));
Layer:addchild (box); label = CC.
Label:createwithttf ("Rotational Skew", S_markerfeltfontpath, 16); Label:setanchorpoint (CC.P (0.5, 0.5)) label:setposition (CC.P (S.WIDTH/2, s.height-250 + label:getcontentsize (). Height
/2)); Layer:adDchild (label); Local ACTIONTO2 = cc.
Rotateby:create (2, 360, 0); Local ActionToBack2 = cc.
Rotateby:create (2,-360, 0); Seq = cc.
Sequence:create (ActionTo2, ActionToBack2) box:runaction (seq);
Helper.subtitleLabel:setString ("Skew Comparison") return layer; End
----------------------------------------Actionskewrotate--skew + rotate + zoom--------------------------------------Local function Actionskewrotate ()--create layer local layer = CC.
Layer:create () initwithlayer (layer)--remove child nodes from parent node tamara:removefromparent (true) grossini:removefromparent (True) Kathia:removefromparent (True)--box size local boxsize = Cc.size (100.0, 100.0)--layer color, 1th, 2, 3 are red and green basket color values, 4th for transparency value Local box = cc. Layercolor:create (cc.c4b (255, 255, 0, 255))--set Anchor point Box:setanchorpoint (CC.P (0, 0))--Set position box:setposition (190, 11 0)--Set content size Box:setcontentsize (boxsize)--Mark size Local markrside = 10.0 local UL = cc. Layercolor:create (cc.c4b (255, 0, 0, 255)) Box:addchild (UL) ul:setcontentsize (Cc.size (Markrside, markrside)) UL:setPo Sition (0, Boxsize.height-markrside) ul:setanchorpoint (CC.P (0, 0)) Local UR = cc. Layercolor:create (cc.c4b (0, 0, 255, 255)) Box:addchild (UR) ur:setcontentsize (Cc.size (Markrside, markrside)) UR:setPo Sition (Boxsize.width-maRkrside, Boxsize.height-markrside) ur:setanchorpoint (CC.P (0, 0)) layer:addchild (box)--Three actions skewto, Rotateto, scale to local actionto = CC. Skewto:create (2, 0, 2) Local rotateto = cc. Rotateto:create (2, 61.0) Local actionscaleto = cc. Scaleto:create (2, -0.44, 0.47) Local actionscaletoback = cc. Scaleto:create (2, 1.0, 1.0) Local rotatetoback = cc. Rotateto:create (2, 0) Local actiontoback = cc. Skewto:create (2, 0, 0)--sequential execution of three action sequences box:runaction (CC. Sequence:create (ACTIONTo, Actiontoback)) Box:runaction (CC. Sequence:create (Rotateto, Rotatetoback)) Box:runaction (CC. Sequence:create (Actionscaleto, Actionscaletoback)) Helper.subtitleLabel:setString ("Skew + Rotate + scale") return lay ER End
Jumpto class: Moving nodes that mimic a jumping trajectory
Jumpby class: moves the node by imitating the jumping trajectory. Provide reverse method
----------------------------------------
actionjump
--the action of jumping
--------------------------------------
local function Actionjump ()
--Create layer
local layer = CC. Layer:create ()
initwithlayer (layer)
centersprites (3)-
-simulates the jumping trajectory to move the node, the first parameter is the duration, the second parameter is the position, the third parameter is the height of the jump, The number of the fourth parameter jumps
local actionto = cc. Jumpto:create (2, CC.P (300,300), 4)
local Actionby = cc. Jumpby:create (2, CC.P (300,0), 4)
local actionup = cc. Jumpby:create (2, CC.P (0,0), 4)
Local actionbyback = Actionby:reverse ()--opposite action--
perform actionto action
Tamara:runaction (ACTIONTo)
-executes the sequence action
grossini:runaction (CC. Sequence:create (Actionby, Actionbyback))
-Performs an infinite loop action
kathia:runaction (CC. Repeatforever:create (actionup))
Helper.subtitleLabel:setString ("Jumpto/jumpby")
return layer
end
Cardinalsplineby class: Base curve Path
----------------------------------------Actioncardinalspline--curve motion--------------------------------------Local function actioncardinalspline () local layer = CC. Layer:create () initwithlayer (layer) centersprites (2)--position array Local array = {CC.P (0, 0), CC.P (size.width /2-30, 0), CC.P (SIZE.WIDTH/2-size.height-80), CC.P (0, size.height-80), CC.P (0, 0),}--Create a Sets the set of points for a continuous base curve action Local action = CC. Cardinalsplineby:create (3, array, 0)--Returns the new action object that performs the opposite of this action object local reverse = Action:reverse ()--Action sequence Loca L seq = cc. Sequence:create (action, reverse) Tamara:setposition (CC.P (s)) tamara:runaction (SEQ)--The first parameter is duration: Duration, second The parameter is an array of positions, and the third parameter is tension, which indicates the tension local action2 = cc. Cardinalsplineby:create (3, array, 1) Local reverse2 = Action2:reverse ()--Create the action sequence local SEQ2 = cc. Sequence:create (Action2, Reverse2) kathia:setposition (CC.P (SIZE.WIDTH/2)) kathia:runaction (SEQ2)--[[Local function drawcardinalsPline () Kmglpushmatrix () Kmgltranslatef (0) cc. Drawprimitives.drawcardinalspline (array, 0,) Kmglpopmatrix () Kmglpushmatrix () Kmgltranslatef (SIZE.WIDTH/2, 50 , 0) cc. Drawprimitives.drawcardinalspline (array, 1, +) Kmglpopmatrix () end Array:retain () local glnode = Gl.glnodecrea Te () glnode:setcontentsize (Cc.size (Size.width, Size.Height)) Glnode:setanchorpoint (CC.P (0.5, 0.5)) glnode:registers
Criptdrawhandler (Drawcardinalspline) layer:addchild (glnode,-10) glnode:setposition (SIZE.WIDTH/2, SIZE.HEIGHT/2) ]]--Helper.titleLabel:setString ("Cardinalsplineby/cardinalsplineat") Helper.subtitleLabel:setString ("Cardinal
Spline paths.\ntesting different tensions for one array ") return layer End
Catmullromby class: This is an action that moves the target point according to the Cartesian curve.
----------------------------------------Actioncatmullrom--Cartesian curve motion--------------------------------------Local function Actioncatmullrom () local layer = CC. Layer:create () initwithlayer (layer) centersprites (2)--Set Sprite position tamara:setposition (CC.P (50, 50))--Define position array L Ocal array = {CC.P (0, 0), CC.P, CC.P (size.width-80, +), CC.P (size.width-80, size.height-80 ), CC.P (size.height-80), CC.P (x, X), CC.P (SIZE.WIDTH/2, SIZE.HEIGHT/2),}--Create a Cartesian curve motion, the first parameter The number is the duration, and the second parameter is the position array local action = CC. Catmullromby:create (3, array) Local reverse = Action:reverse ()--opposite action--creates the action sequence local seq = cc. Sequence:create (action, reverse) tamara:runaction (seq) Local array2 = {CC.P (SIZE.WIDTH/2, +), CC.P (size. Width-80, CC.P (size.width-80, size.height-80), CC.P (SIZE.WIDTH/2, size.height-80), CC.P (size.wi DTH/2, +),} local action2 = cc. Catmullromto:create (3, array2) Local REVERSE2 = Action2:reverse () Local SEQ2 = cc.
Sequence:create (Action2, Reverse2) kathia:runaction (SEQ2)--[[local function Drawcatmullrom () Kmglpushmatrix () KMGLTRANSLATEF (0) cc. Drawprimitives.drawcatmullrom (array,) Kmglpopmatrix () cc. Drawprimitives.drawcatmullrom (array2,50) End Array:retain () Array2:retain () Local glnode = Gl.glnodecreate () g Lnode:setcontentsize (Cc.size (Size.width, Size.Height)) Glnode:setanchorpoint (CC.P (0.5, 0.5)) Glnode: Registerscriptdrawhandler (Drawcatmullrom) layer:addchild (glnode,-10) glnode:setposition (SIZE.WIDTH/2, Size.Height /2)]]----Set the title Helper.titleLabel:setString ("Catmullromby/catmullromto") Helper.subtitleLabel:setString ("Catm ull Rom spline paths. Testing reverse too ") return layer End
Bezierby class: Bézier curve action. Provides a reverse method for performing the opposite action
Bezierto class: Bézier curve action.
----------------------------------------Actionbezier-Bézier curve Motion--------------------------------------Local function actionbezier () local layer = CC. Layer:create () initwithlayer (layer) centersprites (3)--Sprite 1--[[local Bézier = Ccbezierconfig () Bézier . controlpoint_1 = CC.P (0, SIZE.HEIGHT/2) bezier.controlpoint_2 = CC.P (+,-size.height/2) bezier.endposition = C
C.P (+)]]----Bezier configuration structure Local Bézier = {CC.P (0, SIZE.HEIGHT/2), CC.P (-SIZE.HEIGHT/2), CC.P (300, 100),}-creates an action with the configuration structure body for the duration and Bezier curve local bezierforward = cc. Bezierby:create (3, Bézier) Local bezierback = Bezierforward:reverse ()--infinite loop execution sequence Local rep = cc. Repeatforever:create (CC. Sequence:create (Bezierforward, Bezierback))--Sprite 2 tamara:setposition (CC.P (80,160))--[[local bezier2 = CcB Ezierconfig () bezier2.controlpoint_1 = CC.P (+, SIZE.HEIGHT/2) bezier2.controlpoint_2 = CC.P ($,-SIZE.HEIGHT/2 ) Bezier2.endposition = CC.P (]]--local bezier2 ={cc.p (SIZE.HEIGHT/2), CC.P ($,-SIZE.HEIGHT/2), CC.P (240, 160) }-Creates a Bezier action, the first argument is duration, and the second parameter is the Bezier structure local bezierTo1 = cc. Bezierto:create (2, Bezier2)--Sprite 3 kathia:setposition (CC.P (400,160)) Local BEZIERTO2 = cc. Bezierto:create (2, Bezier2)--run Action Grossini:runaction (rep) tamara:runaction (bezierTo1) kathia:runaction (Bezierto 2) Helper.subtitleLabel:setString ("Bezierto/bezierby") return layer End
Blink class: Flashing action
----------------------------------------
actionblink
--Flashing motion
--------------------------------------
local function Actionblink ()
--Create layer
local layer = CC. Layer:create ()
--Initialization layer
initwithlayer (layer)
centersprites (2)
--Create two flicker actions, the first parameter is duration, the second parameter is the number of flashes
local action1 = cc. Blink:create (2, ten)
local action2 = cc. Blink:create (2, 5)
--Two sprites perform action
tamara:runaction (Action1)
kathia:runaction (action2)
Helper.subtitleLabel:setString ("Blink")
return layer
end
Fadeto class: Gradient action
Fadein class: Gradient action "reverse" action is fadeout
Fadeout class: Gradient action "reverse" action is Fadein
----------------------------------------
actionfade
--Gradient action
--------------------------------------
local function Actionfade ()
local layer = CC. Layer:create ()
initwithlayer (layer)
centersprites (2)
--Set transparency
tamara:setopacity (0)
-- Create a light-in action
local action1 = cc. Fadein:create (1)
--Reverse action, FadeOut
local action1back = Action1:reverse ()
--Create fade action
Local Action2 = cc. Fadeout:create (1)
--Reverse action, Fadein action
Local action2back = Action2:reverse ()
--Perform action
Tamara: Runaction (CC. Sequence:create (Action1, Action1back))
kathia:runaction (CC. Sequence:create (Action2, Action2back))
Helper.subtitleLabel:setString ("Fadein/fadeout")
return layer
End
Tintto Class: node discoloration action
Tintby Class: Node discoloration action, provides reverse method.
----------------------------------------
actiontint
--discoloration action
--------------------------------------
local function Actiontint ()
local layer = CC. Layer:create ()
initwithlayer (layer)
centersprites (2)
-Creates an action with duration and color, the first parameter is duration, and the following three is the color value
Local Action1 = cc. Tintto:create (2, 255, 0, 255)
Local action2 = cc. Tintby:create (2, -127, -255, -127)
local action2back = Action2:reverse ()
tamara:runaction (action1)
Kathia:runaction (CC. Sequence:create (Action2, Action2back))
Helper.subtitleLabel:setString ("Tintto/tintby")
return layer
End
Animation class: An animated object that is used to animate on a Sprite object.