zoukankan      html  css  js  c++  java
  • Quick Cocos2dx 与 EnterFrame事件

    利用EnterFrame做出行走的效果,效果图如下:

    具体操作:

    1 给self多加一个bg1用作与bg无限循环换位

    2 在AnotherScene:onEnter方法里面新增onEnterFrame的排成

    3 写了一个简单的onEnterFrame方法用作每帧去更新

    完整代码如下:

     1 local AnotherScene = class("AnotherScene", function()
     2     return display.newScene("AnotherScene")
     3 end)
     4 
     5 function AnotherScene:ctor()
     6     self.speed = 2;
     7     self.curBehaviorId = 1;
     8     self.layer = display.newLayer()
     9     self:addChild(self.layer)
    10     self.layer:setTouchEnabled(true)
    11     self.behaviors = {"anim_walk","anim_eat","anim_placeladder","anim_idle","anim_ladderwalk","anim_laddereat","anim_death"};
    12     self.bg = display.newSprite("battle.png",display.cx, display.cy)
    13     self.bg1 = display.newSprite("battle.png",display.cx+self.bg:getContentSize().width, display.cy)
    14     self.layer:addChild(self.bg)
    15     self.layer:addChild(self.bg1)
    16 end
    17 
    18 function AnotherScene:onTouch(event, x, y)
    19     print("Touched at %d %d",x,y)
    20     self.curBehaviorId = self.curBehaviorId + 1;
    21     if self.curBehaviorId > #self.behaviors then
    22         self.curBehaviorId = 1;
    23     end
    24     print("Now playing ", self.curBehaviorId , #self.behaviors, self.behaviors[self.curBehaviorId])
    25     self.animation:play(self.behaviors[self.curBehaviorId])
    26 end
    27 
    28 function AnotherScene:onEnterFrame(dt)
    29     local tempX = self.bg:getPositionX();
    30     local tempW = self.bg:getContentSize().width;
    31     tempX = tempX - self.speed;
    32     if tempX <= display.cx - tempW then
    33         self.bg:setPositionX(display.cx + tempW)
    34     else
    35         self.bg:setPositionX(tempX)
    36     end
    37 
    38     tempX = self.bg1:getPositionX();
    39     tempW = self.bg1:getContentSize().width;
    40     tempX = tempX - self.speed;
    41     if tempX <= display.cx - tempW then
    42         self.bg1:setPositionX(display.cx + tempW)
    43     else
    44         self.bg1:setPositionX(tempX)
    45     end
    46 end
    47 
    48 function AnotherScene:onEnter()
    49     ui.newTTFLabel({text = "Yo! Yo! Check Out!", size = 64, align = ui.TEXT_ALIGN_CENTER})
    50         :pos(display.cx, display.cy)
    51         :addTo(self.layer)
    52 
    53     local manager = CCArmatureDataManager:sharedArmatureDataManager()
    54     manager:addArmatureFileInfo("Zombie.png","Zombie.plist","Zombie.xml")
    55     local zombie = CCNodeExtend.extend(CCArmature:create("Zombie_ladder"))
    56     zombie:connectMovementEventSignal(function(__evtType, __moveId)
    57             echoInfo("movement, evtType: %d, moveId: %s", __evtType, __moveId)
    58         end)
    59     self.animation = zombie:getAnimation()
    60     self.animation:setAnimationScale(0.5)
    61     self.animation:play("anim_walk")
    62     zombie:setPosition(display.cx, display.cy)
    63     zombie:setScaleX(-1)
    64     self.layer:addChild(zombie)
    65     self.layer:addTouchEventListener(function(event,x,y)
    66         return self:onTouch(event, x,y)
    67         end)    
    68     self.layer:setTouchEnabled(true)
    69     self:scheduleUpdate(function(dt) 
    70         self:onEnterFrame(dt)
    71     end)
    72 end
    73 return AnotherScene

    也可以去我的git里面下载源代码和资源:https://github.com/AdoBeatTheWorld/waytomobile

    game003即为当前的项目,问我为啥没有把项目分出来,我只能说我.....很.....懒。

  • 相关阅读:
    137. 只出现一次的数字 II
    JS_利用Canvas进行图片旋转
    JS_图片压缩并预览
    计蒜客——等和的分隔子集
    中缀表达式转后缀并计算(只考虑个位整数,不考虑除0等情况)
    求最小数 * 区间和最大值
    967 质量检测
    PAT-1102(Invert a Binary Tree)
    PAT-1100(Mars Numbers)
    PAT-1099(Build A Binary Search Tree)
  • 原文地址:https://www.cnblogs.com/adoontheway/p/3824046.html
Copyright © 2011-2022 走看看