在这之前,声明一下:

做不完我是小狗。

 

没办法,没毅力和恒心,之前的那个Quick Cocos2dx做的横版过关游戏的demo已经转成了3.3的版本了,其实也算是个半成品,战斗,UI啥的都有了,呵呵。

 

本次DEMO要达成的目的如下:

1 熟悉Cocos2dx 3.3 - lua

2 使用Joystick

3 完成简单的怪物AI

4 尝试扩展现有的api(可选)

 

嗯,差不多就以上了。

 

今天第一次笔记。

当前完成的任务有如下:

1 使用新的player新建项目

2 在场景中添加Sprite以及其帧动画

3 帧动画的播放与停止

 

完整代码如下:

 1 local MainScene = class("MainScene", function()
 2     return display.newScene("MainScene")
 3 end)
 4 
 5 function MainScene:ctor()
 6     display.newSprite("bg.jpg")
 7         :pos(display.cx, display.cy)
 8         :addTo(self)
 9 end
10 
11 function MainScene:onEnter()
12     display.addSpriteFrames("hero/zhuge.plist","hero/zhuge.png")
13     self.player = display.newSprite()
14     self:addChild(self.player)
15     self.player:pos(display.cx, display.cy)
16     self.animAction = self:playAnimation(self.player, "standby", 0, 48, false)
17     self:setTouchEnabled(true)
18     self:addNodeEventListener(cc.NODE_TOUCH_EVENT, function( event )
19         self:onTouched(event)
20     end)
21 end
22 
23 function MainScene:onTouched( event )
24     self.animAction = self:playAnimation(self.player, "attack", 0, 9, true)
25 end
26 
27 function MainScene:playAnimation(player, framename, startindex, endindex, once)
28     local animationname = player:getName()..framename
29     local animation = display.getAnimationCache(animationname)
30     if animation == nil then
31         local frames = display.newFrames(framename.."%04d",startindex, endindex)
32         animation = display.newAnimation(frames,1/24)
33         display.setAnimationCache(animationname,animation)
34     end
35     if self.animAction ~= nil then
36         transition.removeAction(self.animAction)
37         self.animAction = nil
38     end
39     local function onPlayCompleted( )
40         self.animAction = self:playAnimation(self.player, "standby", 0, 48, false)
41     end
42     if once == true then
43         return player:playAnimationOnce(animation,false,onPlayCompleted,0)
44     else
45         return player:playAnimationForever(animation,0)
46     end
47 end
48 
49 function MainScene:onExit()
50 end
51 
52 return MainScene
MainScene

相关文章: