2016-03-25 14 views
0

ここに私のコードです。私は3レーンカーレースゲームを作っています。ここにスクリーンショットがあります。他の車のいずれかと赤い車の衝突で他のオブジェクトと接触することなく衝突が発生するのはなぜですか? (コロナSDKコンポーザー)

enter image description here

私は、画面のタップに私は戻ってgame.luaファイル(コードに来ているrestart.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

。、私は。グループを作成し、その内のすべての物理学の体を維持するために提案し、画面が非表示になります時に手動で削除してください。

関連する問題