5、cocos2d-Lua的demo--虫子和虫子精灵

5、cocos2d-Lua的demo--虫子和虫子精灵,第1张

概述我们作个比方,场景好比舞台的一幕,models下的类相当于剧本中的角色(注意只是剧本中的),是死的,并没有被演活,那么精灵就好比一个个地演员,把角色演活。 因此虫子类仅仅是类,虫子精灵负责按照虫子类描述的特性执行其动作。 但是这个demo项目在这方面处理得相当蹩脚,且容我整理后再梳理清楚。这一节主要是优化GameView和虫子类以及虫子精灵的分工。 逻辑层应当只负责宏观的逻辑执行,它最好不要去涉及 我们作个比方,场景好比舞台的一幕,models下的类相当于剧本中的角色(注意只是剧本中的),是死的,并没有被演活,那么精灵就好比一个个地演员,把角色演活。
因此虫子类仅仅是类,虫子精灵负责按照虫子类描述的特性执行其动作。
但是这个demo项目在这方面处理得相当蹩脚,且容我整理后再梳理清楚。这一节主要是优化GameVIEw和虫子类以及虫子精灵的分工。
逻辑层应当只负责宏观的逻辑执行,它最好不要去涉及到具体某些类或对象的初始化逻辑,特别是“角色”众多时应该用类来统一管理,然后派生类各自实现自己不同的地方,逻辑层只负责调用它们的公共接口或函数即可。 遵循这个原则,最后把虫子基类、蚂蚁类、蜘蛛类优化为如下(原始代码不再作分析,因为太混乱,难以梳理):
虫子基类:
local BUGBase = class("BUGBase")

function BUGBase:ctor()
self.name = nil
imagefilename = position_ = cc.p(0,0)
rotation_ = 0
dist_ = destination_ = speed_ = 1
touchRange_ = animationTimes_ = nil
end

getname()
if name==nil then
printError('uninit name')
return name
getimagefilename()
imagefilename=='uninit imagefilename')
imagefilename
getposition()
return position_
getRotation()
rotation_
getdist()
dist_
getAnimationTimes()
animationTimes_=='uninit animationTimes_')
animationTimes_
setDestination(destination)
clone(dist_ = math.random(display.wIDth / 2 + 100,200)

rotation = random(360)
position_ = self:calcposition(rotation,dist_,153)">rotation - 180
self
fixedDeltaTime = 1.0 / 60.0
step(dt)
dist_ = dist_ - speed_ * (dt / fixedDeltaTime)
calcposition(rotation_ + 180,43)">destination_)
rotation,255)">dist,255)">destination)
radians = rotation * pi / return cc.p(destination.x + math.cos(radians) * dist,
destination.y - math.sin(radians) * dist)
checktouch(x,255)">y)
dx,153)">dy = x - position_.x,255)">y - y
offset = sqrt(dx * dx + dy * dy)
return offset <= touchRange_
BUGBase

优化之处: 去掉了虫子类型,有了派生类干嘛还用枚举来区分不同的类?神经病一样,典型的C语言思想,没有把类好好利用好。
增加了私有变量:name(虽然源码里面没有使用,但是看起来会舒服清晰很多),imagefilename(用于指示该类的图片资源),animationTimes_(动画延迟时间),并分别为它们设定了对应的get函数。


蚂蚁类:
import(".BUGBase")

BUGAnt = "BUGAnt",153)">BUGBase)

BUGAnt:ctor()
BUGAnt.super.ctor(self)
name = 'BUGAnt'
imagefilename = 'BUGAnt.png'
1.0
70
animationTimes_ = 0.15
BUGAnt
蜘蛛类:
BUGSpIDer = "BUGSpIDer",153)">BUGSpIDer:ctor()
BUGSpIDer.'BUGSpIDer'
'BUGSpIDer.png'
1.5
50
0.1
BUGSpIDer
派生类各自在自己的构造函数中初始化自己的数据,什么?担心会忘记设置?因此在基类的构造函数中把这几个私有变量初始化为nil,并在对应的get函数中进行判断,如果没有被初始化过则输出错误信息,基类的作用就是为派生类制造规范的行为准则,一切就这么简单方便!


接下来是虫子精灵和虫子死亡精灵:
虫子精灵:
BUGSprite = "BUGSprite",128)">function(BUGObj)
imagefilename = BUGObj:texture = getimage(imagefilename)
if texture==loadImage( local frameWIDth = texture:getPixelsWIDe() / 3
frameHeight = getPixelsHigh()

spriteFrame = newSpriteFrame(texture,43)">rect(frameWIDth,153)">frameHeight))
sprite = newSprite(spriteFrame)
sprite.animationname_ = imagefilename
sprite.frameWIDth_ = frameWIDth
frameHeight_ = frameHeight

