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

为了将物理对象放在屏幕中,您必须创建一个"边界框">,将球保持在屏幕内。波纹管是一个关于如何创建边界的示例:

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("混合")将显示您使用物理体设置的每个对象的物理边界。将更容易看到对象如何相互交互。

最新更新