2016-08-06 4 views
0

코로나를 사용하여 2 차원 게임을 만들어 코딩을 연습하고 있는데 문제가 있습니다. 게임에서 적들 중 하나의 뒷면은 울퉁불퉁 한 슬라이드 (일종의) 모양입니다. 내 캐릭터가 적을 때리면 스크린 밖으로 미끄러 져 나오기 시작합니다. 적을 명중하면 게임이 다른 화면으로 전환되고 다시 스위치됩니다. 내 캐릭터가 위치가 변경 되더라도 화면에서 벗어나서 왜 이유가 확실하지 않습니다

https://www.youtube.com/watch?v=xGoZ1jEG0YA&feature=youtu.be

은 약 40 45초 당신이 문자가 슬라이드 시작 볼 수에서 무슨 일이 일어나고 있는지 보여주는 동영상입니다.

이 문제를 해결하기 위해 여러 가지 시도를 해봤습니다. 적과 플레이어의 반송 률이 모두 0으로 설정되었습니다. 플레이어의 x 좌표의 linearVelocity를 0으로 설정하려고 시도했지만 작동하지 않았습니다. 이것이 내 충돌 탐지 코드입니다.

function spawnEnemies() 
     temp = math.random(1, 4) 
      enemy = display.newSprite(beetle, beetleSequenceData) 
      enemy.x = _R + 100 
      enemy.y = _CY 
      enemy.hasBeenScored = false 
      physics.addBody(enemy, "dynamic", physicsData:get("beetle")) 
      enemy.xScale = -1 
      enemy.id = "enemy" 
      enemy.isFixedRotation = true 
      enemy:play() 
      group:insert(enemy) 

      enemy2 = display.newSprite(vulture, vultureSequenceData) 
      enemy2.x = _R + 100 
      enemy2.y = _CY - (enemy2.height * 0.25) 
      enemy2.hasBeenScored = false 
      physics.addBody(enemy2, "dynamic", physicsData:get("vulture")) 
      enemy2.xScale = -1 
      enemy2.gravityScale = -0.01 
      enemy2.id = "enemy2" 
      enemy2.isFixedRotation = true 
      enemy2:play() 
      specialGroup:insert(enemy2) 

      enemy3 = display.newSprite(scorpion, scorpionSequenceData) 
      enemy3.x = _R + 100 
      enemy3.y = _CY 
      enemy3.hasBeenScored = false 
      physics.addBody(enemy3, "dynamic", physicsData:get("scorpion")) 
      enemy3.xScale = -1 
      enemy3.id = "enemy3" 
      enemy3.isFixedRotation = true 
      enemy3:play() 
      group:insert(enemy3) 

      enemy4 = display.newSprite(bee, beeSequenceData) 
      enemy4.x = _R + 100 
      enemy4.y = _CY - (enemy4.height * 0.25) 
      enemy4.hasBeenScored = false 
      physics.addBody(enemy4, "dynamic", floatingEnemies:get("bee")) 
      enemy4.xScale = -1 
      enemy4.gravityScale = -0.01 
      enemy4.id = "enemy4" 
      enemy4.isFixedRotation = true 
      enemy4:play() 
      specialGroup:insert(enemy4) 

      if temp == 1 then 
       enemy2:removeSelf() 
       enemy3:removeSelf() 
       enemy4:removeSelf() 
      elseif temp == 2 then 
       enemy:removeSelf() 
       enemy3:removeSelf() 
       enemy4:removeSelf() 
      elseif temp == 3 then 
       enemy:removeSelf() 
       enemy2:removeSelf() 
       enemy4:removeSelf() 
      else 
       enemy:removeSelf() 
       enemy2:removeSelf() 
       enemy3:removeSelf() 
      end 
    end 

    function moveEnemies() 
     for a = group.numChildren, 1, -1 do 
      if group[a].x < 100 then 
       if group[a].hasBeenScored == false then 
        updateScore() 
        group[a].hasBeenScored = true 
       end 
      end 
      if group[a].x > _L - 100 then 
       group[a].x = group[a].x - 8 
      else 
       group:remove(group[a]) 
      end 
     end 
     for b = specialGroup.numChildren, 1, -1 do 
      if specialGroup[b].x < 100 then 
       if specialGroup[b].hasBeenScored == false then 
        specialUpdateScore() 
        specialGroup[b].hasBeenScored = true 
       end 
      end 
      if specialGroup[b].x > _L - 100 then 
       specialGroup[b].x = specialGroup[b].x - 8 
      else 
       specialGroup:remove(specialGroup[b]) 
      end 
     end 
    end 

