0
在我的遊戲角色應該從左向右移動(使用加速度計),並且我已經設置了角色可以做出的移動限制(我認爲)。然而,它發生了很多次,當角色移動到正確的極限時,我嘗試了更多的角度傾斜手機,不知何故角色可以移出允許的限制,並遺忘在遺忘中。這裏是我的代碼 -遊戲角色移出屏幕限制
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)
什麼應該是xForce,yForce,bodyX,bodyY的價值? – imin 2012-03-09 04:31:32
xForce和yForce將會改變你的motionx/y屬性。 bodyX和bodyY將是「收集器」對象(collector.x,collector.y)的x/y屬性 – Corey 2012-03-09 15:28:55