Face coronasdk physics engine

Development on mobile phone,Currently popular on the network mainly two development software,Is a well-known unity (after all, the year 4399 above the game online edition of Minecraft developed by it,3d engine without any software beyond it),Another is the relatively small minority of the cocos。cocos occupy a major position 2d game,And unity has occupied a major position 3d game。And we are talking about the same as coronasdk and cocos 2d game engine (coronasdk not been known before 2010 mainly due to charges,Now, even the unity it does not charge fees,The scared off many developers。So better cocos high visibility),The cocos current official language is recommended js,I've read a book on the best java called "learning java from entry to give up.",The coronasdk is the use of interpreted languages ​​lua (In fact, before cocos support lua,But that company the project team to cut lua,Very, very sorry)。so,corona like to write more。

Any game will have a physics engine,It is better written than the animation。It can control what the main property:density,Coefficient of friction,Bounce coefficient,Whether under the influence of gravity, etc.。Its main command:

local physics = require "physics" - introducing physics engine

physics.start() - Start physics engine

physics.stop() - Close the physics engine

physics.addBody() - Add physical objects

All right.,We now look forward to a bug below

This is a normal situation,Box free fall,everything is normal

 

See "iang" section below,A mysterious object in the bottom of the box,The key is also the engine (the code when I hit a person at night,That haunted,Scared me)。

This is the beginning interface,There have been a small box fell from the sky,A drop interval is 300 milliseconds,After tests found,A quick click on click on the screen you can avoid this bug。The more time testing again found a small box landing,big boxWillThe higher the mat。on the other hand,Is this little box to the ghost。

So now there are two paths to take a,One is to remove all produce all the little box at the time of the conversion page,The second is to allow a small box to delete all (say feeling a thing)。So strand clear idea easier to handle。

The main code is as follows

main = function()

time1 = timer.performWithDelay(300,addcrate,0)

end

addcrate = function()

math.randomseed(os.time())

local seed = math.random(1,5)

choice = math.ceil(seed)

if choice == 1 then

crate = display.newImageRect(“crate1.png”,30,30)

crate.x = 120

crate.y = 5

crate.rotation = 15

physics.addBody(crate,{density = 1,friction = 0.3,bounce = 0})

elseif choice == 2 then

crate = display.newImageRect(“crate1.png”,30,30)

crate.x = 150

crate.y = 5

crate.rotation = 15

physics.addBody(crate,{density = 1,friction = 0.3,bounce = 0})

elseif choice == 3 then

crate = display.newImageRect(“crate1.png”,30,30)

crate.x = 180

crate.y = 5

crate.rotation = 15

physics.addBody(crate,{density = 1,friction = 0.3,bounce = 0})

elseif choice == 4 then

crate = display.newImageRect(“crate1.png”,30,30)

crate.x = 210

crate.y = 5

crate.rotation = 15

physics.addBody(crate,{density = 1,friction = 0.3,bounce = 0})

elseif choice == 5 then

crate = display.newImageRect(“crate1.png”,30,30)

crate.x = 240

crate.y = 5

crate.rotation = 15

physics.addBody(crate,{density = 1,friction = 0.3,bounce = 0})

end

a = a + 1

end

timer.performWithDelay(10,removeCrate,on one)

After the corona will error

“crate” is a nil value

no way,To find another way。Re-think what we just did it again:Start the physics engine,Add a physical object。All come with physical word,Meaning that we can achieve this pile of stuff disappear through the physics engine。So get everything。

function scene:exitScene()

–To do something to write here

audio.stop()

audio.dispose(backgroundMusic)

backgroundMusic = nil

background:removeSelf()

background = nil

crate1:removeSelf()

physics.stop()

local result = timer.pause(time1)

end

The following big box removed,Then close the physics engine,Small box will quickly drop out。(Normal, then a small box y coordinates are unlimited low,But the corona waste recovery will take them recover,That's why no matter how much you produce animation box will not Caton)

Original article written by LogStudio:R0uter's Blog » Face coronasdk physics engine

Reproduced Please keep the source and description link:https://www.logcg.com/archives/2846.html

About the Author

Yao Yao

Leave a Reply

Your email address will not be published. Required fields are marked *