Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Lua script calls cocos2d-x how to create a wizard, Tmx map

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about how lua scripts call cocos2d-x to create sprite and Tmx maps. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Hello, everyone, the editor is here today with you through the Lua script to create a wizard and Tmx map with cocos2d-x 2.0.4, so as to provide a learning example for you to enter IOS game development. Old people, experts, experts, great gods, please float by consciously, ha, of course, if you want to pat bricks, please show mercy.

It would be very grateful if the prawns would leave a footprint or other relevant learning examples.

The construction of the development environment will not be introduced, ha, we can do Niang, Google a turn.

All right, don't bother, let's start the official coding. One more nonsense, if you don't know the lua script, please sweep away the blindness of Duniang and Google.

First create a good project, if you do not understand, you can directly from the samples of cocos2d-x to get a project to change.

1) create a wizard by calling cocos2d-x through lua

Prepare a picture of the elf walking in four directions

Define createSprite script methods to generate sprites

-- create sprite local sprite = nil-- define sprite variable local function createSprite () local layer = CCLayer:create ()-- define layer local frame0-- define sprite to display picture frames by default-- create frame animation through CCSpriteFrame frame0 = CCSpriteFrame:create ("p_w_picpaths/spirit001_32.png", CCRectMake (blockWH*0, blockWH*0, blockWH) BlockWH)-- initialize the sprite sprite = CCSprite:createWithSpriteFrame (frame0) by frame animation-- set the sprite default starting position sprite:setPosition (ccp (winSize.width / 2-64, winSize.height / 3))-- add the sprite to the layer layer:addChild (sprite) return layer-return the layer End for CCScene

2) realize sprite frame animation by calling cocos2d-x through lua

According to the characteristics of the genie picture: the first behavior walking downward animation; the second behavior walking animation to the left; the third behavior walking animation to the right; the fourth behavior walking animation upward.

-- create sprite frame animation action local animation = nil-- define action direction: sprite direction (top, bottom, left, right) local function createAnimaByDirection (direction) local frame0,frame1,frame2,frame3-define four sprite animation frame0 = CCSpriteFrame:create ("TileMaps/spirit001_32.png", CCRectMake (blockWH*0, blockWH*direction, blockWH, blockWH)) frame1 = CCSpriteFrame:create ("TileMaps/spirit001_32.png", CCRectMake (blockWH*1, blockWH*direction, blockWH) BlockWH) frame2 = CCSpriteFrame:create ("TileMaps/spirit001_32.png", CCRectMake (blockWH*2, blockWH*direction, blockWH, blockWH)) frame3 = CCSpriteFrame:create ("TileMaps/spirit001_32.png", CCRectMake (blockWH*1, blockWH*direction, blockWH, blockWH))-- create an array Used to store sprite frame actions local frameArray = CCArray:create () frameArray:addObject (frame0) frameArray:addObject (frame1) frameArray:addObject (frame2) frameArray:addObject (frame3)-create animation through CCAnimation:createWithSpriteFrames-Parameter 1: frame action array Parameter 2: animation playback time per frame animation = CCAnimation:createWithSpriteFrames (frameArray, 0.2) animation:setLoops (10)-return the animation for the wizard to use return CCAnimate:create (animation) end

3) create a menu to control the wizard animation

To facilitate the demonstration of the example, we create four menu items to control the playback of the upper, lower, left, and right animations of the wizard.

-- define menu item local menuLeft,menuRight,menuLeft,menuRight Menu-create control menu local function createControllerMenu () local layer = CCLayer:create ()-initialize menu item menuLeft = CCMenuItemImage:create ("Images/menu/leftNormal.png", "Images/menu/leftPress.png") menuLeft:registerScriptTapHandler (runLeft)-click handler menuLeft:setPosition (ccp (menuLeft:getContentSize (). Width/2*3/2, menuLeft:getContentSize () height * 2))-- menu item position menuUp = CCMenuItemImage:create ("Images/menu/upNormal.png") "Images/menu/upPress.png") menuUp:registerScriptTapHandler (runUp) menuUp:setPosition (ccp (menuLeft:getContentSize (). Width/2*3/2+menuLeft:getContentSize (). Width, menuLeft:getContentSize () height * 3)) menuRight = CCMenuItemImage:create ("Images/menu/rightNormal.png", "Images/menu/rightPress.png") menuRight:registerScriptTapHandler (runRight) menuRight:setPosition (ccp (menuLeft:getContentSize (). Width/2*3/2+menuLeft:getContentSize (). Width*2 MenuLeft:getContentSize () .height * 2) menuDown = CCMenuItemImage:create ("Images/menu/downNormal.png", "Images/menu/downPress.png") menuDown:registerScriptTapHandler (runDown) menuDown:setPosition (ccp (menuLeft:getContentSize (). Width/2*3/2+menuLeft:getContentSize (). Width, menuLeft:getContentSize () height))-create menu menu = CCMenu:create () Menu:addChild (menuLeft)-add menu items to the menu menu:addChild (menuUp) menu:addChild (menuRight) menu:addChild (menuDown) menu:setPosition (ccp (0,0))-add the menu to the layer layer:addChild (menu) return layer end-- wizard walk to the left animation local function runLeft () sprite:runAction (createAnimaByDirection (left))-the method in step 2 to create end-- walking animation in other directions is the same as runLeft, so it is not listed in detail. -- where left:1;down:0;right:2;up:3 corresponds to the corresponding direction of the sprite image.

At this point, we have completed the script code for the creation of sprites and the control of sprite walking animation by calling cocos2d-x through lua scripts.

The result is: play the wizard walking animation to the left when the left walk menu is pressed, play the wizard walk animation to the right when the right walk menu is pressed, play the wizard walk animation down when the walk menu is pressed, and play the sprite walk up animation when the walk menu is pressed.

4) call cocos2d-x through lua script to create tmx map

Tiled this map editing tool can edit a lot of 2D game maps, as long as you want to do, specific usage and software download and installation everyone will have a pile of information, here will not be verbose.

First use tiled to create a map file such as test-tilemap.tmx, and then you can write a lua script to create the operation, as follows:

-- create tmx map layer local function CreateTileMapLayer () local layer = CCLayer:create ()-- create TMX map by file name local map = CCTMXTiledMap:create ("TileMaps/test-tilemap.tmx") layer:addChild (map)-- add tmx map to layer return layer end

It's easy to create tmx map layers in a few lines of script. Of course, this is just an example, there will be more events to deal with in the specific game.

5) display to the phone screen

Add the layers created in the above steps to the scene, and then you can display them on the phone screen.

-- create the scene local scene = CCScene:create () scene:addChild (CreateTileMapLayer ())-- add the tmx map layer to the scene scene:addChild (createSprite ())-- add the spirit layer to the scene scene:addChild (createControllerMenu ())-- add the control menu layer to the scene CCDirector:sharedDirector (): replaceScene (scene)-- display the scene on the mobile screen. Thank you for reading! This is the end of the article on "how lua script calls cocos2d-x to create genie and Tmx map". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report