--判断动画是否已经缓存,如果没有则创建
if getAnimationCache(animationname_)==-- create sprite frame based on image
frames = {}
for i = 1 do
frame = frameWIDth * i,153)">frames[#frames + 1] = frame
-- create animation
animation = newAnimation(frames,43)">getAnimationTimes())
-- caching animation
setAnimationCache(animationname_,153)">animation)
return sprite
end)

BUGSprite:BUGObj)
model_ = BUGObj
getModel()
model_
start(model_:updateposition()
playAnimationForever(getAnimationCache(animationname_))
updateposition()
move(getposition())
:rotate(getRotation())
BUGSprite

虫子死亡精灵:
DeadBUGSprite = "DeadBUGSprite",43)">getimage(getimagefilename())
getPixelsHigh()
2,128)">return display.newSprite(spriteFrame)
DeadBUGSprite
这里一并分析说明,原始的逻辑是GameVIEw里面告诉虫子精灵和死亡虫子精灵初始化的资源文件在哪里并如何初始化,神经病一样!正确的做法是:演员自己去看剧本,自行初始化自己的资源,因此这里优化后的虫子精灵只需通过虫子基类的get函数来获取对应的资源进行初始化即可。 这里需要注意的是:
end
一开始是getimage是为nil的,只需要调用loadImage加载一次即可,后面的精灵对象便可以使用getimage获取了。死亡虫子精灵获取资源时也应该这样,但是由于死亡虫子精灵和虫子精灵使用的资源是同一个图片,因此前面加载过了这里就可以直接使用了,这里对资源的 *** 作暂时不做修改了。
死亡虫子精灵和虫子精灵的类初始化函数(暂时这么叫吧,以便于区分后面的构造函数)由原来的接受资源文件名改为直接接受虫子对象,然后通过get函数来获取它们具体的资源文件名,这样清晰多了。那么GameVIEw的初始化就变得相当简单了,我们看看原来GameVIEw是如何添加虫子和虫子精灵的:
GameVIEw:addBUG()
BUGType = BUGBase.BUG_TYPE_ANT
1,255)">2) % 2 == 0 BUG_TYPE_SPIDER
BUGModel
BUGType == BUG_TYPE_ANT BUGModel = BUGAnt:create()
else
BUGModel = BUGSpIDer:BUG = BUGSprite:create(GameVIEw.IMAGE_filenameS[BUGType],153)">BUGModel)
:start(GameVIEw.HolE_position)
:addTo(BUGsNode_,GameVIEw.ZORDER_BUG)

BUGs_[BUG] = BUG
end
再看看优化后的:
BUGClass = {BUGAnt,BUGSpIDer}
index = BUGClass)
BUGObj = BUGClass[index]:create()

create(BUGObj)
:HolE_position)
:end
打个比方,好比现在这一幕剧情需要一群群众演员,导演的要求是这些群众演员随机出现即可,只需要演员各自扮演好即可。这里,有多少虫子类就添加多少个,这里就是蚂蚁和蜘蛛,把它们放在一个数组里,然后随机选取一个让虫子精灵表演,虫子精灵由于已经被我们优化为了只接受一个虫子对象,它内部会调用虫子的get函数获取资源信息进行初始化,也就是演员各自表演好自己的角色。 优化去掉了GameVIEw的onCreate函数中对资源的初始化过程:
-- create animation for BUGs
BUGType,153)">filename in pairs(GameVIEw.IMAGE_filenameS) -- load image
filename)
getPixelsHigh()

BUG_ANIMATION_TIMES[BUGType])
filename,128)">end
上面一段代码直接删除,还可以删除的部分:
BUGBase   = "..models.BUGBase")

GameVIEw.IMAGE_filenameS = {}
BUGBase.BUG_TYPE_ANT] = "BUGAnt.png"
BUG_TYPE_SPIDER] = "BUGSpIDer.png"

BUG_ANIMATION_TIMES = {}
0.15
0.1
经过以上梳理,现在的逻辑和流程清晰多了,下一节开始MOD成红警塔防游戏。 总结

以上是内存溢出为你收集整理的5、cocos2d-Lua的demo--虫子和虫子精灵全部内容,希望文章能够帮你解决5、cocos2d-Lua的demo--虫子和虫子精灵所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1063877.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存