2012-03-07 7 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

関連する問題