【唠叨】

当时学Lua的时候,我将《战神传说》用Lua也写了一遍。

C++版的《战神传说》参考这篇:http://shahdza.blog.51cto.com/2410787/1549660


【源码】

https://github.com/shahdza/Cocos-Lua_Plane




【学习心得】


1、继承自cc.Sprite后,设置自身纹理图片的方式

--【方式一】通过精灵帧设置cc.SpriteFrameCache:getInstance():addSpriteFrames("123.plist")localframe=cc.SpriteFrameCache:getInstance():getSpriteFrame("1.png")self:setSpriteFrame(frame)--【方式二】通过图片纹理localtexture=cc.Director:getInstance():getTextureCache():addImage("ship01.png")localframe=cc.SpriteFrame:createWithTexture(texture,cc.rect(0,0,60,38))self:setSpriteFrame(frame)


2、混合模式

--localcbl={GL_SRC_ALPHA,GL_ONE}self:setBlendFunc(GL_SRC_ALPHA,GL_ONE)


3、定时器

--【默认定时器】self:scheduleUpdateWithPriorityLua(update,priority)self:unscheduleUpdate()--【自定义定时器】schedulelocaldt=0functionGameLayer:addUpdate()localfunctionupdate(_dt)dt=_dtendself:scheduleUpdateWithPriorityLua(update,0)endfunctionGameLayer:xxxxx()localfunctionfunc()print(dt)endschedule(self,func,1.0/60.0)end


4、按钮回调

--registerScriptTapHandlerlocalfunctionturnToLoadingScene(tag,sender)self:turnToLoadingScene()endlocalbacklb=cc.Label:createWithBMFont("Font/bitmapFontTest.fnt","GoBack")localpback=cc.MenuItemLabel:create(backlb)pback:registerScriptTapHandler(turnToLoadingScene)


5、触摸事件

--registerScriptHandler--【单点触摸】localdispatcher=self:getEventDispatcher()locallistener=cc.EventListenerTouchOneByOne:create()listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN)listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED)listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED)dispatcher:addEventListenerWithSceneGraphPriority(listener,self)--【吞噬触摸】listener:setSwallowTouches(true)


6、单例类

Effect=class("Effect",function()returncc.Node:create()end)local_effect=nilfunctionEffect:getInstance()ifnil==_effectthen_effect=clone(Effect)--用clone(),好像用.new()有问题_effect:init()endreturn_effectend


7、贝塞尔曲线运动

localbezier={cc.p(sgn*dx,0),--controlPoint_1cc.p(sgn*dx,-dy),--controlPoint_1cc.p(0,-dy),--bezier.endPosition}localb0=cc.BezierBy:create(dt,bezier)


8、动画

--【方式一】通过精灵帧创建localarr={}fori=1,34dolocalstr=string.format("explosion_%02d.png",i)--getSpriteFrameByName->getSpriteFramelocalframe=cc.SpriteFrameCache:getInstance():getSpriteFrame(str)table.insert(arr,frame)endlocalanimation=cc.Animation:createWithSpriteFrames(arr,0.02)cc.AnimationCache:getInstance():addAnimation(animation,"Explosion")--【方式二】通过图片纹理localtexture=cc.Director:getInstance():getTextureCache():addImage("ship01.png")localsp1=cc.SpriteFrame:createWithTexture(texture,cc.rect(0,0,60,38))localsp2=cc.SpriteFrame:createWithTexture(texture,cc.rect(60,0,60,38))localanimation=cc.Animation:create()an:addSpriteFrame(sp1)an:addSpriteFrame(sp2)an:setDelayPerUnit(0.1)an:setRestoreOriginalFrame(true)self:runAction(cc.RepeatForever:create(cc.Animate:create(animation)))


9、背景滚动

--【1】添加背景functionGameLayer:addBG()self.bg1=cc.Sprite:create("bg01.jpg")self.bg2=cc.Sprite:create("bg01.jpg")self.bg1:setAnchorPoint(cc.p(0,0))self.bg2:setAnchorPoint(cc.p(0,0))self.bg1:setPosition(0,0)self.bg2:setPosition(0,self.bg1:getContentSize().height)self:addChild(self.bg1,-10)self:addChild(self.bg2,-10)end--【2】背景滚动functionGameLayer:moveBG()localheight=self.bg1:getContentSize().heightlocalfunctionupdateBG()self.bg1:setPositionY(self.bg1:getPositionY()-1)self.bg2:setPositionY(self.bg1:getPositionY()+height)ifself.bg1:getPositionY()<=-heightthenself.bg1,self.bg2=self.bg2,self.bg1self.bg2:setPositionY(WIN_SIZE.height)endendschedule(self,updateBG,0)end


10、物理碰撞

>碰撞事件 & 条件

(1)b1.CategoryBitmask 与 b2.ContactTestBitmask 进行按位与

(2)b2.CategoryBitmask 与 b1.ContactTestBitmask 进行按位与

只有(1)、(2)都不为0的时候,触发碰撞检测事件。

>物体碰撞 & 条件

(1)b1.CategoryBitmask 与 b2.CollisionBitmask 进行按位与

(2)b2.CategoryBitmask 与 b1.CollisionBitmask 进行按位与

只有(1)、(2)都不为0的时候,触发物体碰撞。

--设置场景物理信息scene:getPhysicsWorld():setGravity(cc.p(0,0))scene:getPhysicsWorld():setDebugDrawMask(cc.PhysicsWorld.DEBUGDRAW_ALL)--创建物体b1=cc.Sprite:create("123.png")--半径、材质(密度、弹性、摩擦力)、偏移量localbody=cc.PhysicsBody:createCircle(3,cc.PhysicsMaterial(0.1,1,0),cc.p(0,16))b1:setPhysicsBody(body)b1:getPhysicsBody():setCategoryBitmask(2)b1:getPhysicsBody():setCollisionBitmask(3)b1:getPhysicsBody():setContactTestBitmask(12)--注册碰撞事件localfunctiononContactBegin(contact)locala=contact:getShapeA():getBody():getNode()localb=contact:getShapeB():getBody():getNode()ifa~=nilandb~=nilthena:setPosition(0,0)b:setPosition(0,0)endreturntrueendlocaldispatcher=self:getEventDispatcher()localcontactListener=cc.EventListenerPhysicsContact:create()contactListener:registerScriptHandler(onContactBegin,cc.Handler.EVENT_PHYSICS_CONTACT_BEGIN)dispatcher:addEventListenerWithSceneGraphPriority(contactListener,self)


11、程序结束

--【方式一】cc.Director:getInstance():endToLua()--【方式二】os.exit(0)


12、问题集

--【敌人管理器】--不加入层中,好像就调用不了enemyManager的方法self.enemyManager=EnemyManager:create(self)self:addChild(self.enemyManager)