2012-03-07 4 views
0

내 캐릭터가 왼쪽에서 오른쪽으로 (가속도계 사용) 움직여야하고, 캐릭터가 움직일 수있는 한계를 설정했습니다 (생각합니다). 그러나 캐릭터가 예를 들어 옳은 한계로 옮겨 졌을 때 꽤 많은 시간이 걸리고, 전화기를 좀더 기울여 보았습니다. 어쨌든 캐릭터는 허용 된 한도를 벗어나 망각에서 빠져 나옵니다. 여기에 내 코드입니다 - 당신의 movecollector 기능에서게임 캐릭터가 화면 한계에서 벗어납니다.

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) 

답변

0

, body:applyForce(xForce, yForce, bodyX, bodyY)를 사용하는 대신 x와 y 위치를 업데이트하려고합니다.

+0

xForce, yForce, bodyX, bodyY의 값은 무엇이되어야합니까? – imin

+0

xForce 및 yForce는 사용자의 motionx/y 속성의 변형입니다. bodyX 및 bodyY는 "collector"객체 (collector.x, collector.y)의 x/y 속성입니다. – Corey

관련 문제