Cocos2d-x 3.1.1 Lua sample ActionManagerTest (Action Management), cocos2dlua
Cocos2d-x 3.1.1 Lua example ActionManagerTest (Action Management)
This blog introduces the Cocos2d-x Action Management example, this example shows the Cocos2d-x of several actions:
MoveTo -- move an action to a certain point
MoveBy: A Moving action, similar to MoveTo, but MoveBy can be moved to a certain point and then returned based on the original path, providing the reverse method.
RotateTo-rotate a certain genie to a certain angle
RotateBy: A rotation action that rotates an angle of a certain genie. It has a reverse method, which allows the object to be rotated back to the original path.
ScaleTo-scale up or down a certain Sprite
Scaleby-scale up or down a certain Sprite. It has a reverse method that allows an object to be rotated back to the original path.
Knowledge points involved in this example include:
- Create an action Sequence, such as cc. Sequence: create (cc. DelayTime: create (1.4), cc. CallFunc: create (removeThis)
- Execute the action Sequence, for example: ret: runAction (cc. Sequence: create (cc. DelayTime: create (1.4), cc. CallFunc: create (removeThis )))
Let's take a look at the results of this example, and make a dynamic diagram to show you:
For specific code implementation and API usage, I hope you can take it seriously. If you are not clear about or understand the parameters, please consult Baidu or go to the official website to refer to the specific API, this is a learning method." ActionManagerTest. lua
Local kTagNode = 0 -- node ID local kTagGrossini = 1 -- local kTagSequence = 2 -- get the scheduler local schedence = cc associated with this ctor. director: getInstance (): getScheduler () tables Test1 tables local function CrashTest () -- create Test layer local ret = createTestLayer ("Test 1. shocould not crash ")- -Genie, s_pPathGrossini is the image path local child = cc. sprite: create (s_pPathGrossini) -- display to the position x = 200, y = 200 child: setPosition (200,200) ret: addChild (child, 1) -- Sum of all action's duration is 1.5 second. -- rotate a node for 1.5 seconds. Rotate for 90 degrees. child: runAction (cc. rotateBy: create (1.5, 90) -- execute the action sequence, delay of 1.4 seconds, fade out child: runAction (cc. sequence: create (cc. delayTime: create (1.4), cc. fadeOut: create (1.1) local function removeThis () -- overflow child Ret: getParent (): removeChild (ret, true) Helper. nextAction () end -- After 1.5 second, self will be removed. -- After 1.5 seconds, ret: runAction (cc. sequence: create (cc. delayTime: create (1.4), cc. callFunc: create (removeThis) return retend logs LogicTest -- logic test ------------------------------------------------------------------ local function L OgicTest () local ret = createTestLayer ("Logic test") -- genie, s_pPathGrossini is the image path local grossini = cc. sprite: create (s_pPathGrossini) -- add a subnode to the container, which has a z-axis sequence and a mark. Ret: addChild (grossini, 0, 2) grossini: setPosition (200,200) local function bugMe (node) -- stop all actions node: stopAllActions () -- After this stop next action not working, if remove this stop everything is working node: runAction (cc. scaleTo: create (2, 2) end -- run the action sequence grossini: runAction (cc. sequence: create (cc. moveBy: create (1, cc. p (), cc. callFunc: create (bugMe) return retend ----------------------------- Pause PauseTest -- Pause the Test. local function PauseTest () local ret = createTestLayer ("Pause Test") local schedulerEntry = nil local function unpause (dt) scheduler: unscheduleScriptEntry (schedulerEntry) schedulerEntry = nil local node = ret: getChildByTag (kTagGrossini) local pDirector = cc. director: get Instance () pDirector: getActionManager (): resumeTarget (node) end local function onNodeEvent (event) -- enter if event = "enter" then local s = cc. director: getInstance (): getWinSize () local l = cc. label: createWithTTF ("After 3 seconds grossini shoshould move", "fonts/Thonburi. ttf ", 16) ret: addChild (l) l: setAnchorPoint (cc. p (0.5, 0.5) l: setPosition (cc. p (s. width/2,245) local grossini = cc. sprite: create (S_pPathGrossini) ret: addChild (grossini, 0, kTagGrossini) grossini: setPosition (cc. p (200,200) -- creates a movement action. The duration is 1 second. The local action is equal to (, 0. moveBy: create (1, cc. p (150,0) local pDirector = cc. director: getInstance () -- get the ActionManager associated with ctor and add actions to the target -- add actions to a target. If the target already exists, the action is added to the existing target. -- If the target does not exist, a new object of the target will be created. This action will be added to the newly created object when the target action is paused, the order of the Action queue will not be messy. PDirector: getActionManager (): addAction (action, grossini, true) schedulerEntry = schedentry: scheduleScriptFunc (unpause, 3.0, false) -- exit elseif event = "exit" then if schedulerEntry ~ = Nil then schedtry: unscheduleScriptEntry (schedulerEntry) end -- register the RESPONSE event ret: registerScriptHandler (onNodeEvent) return retend initialize RemoveTest -- initialize local function RemoveTest () local ret = createTestLayer ("Remove Test") local l = cc. label: createWithTTF ("shocould no T crash "," fonts/Thonburi. ttf ", 16) -- Obtain the screen size. local s = cc. director: getInstance (): getWinSize () ret: addChild (l) l: setAnchorPoint (cc. p (0.5, 0.5) l: setPosition (cc. p (s. width/2,245) -- creates a Moving action, lasting 2 seconds, to the position (, 0) local pMove = cc. moveBy: create (2, cc. p (200, 0) -- stop the local function stopAction () -- Obtain the child node local pSprite = ret: getChildByTag (kTagGrossini) pSprite: stopActionByTag (kTagSequence) end- -Create a callback function: local callfunc = cc. callFunc: create (stopAction) local fetch quence = cc. sequence: create (pMove, callfunc) specify quence: setTag (kTagSequence) local pChild = cc. sprite: create (s_pPathGrossini) pChild: setPosition (200,200) ret: addChild (pChild, 1, kTagGrossini) pChild: runAction (resume quence) return retend resume ResumeTest -- Restore test -------- Required local function ResumeTest () local ret = createTestLayer ("Resume Test") local schedulerEntry = nil local function resumegrosini (time) scheduler: vertex (schedulerEntry) schedulerEntry = nil local pGrossini = ret: getChildByTag (kTagGrossini) local pDirector = cc. director: getInstance () pDirector: getActionManager (): resumeTarget (p Grossini) end local function onNodeEvent (event) if event = "enter" then local l = cc. label: createWithTTF ("Grossini only rotate/scale in 3 seconds", "fonts/Thonburi. ttf ", 16) ret: addChild (l) local s = cc. director: getInstance (): getWinSize () l: setAnchorPoint (cc. p (0.5, 0.5) l: setPosition (s. width/2,245) local pGrossini = cc. sprite: create (s_pPathGrossini) ret: addChild (pGrossini, 0, kTagGrossini) p Grossini: setPosition (200,200) -- run the scaling action pGrossini: runAction (cc. scaleBy: create (2, 2) local pDirector = cc. director: getInstance () -- Pause target pDirector: getActionManager (): pauseTarget (pGrossini) -- run the rotation action, rotate 360 degrees, lasting 2 seconds pGrossini: runAction (cc. rotateBy: create (2,360) schedulerEntry = scheduler: scheduleScriptFunc (resumegrosini, 3.0, false) elseif event = "exit" then if schedulerEntry ~ = Nil then schedtry: unscheduleScriptEntry (schedulerEntry) end ret: registerScriptHandler (onNodeEvent) return retendfunction ActionManagerTestMain () cclog ("ActionManagerTestMain") Helper. index = 1 -- Initial index is 1 -- set the deep test cc. director: getInstance (): setDepthTest (true) -- create scenario local scene = cc. scene: create () -- initialization method table Helper. createFunctionTable = {CrashTest, LogicTest, PauseTest, RemoveTest, ResumeTest} -- add layer scene: addChild (CrashTest () scene: addChild (CreateBackMenuItem () return sceneend
Some defined resource paths and related help classes are used in this example. You can find them in the corresponding directory:
Help. lua (help class, which encapsulates and defines related methods, creates test layers, switches scenarios, and so on)
Require "Cocos2d" CC_CONTENT_SCALE_FACTOR = function () -- get the surface size, in pixels return cc. director: getInstance (): getContentScaleFactor () end -- convert a rectangle in pixels to a rectangle in the unit of points. CC_POINT_PIXELS_TO_POINTS = function (pixels) return cc. p (pixels. x/CC_CONTENT_SCALE_FACTOR (), pixels. y/CC_CONTENT_SCALE_FACTOR () end -- convert a rectangle in points to a rectangle in pixels. CC_POINT_POINTS_TO_PIXELS = function (points) return cc. p (points. x * CC_CONTENT_SCALE_FACTOR (), points. y * CC_CONTENT_SCALE_FACTOR () end -- cclog print log cclog = function (...) print (string. format (...)) end -- change table to enum type converts the table to the enumerated type function CreateEnumTable (tbl, index) local enumTable = {} local enumIndex = index or-1 for I, v in ipairs (tbl) do enumTable [v] = enumIndex + I end return enumTableend -- back menu callback return menu callback local function MainMenuCallback () local scene = cc. scene: create () scene: addChild (CreateTestMenu () -- switch scenario cc. director: getInstance (): replaceScene (scene) end -- add the menu item for back to main menu -- add the menu item function CreateBackMenuItem () to the return main menu () -- create a label local label = cc. label: createWithTTF ("MainMenu", s_arialPath, 20) -- setAnchorPoint (cc. p (0.5, 0.5) -- set the menu item label local MenuItem = cc. menuItemLabel: create (label) MenuItem: registerScriptTapHandler (MainMenuCallback) -- Obtain the screen size. local s = cc. director: getInstance (): getWinSize () -- create Menu local Menu = cc. menu: create () -- add Menu item Menu: addChild (MenuItem) -- Set Menu position Menu: setPosition (0, 0) -- set Menu item position, roughly in the lower right corner of the position MenuItem: setPosition (s. width-50, 25) return Menuend -- help class Helper = {index = 1, -- index createFunctioinTable = nil, -- store method table currentLayer = nil, -- Current layer titleLabel = nil, -- title subtitleLabel = nil -- subtitle} -- function Helper of the next action. nextAction () Helper. index = Helper. index + 1 -- index plus 1 if Helper. index> table. getn (Helper. createFunctionTable) then Helper. index = 1 end return Helper. newScene () end -- rollback Action function Helper. backAction () Helper. index = Helper. index-1 if Helper. index = 0 then Helper. index = table. getn (Helper. createFunctionTable) end return Helper. newScene () end -- restart the function Helper. restartAction () return Helper. newScene () end -- switch to the new scenario function Helper. newScene () local scene -- if you use the physical effect if Helper. usePhysics then -- creates a scenario with physical effects scene = cc. scene: createWithPhysics () else scene = cc. scene: create () end Helper. currentLayer = Helper. createFunctionTable [Helper. index] () -- add the current layer scene: addChild (Helper. currentLayer) -- add return menu scene: addChild (CreateBackMenuItem () -- switch scenario cc. director: getInstance (): replaceScene (scene) end -- initialization layer function Helper. initWithLayer (layer) Helper. currentLayer = layer -- get the screen size. local size = cc. director: getInstance (): getWinSize () Helper. titleLabel = cc. label: createWithTTF ("", s_arialPath, 28) Helper. titleLabel: setAnchorPoint (cc. p (0.5, 0.5) layer: addChild (Helper. titleLabel, 1) Helper. titleLabel: setPosition (size. width/2, size. height-50) Helper. subtitleLabel = cc. label: createWithTTF ("", s_thonburiPath, 16) Helper. subtitleLabel: setAnchorPoint (cc. p (0.5, 0.5) layer: addChild (Helper. subtitleLabel, 1) Helper. subtitleLabel: setPosition (size. width/2, size. height-80) -- menu local item1 = cc. menuItemImage: create (s_pPathB1, s_pPathB2) local item2 = cc. menuItemImage: create (s_pPathR1, s_pPathR2) local item3 = cc. menuItemImage: create (s_pPathF1, s_pPathF2) item1: registerScriptTapHandler (Helper. backAction) item2: registerScriptTapHandler (Helper. restartAction) item3: registerScriptTapHandler (Helper. nextAction) local menu = cc. menu: create () menu: addChild (item1) menu: addChild (item2) menu: addChild (item3) menu: setPosition (cc. p (0, 0) -- place item1: setPosition (cc. p (size. width/2-item2: getContentSize (). width * 2, item2: getContentSize (). height/2) item2: setPosition (cc. p (size. width/2, item2: getContentSize (). height/2) item3: setPosition (cc. p (size. width/2 + item2: getContentSize (). width * 2, item2: getContentSize (). height/2) layer: addChild (menu, 1) local background = cc. layer: create () layer: addChild (background,-10) end -- create a test layer function createTestLayer (title, subtitle) -- create a Layer local layer = cc. layer: create () Helper. initWithLayer (layer) local titleStr = title = nil and "No title" or title local subTitleStr = subtitle = nil and "" or subtitle Helper. titleLabel: setString (titleStr) Helper. subtitleLabel: setString (subTitleStr) return layerend
TestResource. lua
Defines all resource paths for convenient management and Retrieval
s_pPathGrossini = "Images/grossini.png"s_pPathSister1 = "Images/grossinis_sister1.png"s_pPathSister2 = "Images/grossinis_sister2.png"s_pPathB1 = "Images/b1.png"s_pPathB2 = "Images/b2.png"s_pPathR1 = "Images/r1.png"s_pPathR2 = "Images/r2.png"s_pPathF1 = "Images/f1.png"s_pPathF2 = "Images/f2.png"s_pPathBlock = "Images/blocks.png"s_back = "Images/background.png"s_back1 = "Images/background1.png"s_back2 = "Images/background2.png"s_back3 = "Images/background3.png"s_stars1 = "Images/stars.png"s_stars2 = "Images/stars2.png"s_fire = "Images/fire.png"s_snow = "Images/snow.png"s_streak = "Images/streak.png"s_PlayNormal = "Images/btn-play-normal.png"s_PlaySelect = "Images/btn-play-selected.png"s_AboutNormal = "Images/btn-about-normal.png"s_AboutSelect = "Images/btn-about-selected.png"s_HighNormal = "Images/btn-highscores-normal.png"s_HighSelect = "Images/btn-highscores-selected.png"s_Ball = "Images/ball.png"s_Paddle = "Images/paddle.png"s_pPathClose = "Images/close.png"s_MenuItem = "Images/menuitemsprite.png"s_SendScore = "Images/SendScoreButton.png"s_PressSendScore = "Images/SendScoreButtonPressed.png"s_Power = "Images/powered.png"s_AtlasTest = "Images/atlastest.png"-- tilemaps resources_TilesPng = "TileMaps/tiles.png"s_LevelMapTga = "TileMaps/levelmap.tga"-- spine test resources_pPathSpineBoyJson = "spine/spineboy.json"s_pPathSpineBoyAtlas = "spine/spineboy.atlas"-- fonts resources_markerFeltFontPath = "fonts/Marker Felt.ttf"s_arialPath = "fonts/arial.ttf"s_thonburiPath = "fonts/Thonburi.ttf"s_tahomaPath = "fonts/tahoma.ttf"
How to combine cocos2d-x with Lua to use specific points
The cocos2d-x comes with the lua engine. Look at this. Although it is English, it is easy to understand.
Cocos2d-x NEW Lua Engine README
Main features
Support autorelease CCObject object.
Call Lua function from C ++ (local, global, closure), avoid memory leaks.
Add CCNode: setPosition (x, y), CCNode: getPosition () huge performance boost.
Remove needless class and functions from tolua ++. pkg files, improved performance.
CCMenuItem events handler.
CCNode onEnter/onExit events handler.
CCLayer touch & multi-touches events handler.
Check the two connections.
Www.cocos2d-x.org/..uments
Scripting and Translating between Programming ages
When creating a cocos2D-x project in VS2010, Select Lua support: Support Lua (Hook), an error is reported after generation,
Some library files are not properly loaded. Find the corresponding library file and paste it to the corresponding location. Do not forget to check whether the file has permissions.