2016-03-25 30 views
0

這是我的代碼。我在做一個3車道的賽車遊戲。這是屏幕截圖。爲什麼發生碰撞時與其他物體沒有任何接觸? (Corona SDK作曲家)

enter image description here

與任何其他的車我要到restart.lua文件的紅旗轎車相撞,其中在我回來的game.lua文件屏幕的龍頭(代碼如下所示)。當控制權從restart.lua移回game.lua時,會出現問題。

會發生什麼事情是碰撞事件發生的原因沒有任何原因造成遊戲連續故障和重新啓動和game.lua之間轉移。有誰知道爲什麼會發生這種情況,以及如何停止?

local composer = require("composer") 


local scene = composer.newScene() 
local pix=0 
local no=0 
local physics=require "physics" 
physics.start() 
local x=2000 
local prevP=0 
local prevS=5 
local count=1 
-- ----------------------------------------------------------------------------------------------------------------- 
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called 
-- ----------------------------------------------------------------------------------------------------------------- 

-- Local forward references should go here 

-- ------------------------------------------------------------------------------- 

local opp1 
local opp 
local sceneGroup 
local roadCopy 
local road 
local car 

function removeAllListeners(self) 
    self._functionListeners = nil 
    self._tableListeners = nil 
end 



local function touchScreen(event) 
    print("touchScreen") 

    if event.x < dw/2 then 

     if car.current==car.posB then 
      car.x=car.posA 
      car.current=car.posA 
     elseif car.current == car.posC then 
      car.x=car.posB 
      car.current=car.posB  
     end 
    else 

     if car.current==car.posB then 
      car.x=car.posC 
      car.current=car.posC 
     elseif car.current == car.posA then 
      car.x=car.posB 
      car.current=car.posB  
     end 

    end 
end 

local function removeListeners() 
    removeAllListeners(road) 
    removeAllListeners(roadCopy) 
    removeAllListeners(opp) 
    removeAllListeners(car) 
    road:removeEventListener("tap",touchScreen) 
    roadCopy:removeEventListener("tap",touchScreen) 
    car:removeEventListener("collision",onLocalCollision) 
    composer.removeScene("game") 
    composer.gotoScene("restart") 
end 
function onLocalCollision(event) 
    print("collision") 
    removeListeners() 
end 


local function moveRoad(self,event) 


    if self.y > dh-20 then 
     self.y=20 
     pix=pix+10 
     if pix > 100 then 
      road.speed=road.speed+1 
      roadCopy.speed=roadCopy.speed+1 
      pix=0 
     else 

     end 
    else 
     self.y=self.y+self.speed 
     pix=pix+1 
     if pix > 100 then 
      road.speed=road.speed+1 
      roadCopy.speed=roadCopy.speed+1 
      pix=0 
     else 

     end 
    end 
    if road.speed>30 or roadCopy.speed>30 then 
     road.speed=30 
     roadCopy.speed=30 
    end 

end 

local function moveoppcar(self, event) 
    --to move the incoming cars 
    if(self.y==nil)then 
     return 
    end 
    if(self.y>dh) then 
     Runtime:removeEventListener("enterFrame", self) 
     self:removeSelf() 
     self=nil 
     randomobject1() 

    else 

     self.y=self.y+self.speed+roadCopy.speed 
    end 
end 

function randomobject1(event) 
    --to randomly generate the incoming cars 
    math.randomseed(os.time()) 
    opp=display.newRect(center-dw/4.75, 30, dw/8, dw/4.5) 

    local position = math.random(1, 3) 
    local cartype = math.random(1, 3) 

    if(position==prevP) then 
     position=(prevP+2)%3 
    end 

    if position==1 then 
     opp.x=center-dw/4.75 
    end 
    if position == 2 then 
     opp.x=center 
    end 
    if position == 3 then 
     opp.x=center+dw/4.75 
    end 
    if cartype == 1 then 
     opp.fill={type="image", filename="car_green_1.png"} 
    end 
    if cartype == 2 then 
     opp.fill={type="image", filename="car_blue_1.png"} 
    end 
    if cartype == 3 then 
     opp.fill={type="image", filename="car_red_1.png"} 
    end 
    opp.speed=math.random(1,10) 

    sceneGroup:insert(opp) 
    prevP=position 
    prevS=opp.speed 
    local r=opp.width 
    physics.addBody(opp,"static",{density=1, bounce=0.1, friction=0.2,radius=5}) 
    opp.gravityScale=0 
    opp.enterFrame=moveoppcar 
    Runtime:addEventListener("enterFrame", opp) 
end 





-- "scene:create()" 
function scene:create(event) 
    print("Scene:create") 
    sceneGroup = self.view 


    ---------ROAD--------- 
    road=display.newRect(0, 0, dw, dh) 
    road.fill={type="image",filename="road.png"} 
    road.anchorX,road.anchorY=0,0 
    road.contentHeight=dh 
    sceneGroup:insert(road) 
    road.speed=5 

    roadCopy=display.newRect(0, 0, dw, dh) 
    roadCopy.fill={type="image",filename="road.png"} 
    roadCopy.anchorX,roadCopy.anchorY=0,1 
    roadCopy.contentHeight=dh 
    sceneGroup:insert(roadCopy) 
    roadCopy.speed=5 


    ----PLAYER CAR-------- 
    car=display.newRect(dw/2, dh-10, dw/8, dw/4.5) 
    car.fill={type="image", filename="player.png"} 
    car.anchorY=1 
    center=dw/2 
    car.posA=center-dw/4.75 
    car.posB=center 
    car.posC=center+dw/4.75 
    car.current=car.posB 
    sceneGroup:insert(car) 
    physics.addBody(car,"dynamic",{density=1, bounce=0.1, friction=0.2}) 
    car.gravityScale=0 
    --[[randomobject1() 
    randomobject1()]] 
end 



-- "scene:show()" 
function scene:show(event) 

    if(event.phase=="did") then 
     print("Scene:show") 

     addList() 
     randomobject1() 
     randomobject1() 
    end 
end 


function addList() 
    --adding event listeners 
    car.collision = onLocalCollision 
    car:addEventListener("collision", car) 
    road.enterFrame=moveRoad 
    Runtime:addEventListener("enterFrame", road) 
    roadCopy.enterFrame=moveRoad 
    Runtime:addEventListener("enterFrame", roadCopy) 
    road:addEventListener("tap",touchScreen) 
    roadCopy:addEventListener("tap",touchScreen) 
end 



-- "scene:hide()" 
function scene:hide(event) 
    print("Scene:hide") 
    local sceneGroup = self.view 
    local phase = event.phase 

    if (phase == "will") then 
     -- Called when the scene is on screen (but is about to go off screen) 
     -- Insert code here to "pause" the scene 
     -- Example: stop timers, stop animation, stop audio, etc. 
    elseif (phase == "did") then 
     -- Called immediately after scene goes off screen 
    end 
end 


-- "scene:destroy()" 
function scene:destroy(event) 
    print("Scene:destroy") 
    local sceneGroup = self.view 
    -- Called prior to the removal of scene's view 
    -- Insert code here to clean up the scene 
    -- Example: remove display objects, save state, etc. 
end 







-- ------------------------------------------------------------------------------- 

-- Listener setup 
scene:addEventListener("create", scene) 
scene:addEventListener("show", scene) 
scene:addEventListener("hide", scene) 
scene:addEventListener("destroy", scene) 


-- ------------------------------------------------------------------------------- 

return scene 

回答

0

你必須手動刪除物理體,我建議創建一個組並保留所有物理體,並在屏幕隱藏時手動刪除它。