博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Quick Cocos2dx 与 EnterFrame事件
阅读量:7077 次
发布时间:2019-06-28

本文共 3061 字,大约阅读时间需要 10 分钟。

利用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 end17 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 then22 self.curBehaviorId = 1;23 end24 print("Now playing ", self.curBehaviorId , #self.behaviors, self.behaviors[self.curBehaviorId])25 self.animation:play(self.behaviors[self.curBehaviorId])26 end27 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 then33 self.bg:setPositionX(display.cx + tempW)34 else35 self.bg:setPositionX(tempX)36 end37 38 tempX = self.bg1:getPositionX();39 tempW = self.bg1:getContentSize().width;40 tempX = tempX - self.speed;41 if tempX <= display.cx - tempW then42 self.bg1:setPositionX(display.cx + tempW)43 else44 self.bg1:setPositionX(tempX)45 end46 end47 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 end73 return AnotherScene

 

也可以去我的git里面下载源代码和资源:

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

 

 

转载于:https://www.cnblogs.com/adoontheway/p/3824046.html

你可能感兴趣的文章
Spring Jackson AjaxFileUpload 没有执行回调函数的解决办法
查看>>
Liunx笔记:zabbix编译安装
查看>>
DUBBO服务调用超时问题记录
查看>>
【学习笔记】屏幕尺寸的信息
查看>>
Linux下启动Java进程并获得进程ID(PID)
查看>>
Android单元测试
查看>>
报表性能优化方案之数据集缓存与共享
查看>>
Linux RAID
查看>>
R文件系统管理
查看>>
android sqllite 使用笔记
查看>>
Oracle 关闭(shutdown immediate)时hang住
查看>>
【unity】关于时间等常用工具类
查看>>
在论坛中出现的比较难的sql问题:12(递归问题2)
查看>>
PXE结合kiskstart实现自动化安装系统
查看>>
Mysql isam数据库恢复实战
查看>>
mysql LINESTRING ,POINT 类型操作
查看>>
centos7 双网卡双ip内外网设置最小化安装
查看>>
第十次课作业(风险管理、项目收尾、知识产权)
查看>>
win10系统 VMWare12.5虚拟机 Ubuntu 16.04 LTS 开始我的Linux学习
查看>>
数据库 Oracle12c (三):安装与启动
查看>>