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

How to create a heap of balls and keep it within the screen on ground in lua and corona sdk?

如何在 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)

为了将您的物理对象放在屏幕内,您必须创建一个 "Boundary box" 来让您的球保持在屏幕内。下面是如何创建边界的示例:

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} )

说明: "walls" 假设您的应用设置为 horizo​​ntal 将使您的物理对象保持原位] 这将按原样工作。如果您的应用程序是 vertical.

,请告诉我

physics.setDrawMode("hybrid") 将显示 EACH 的物理边界 您使用物理 body.It 设置的对象将更容易看到您的对象如何相互作用。