游戏角色移出屏幕限制

时间:2012-03-07 09:02:00

标签: corona

在我的游戏中角色应该从左向右移动(使用加速度计),并且我已经设置了角色可以移动的限制(我认为)。然而,它发生了很多次,例如,当角色已经移动到它的右边界时,我尝试将手机向右倾斜一些,不知何故,角色可以移出允许的限制并在遗忘中丢失。这是我的代码 -

display.setStatusBar (display.HiddenStatusBar)
-- Hides the status bar

local physics = require ("physics")
physics.start()
physics.setGravity(0,0)
-- start physics engine and set the gravity (I'm using 0 to start, you might want to change this.)

background = display.newImage ("back.jpg")
local floor = display.newRect(320, 0, 1, 480)
local lWall = display.newRect(0, 480, 320, 1)
local rWall = display.newRect(0, -1, 320, 1)
local ceiling = display.newRect(-1, 0, 1, 480)

staticMaterial = {density=2, friction=.3, bounce=.4}
physics.addBody(floor, "static", staticMaterial)
physics.addBody(lWall, "static", staticMaterial)
physics.addBody(rWall, "static", staticMaterial)
physics.addBody(ceiling, "static", staticMaterial)
-- Sets the background

collector = display.newImage ("ms_throw.png")
collector.x = 10
collector.y = 10
physics.addBody(collector, {friction = 1.0, bounce=0.6})
-- Adds the collector and adds physics to the collector

-- Create the table to throw eggs
local vertPost = display.newRect(0, 400, 160, 10)
vertPost:setFillColor(33, 33, 33)
local horizPost = display.newRect(160, 400, 10, 155)
horizPost:setFillColor(33, 33, 33)
physics.addBody(vertPost, "static", staticMaterial)

-- Put a character on top of the table
thrower = display.newImage ("mr_throw.png")
thrower.x = 230
thrower.y = 460

local motionx = 0
local motiony = 0

local function onAccelerate( event )
motiony = 35 * event.yGravity
end
Runtime:addEventListener ("accelerometer", onAccelerate)

local function movecollector (event)
collector.x = collector.x + motionx
collector.y = collector.y - motiony
end
Runtime:addEventListener("enterFrame", movecollector)

1 个答案:

答案 0 :(得分:0)

在你的movecollector函数中,尝试使用body:applyForce( xForce, yForce, bodyX, bodyY )而不是更新x和y位置。