如何创建一堆球并将其保存在lua和corona sdk的地面屏幕内?

时间:2018-04-10 19:36:21

标签: lua corona

如何创建一堆球并将其保存在lua和corona sdk的地面屏幕内?

现在它正从屏幕上掉下来并从屏幕上消失。

local physics = require("physics")
physics.start()

--local sky = display.newImage("sky.png")
--sky:scale( 5, 10 )
--sky.x = -100
--sky.y = -200

local field = display.newImage("field.png")
field:scale( 5, 10 )
field.x = 240
field.y = 470
local sky = display.newImage("sky.png")
sky:scale( 10, 3 )

physics.addBody(field,{friction = 0.5})
field.bodyType = "static"
local football = display.newImage("football.png")
football.x = 180
football.y = 80
football.rotation = 20
physics.addBody(football,{density = 2.0,friction = 0.5,bounce =0.5})

local function fallingball_field()
local football = display.newImage("football.png")
football.x = math.random(400)
football.y = -100
football.rotation = 20
physics.addBody(football, { density = 4.0,friction = 0.5, bounce = 0.5})
end
timer.performWithDelay(200, fallingball_field,200)

1 个答案:

答案 0 :(得分:0)

为了将您的物理对象放在屏幕中,您必须创建一个"边界框" ,以保持您的球在屏幕内。贝娄是如何创建边界的一个例子:

local physics = require("physics")
physics.start()
physics.setDrawMode("hybrid")


local myCircle = display.newCircle( 100, 100, 30 )
myCircle:setFillColor( 0.5 )

local leftWall = display.newRect(0,display.contentCenterY, 10, display.contentHeight)
local rightWall = display.newRect(display.contentWidth,display.contentCenterY, 10, display.contentHeight)
local bottomWall = display.newRect(display.contentCenterX, display.contentHeight - 25, display.contentWidth, 10)
local topWall = display.newRect(display.contentCenterX, 25, display.contentWidth, 10)

leftWall:setFillColor(nil)
rightWall:setFillColor(nil)
bottomWall:setFillColor(nil)
topWall:setFillColor(nil)

physics.addBody (leftWall, "static", { bounce = 0.1} )
physics.addBody (rightWall, "static", { bounce = 0.1} )
physics.addBody (bottomWall, "static", { bounce = 0.1} )
physics.addBody (topWall, "static", { bounce = 0.1} )
physics.addBody (myCircle, "dynamic", { bounce = 1.5} )

说明: 假设您的应用设置为 水平 "墙" 会保留您的物理对象,这将按原样运行。如果您的申请是 垂直 ,请与我们联系。

physics.setDrawMode(" hybrid") 将显示您设置的 EACH 对象的物理边界一个物理体。你可以更容易地看到你的物体如何相互作用。