function onCollision(event) 
     local function removeOnPlayerHit(obj1, obj2) 
      if(obj1 ~= nil and obj1.id == "enemy") then 
       display.remove(obj1) 
      end 
      if(obj2 ~= nil and obj2.id == "enemy") then 
       display.remove(obj2) 
      end 
      if(obj1 ~= nil and obj1.id == "enemy2") then 
       display.remove(obj1) 
      end 
      if(obj2 ~= nil and obj2.id == "enemy2") then 
       display.remove(obj2) 
      end 
      if(obj1 ~= nil and obj1.id == "enemy3") then 
       display.remove(obj1) 
      end 
      if(obj2 ~= nil and obj2.id == "enemy3") then 
       display.remove(obj2) 
      end 
      if(obj1 ~= nil and obj1.id == "enemy4") then 
       display.remove(obj1) 
      end 
      if(obj2 ~= nil and obj2.id == "enemy4") then 
       display.remove(obj2) 
      end 
     end 

     local function showPlayerHit() 
      local tmr_onPlayerHit = timer.performWithDelay(1, playerHit, 1) 
     end 

     if event.phase == "began" then 
      if((event.object1.id == "enemy" or event.object1.id == "enemy2" or event.object1.id == "enemy3" or event.object1.id == "enemy4") and event.object2.id == "character") then 
       showPlayerHit() 
       removeOnPlayerHit(event.object1, nil) 
      elseif(event.object1.id == "character" and (event.object2.id == "enemy" or event.object2.id == "enemy2" or event.object2.id == "enemy3" or event.object2.id == "enemy4")) then 
       showPlayerHit() 
       removeOnPlayerHit(nil, event.object2) 
      end 
     end 
    end 

답변

0

난 그냥에 대한 코드를 단순화하면

-- utility function 
function isInArray(a, s, t) 
    local g = {} 
    for k, v in pairs(a) do 
     if t then 
      if v ~= s then 
       table.insert(g, v) 
      end 
     else 
      if v == s then 
       return true 
      end 
     end 
    end 
    return t and (#g > 0 and g or false) or false 
end 

-- this will hold all the enemies names 
local enemies = {} 
-- this function creates all the enemies 
function createEnemies(name, number) 
    for i = 1, number do 
     table.insert(enemies, i, name..i) 
    end 
end 

-- now lets create the enemies, 4 enemies in total with the name enemy+n 
-- e.g. enemy1, enemy2 etc.. for value of number 
createEnemies('enemy', 4) 


function onCollision(event) 
    local function removeOnPlayerHit(obj1, obj2) 
     -- see if either of the arguments are nil 
     local objects = isInArray({obj1, obj2}, nil, true) 
     -- now check if objects contains any of the objects 
     if next(objects) then 
      -- if it does cycle through them 
      for i = 1, #objects do 
       -- now check against all the enemies 
       if isInArray(enemies, objects[i].id) then 
        -- now remove the objects 
        display.remove(objecta[i]) 
       end 
      end 
     end 
    end 

    local function showPlayerHit() 
     local tmr_onPlayerHit = timer.performWithDelay(1, playerHit, 1) 
    end 
    if event.phase == "began" then 
     if isInArray(enemies, event.object1.id) and event.object2.id == "character" then 
      showPlayerHit() 
      removeOnPlayerHit(event.object1, nil) 
     elseif isInArray(enemies, event.object2.id) and event.object1.id == "character" then 
      showPlayerHit() 
      removeOnPlayerHit(nil, event.object2) 
     end 
    end 
end 
+0

좀 더 단순화 할 수있다 썼다 다시 위의 스크립트는 그러나 나는 당신이 촬영 된 로직을 변경하지 않았다 : P –

+0

감사를 너는 그렇지만이 충돌 문제를 도와 줄 수 있다고 생각하니? – alexjr

+0

적을 스폰하는 방법을 보여주기 위해 코드를 업데이트했습니다. 객체가 x 좌표에 도달하면 사용자에게 점수를주는 점수 시스템이 있습니다. 이것이 나의 필요에 맞게 간소화 될 수 있습니까? – alexjr