2013-11-04 79 views
1

我在屏幕上有兩個圖像重疊稱爲A和B.我有第三個圖像稱爲C,用戶可以在屏幕上移動。兩者中較大的一個(A)將C重置爲碰撞後的重生點。我需要知道如何讓C可以遍歷圖像B,而不是重置到重生點。如在圖像B中重疊圖像A並且否定碰撞功能。我如何讓一個物理體取消另一個物體的碰撞。

這裏是代碼:

--Hides status bar from top of page 
display.setStatusBar(display.HiddenStatusBar) 

--Set a test background image 
local backgroundimg = display.newImageRect("testbackground.png", 320,576) 
backgroundimg.x = display.contentWidth*0.5 
backgroundimg.y = display.contentHeight*0.5 

--Set the position and amount of score and lives 

local score = 0 
local lives = 5 

local showscore = display.newText("Score: "..score,0,-36,native.systemFont,25) 
showscore:setTextColor(0, 0, 0) 

local showlives = display.newText("Lives: "..lives,230,-36,native.systemFont,25) 
showlives:setTextColor(0, 0, 0) 

--Set physics for collisions, etc. 
physics = require("physics") 
physics.start() 
physics.setGravity(0,0) 

--set water 
local water = display.newImageRect("water.png",320,192) 
water.x = display.contentWidth*0.5 
water.y = 144 
physics.addBody(water,"static") 
water:addEventListener("collision", function()timer.performWithDelay(50,waterCollide)end) 

function waterCollide(event) 
    lives = lives - 1 
    display.remove(frog) 
    frog = display.newImageRect("FrogTest.png",32,48) 
    frog.x = display.contentWidth*0.5 
    frog.y = 504 
    physics.addBody(frog, "dynamic") 
    frog.isFixedRotation = true 
end 

--Sets buttons images and positions 
local forward = display.newImageRect("Forward Button.png",106,100) 
forward.x = 160 
forward.y = 478 

local left = display.newImageRect("Left Button.png",106,100) 
left.x = 53 
left.y = 478 

local right = display.newImageRect("Right Button.png",106,100) 
right.x = 267 
right.y = 478 

--Set log position and movement 
local log1 = display.newImageRect("log1.png", 96, 48) 
log1.x = 32 
log1.y = 226 
physics.addBody(log1,"kinematic") 
transition.to(log1, {time = 3500, x = 288}) 


--Set a frog sprite on the screen 
frog = display.newImageRect("FrogTest.png",32,48) 
frog.x = display.contentWidth*0.5 
frog.y = 504 
physics.addBody(frog, "dynamic",{density = 1.0, friction = 1, bounce = -1}) 
frog.isFixedRotation = true 

--Sets motion variables 
local motionX = 0 
local motionY = 0 
local speed = 4 

--Moving forward 
function forward:touch() 
    motionX = 0 
    motionY = -speed 
end 
forward:addEventListener("touch",forward) 

--Moving Right 
function right:touch() 
    motionX = speed 
    motionY = 0 
end 
right:addEventListener("touch",right) 

--Moving left 
function left:touch() 
    motionX = -speed 
    motionY = 0 
end 
left:addEventListener("touch",left) 

--Moves Frog each time frame is called 
function movefrog (event) 
    frog.x = frog.x + motionX 
    frog.y = frog.y + motionY 
end 
Runtime:addEventListener("enterFrame", movefrog) 

--Stops frog from moving continuously 
local function stop (event) 
    if event.phase == "ended" then 
     motionX = 0 
     motionY = 0 
    end 
end 
Runtime:addEventListener("touch", stop) 

--Making sure the frog does not go off the screen 
local function stopfrog (event) 
    if frog.x <= 16 then 
     frog.x = 16 
    end 
    if frog.x >= 304 then 
     frog.x = 304 
    end 
end 
Runtime:addEventListener("enterFrame", stopfrog) 

回答

1

你需要以否定重生設置waterCollide功能的一個條件。

取決於你有多複雜需要它,你可以簡單地檢查是否frog的立場是log1內的邊界,也許你可以有log1有自己的碰撞任何未來的日誌在碰撞時將標誌設置爲不觸發重生,然後在與任何日誌碰撞結束時清除標誌。

下面是一個例子:

local onLog = 0 

function frogDie() 
    lives = lives - 1 
    display.remove(frog) 
    frog = display.newImageRect("FrogTest.png",32,48) 
    frog.x = display.contentWidth*0.5 
    frog.y = 504 
    physics.addBody(frog, "dynamic") 
    frog.isFixedRotation = true 
end 

function waterCollide(event) 
    if onLog < 1 then frogDie() end 
end 

function logCollide(event) 
    if event.phase == 'began' then 
     onLog = onLog + 1 
    else 
     onLog = onLog - 1 
    end 
end 

log1:addEventListener("collision", logCollide) 
--log2:addEventListener("collision", logCollide) 

用一個數字來跟蹤青蛙是否是對數應該比一個布爾更安全,因爲日誌最終可能會重疊並清除標誌,才能正確復位在多次碰撞過程中。

相關問題