Nagging
When I was learning Lua, I wrote the Legend of Mars in Lua.
C + + version of "The Legend of the Gods" reference this article: http://shahdza.blog.51cto.com/2410787/1549660
Source
Https://github.com/shahdza/Cocos-Lua_Plane
"Learning experience"
1, Inherit from CC. How to set your own texture picture after a sprite
--"mode One" is set by the sprite frame cc. Spriteframecache:getinstance (): Addspriteframes ("123.plist") Local frame = cc. Spriteframecache:getinstance (): Getspriteframe ("1.png") Self:setspriteframe (frame)--"way two" through the picture texture local texture = cc. Director:getinstance (): Gettexturecache (): AddImage ("Ship01.png") Local frame = cc. Spriteframe:createwithtexture (texture, cc.rect (0, 0, max)) Self:setspriteframe (frame)
2. Mixed mode
--Local CBL = {gl_src_alpha, Gl_one}self:setblendfunc (Gl_src_alpha, Gl_one)
3. Timer
--"Default Timer" Self:scheduleupdatewithprioritylua (update, priority) Self:unscheduleupdate ()--"Custom Timer" schedulelocal dt = 0function gamelayer:addupdate () Local function update (_DT) dt = _dt Endself:scheduleupdatewithprioritylua (update, 0) Endfunction gamelayer:xxxxx () Local function func () print (DT) endschedule (self, func, 1.0/60.0) end
4. Button callback
--registerscripttaphandlerlocal function turntoloadingscene (tag, sender) Self:turntoloadingscene () endlocal backlb = Cc. Label:createwithbmfont ("Font/bitmapfonttest.fnt", "Go back") Local pback = cc. Menuitemlabel:create (BACKLB) Pback:registerscripttaphandler (Turntoloadingscene)
5. Touch Events
--registerscripthandler--"single Touch" local dispatcher = Self:geteventdispatcher () Local listener = 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)--"engulfing touch" listener: Setswallowtouches (True)
6. Single Case class
Effect = Class ("Effect", function () return CC. Node:create () end) Local _effect = Nilfunction effect:getinstance () if nil = = _effect Then_effect = Clone (effect)--with clone () , it seems that there is a problem with. New () _effect:init () Endreturn _effectend
7. Bezier Curve Motion
Local Bézier = {CC.P (SGN * dx, 0),--controlpoint_1cc.p (SGN * dx,-dy),--controlpoint_1cc.p (0,-dy),--bezier.endposition }local B0 = cc. Bezierby:create (DT, Bézier)
8. Animation
-- "Way One" creates local arr = {}for i=1, 34 dolocal str = through Sprite frames String.Format ("Explosion_%02d.png" , i)-- getspriteframebyname -> GETSPRITEFRAMELOCAL FRAME = CC. Spriteframecache:getinstance (): Getspriteframe (str) table.insert (arr, frame) endlocal animation =  CC. Animation:createwithspriteframes (arr, 0.02) cc. Animationcache:getinstance (): Addanimation (animation, "Explosion")-- "way two" through the picture texture local texture  = CC. Director:getinstance (): Gettexturecache (): AddImage ("Ship01.png") local sp1 = cc. Spriteframe:createwithtexture (Texture, cc.rect (0, 0, 60, 38)) local sp2 = cc . Spriteframe:createwithtexture (Texture, cc.rect (60, 0, 60, 38)) local animation =  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. Background scrolling
--"1" Add background function gamelayer: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" background scrolling function gamelayer:movebg () Local height = self.bg1:getContentSize (). heightlocal function Updatebg () Self.bg1:setPositionY (Self.bg1:getPositionY ()-1) Self.bg2:setPositionY (Self.bg1:getPositionY ( ) + height) if Self.bg1:getPositionY () <=-height thenself.bg1, self.bg2 = Self.bg2, Self.bg1self.bg2:setPositionY (WIN _size.height) Endendschedule (self, UPDATEBG, 0) end
10. Physical collisions
> Collision Events & Conditions
(1) B1. categorybitmask and B2. Contacttestbitmask for bitwise AND
(2) B2. Categorybitmask and B1. Contacttestbitmask for bitwise AND
The collision detection event is triggered only when (1), (2) are not 0.
> Object Collision & Condition
(1) B1. Categorybitmask and B2. Collisionbitmask for bitwise AND
(2) B2. Categorybitmask and B1. Collisionbitmask for bitwise AND
only when (1), (2) are not 0, trigger the object to collide.
-- Set Scene physical information Scene:getphysicsworld (): Setgravity (CC.P (0, 0)) Scene:getphysicsworld (): Setdebugdrawmask (cc . Physicsworld.debugdraw_all)-- Create object b1 = cc. Sprite:create ("123.png")-- radius, material (density, elasticity, friction), offset local body = cc. Physicsbody:createcircle (3,&NBSP;CC. Physicsmaterial (0.1, 1, 0), &NBSP;CC.P (0, 16)) b1:setphysicsbody (body) b1:getPhysicsBody (): Setcategorybitmask (2) b1:getphysicsbody (): Setcollisionbitmask (3) b1:getphysicsbody (): Setcontacttestbitmask (12)-- Register Collision Event Local function oncontactbegin (Contact) Local a = contact:getshapea (): GetBody (): GetNode () Local b = contact:getshapeb (): GetBody (): GetNode () If a ~= nil and b ~= nil thena:setposition (0, 0) b:setposition (0, 0) endreturn Trueendlocal dispatcher = self:geteventdispatcher () local contactlistener = cc. Eventlistenerphysicscontact:create () Contactlistener:registerscriptHandler (ONCONTACTBEGIN,&NBSP;CC. Handler.event_physics_contact_begin) dispatcher:addeventlistenerwithscenegraphpriority (contactListener, Self
11. End of Program
--"Way One" CC. Director:getinstance (): Endtolua ()--"Way Two" os.exit (0)
12. Problem Set
--"enemy manager"--does not join the layer, as if the method of Enemymanager can not be called Self.enemymanager = Enemymanager:create (self) self:addchild ( Self.enemymanager)
This article is from the "Summer Wind" blog, please be sure to keep this source http://shahdza.blog.51cto.com/2410787/1622187
Cocos actual combat [3.2]--, The Legend of the Gods, Lua edition