Note: Make an action game with COCOS2DX 3.3 Lua (i)

Source: Internet
Author: User

Before you do that, declare that:

I'm a puppy after all.

No way, no perseverance and perseverance, before the quick cocos2dx to do the horizontal version of the game has turned into a version of the 3.3, in fact, is a semi-finished, fighting, UI what all have, hehe.

The purpose of this demo is to achieve the following:

1 familiar with COCOS2DX 3.3-lua

2 Using Joystick

3 Complete the simple monster AI

4 trying to extend an existing API (optional)

Well, it's almost over.

Today for the first time notes.

The tasks currently completed are as follows:

1 Creating a new project with a newer player

2 Adding a Sprite and its frame animation to the scene

3-Frame animation playback and stop

The complete code is as follows:

1 LocalMainscene = Class ("Mainscene",function()2     returnDisplay.newscene ("Mainscene")3 End)4 5 functionMainscene:ctor ()6Display.newsprite ("bg.jpg")7 :p os (display.cx, display.cy)8 : AddTo (self)9 EndTen  One functionMainscene:onenter () ADisplay.addspriteframes ("hero/zhuge.plist","Hero/zhuge.png") -Self.player =Display.newsprite () - self:addchild (Self.player) the Self.player:pos (display.cx, display.cy) -Self.animaction = Self:playanimation (Self.player,"Standby",0, -,false) -Self:settouchenabled (true) -Self:addnodeeventlistener (CC. Node_touch_event,function(Event) + self:ontouched (Event) -     End) + End A  at functionmainscene:ontouched (Event) -Self.animaction = Self:playanimation (Self.player,"Attack",0,9,true) - End -  - functionmainscene:playanimation (Player, FrameName, startindex, Endindex, once) -     LocalAnimationname =Player:getname (). FrameName in     LocalAnimation =Display.getanimationcache (animationname) -     ifAnimation = =Nil  Then to         LocalFrames = Display.newframes (framename."%04d", startindex, Endindex) +Animation = Display.newanimation (frames,1/ -) - Display.setanimationcache (animationname,animation) the     End *     ifSelf.animaction ~=Nil  Then $ transition.removeaction (self.animaction)Panax NotoginsengSelf.animaction =Nil -     End the     Local functiononplaycompleted () +Self.animaction = Self:playanimation (Self.player,"Standby",0, -,false) A     End the     ifonce = =true  Then +         returnPlayer:playanimationonce (animation,false, onplaycompleted,0) -     Else $         returnPlayer:playanimationforever (animation,0) $     End - End -  the functionmainscene:onexit () - EndWuyi  the returnMainscene
Mainscene

Learning points:

1 Making resources using Flash CS6:

Some animated GIF resources have been found on the website, and a movie clip is automatically generated after importing it directly to the library via Flash CS6, and all actions can be imported into the same library:

Then need to go into each action movie clip, adjust its registration point, my registration point alignment is, X-aligned belly belt of the middle point, y is directly negative height, so that each action of excessive should not be too sudden:

After each action has been adjusted, press CTR, then select the action clip that needs to export the footage, then right-click to generate the Spritesheet table ... :

Double-check the Hint box options:

It is especially important to note that the stack frame is the option to remove duplicate images.

Then click Export and you can use it directly in the project.

Use of 2-frame animations in cocos2dx Lua v3.3:

The most important piece of code for this time:

functionmainscene:playanimation (Player, FrameName, startindex, Endindex, once)LocalAnimationname =Player:getname (). FrameNameLocalAnimation =Display.getanimationcache (animationname)ifAnimation = =Nil  Then        LocalFrames = Display.newframes (framename."%04d", startindex, endindex) animation= Display.newanimation (Frames,1/ -) Display.setanimationcache (animationname,animation)End    ifSelf.animaction ~=Nil  Thentransition.removeaction (self.animaction) self.animaction=Nil    End    Local functiononplaycompleted () self.animaction= Self:playanimation (Self.player,"Standby",0, -,false)    End    ifonce = =true  Then        returnPlayer:playanimationonce (animation,false, onplaycompleted,0)    Else        returnPlayer:playanimationforever (animation,0)    EndEnd

This approach mainly uses Display.setanimationcache and Display.getanimationcache to cache and read frame animations.

This method then returns an actionthat corresponds to the actionof the animation that is played, because in the Spriteex.lua Source we can find:

function sprite:playanimationonce (animation, removewhenfinished, OnComplete, delay)     return transition.playanimationonce (self, animation, removewhenfinished, OnComplete, delay) End function Sprite:playanimationforever (animation, delay)     return Transition.playanimationforever (self, animation, delay) End

The playback of the frame animation is given to the transition implementation.

Spritesheet can be used in the program because we have written in OnEnter before:

Display.addspriteframes ("hero/zhuge.plist","hero/zhuge.png" )

This section ends.

Note: Make an action game with COCOS2DX 3.3 Lua (i)

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.