2016-08-06 1 views
0

コロナを使用してコーディングを練習する2dゲームを作成していて、問題が残っています。ゲーム中の敵の背中は、凹凸のある滑り台(一種)のような形をしています。私のキャラクターが背中の敵に当ると、スクリーンから滑り落ちます。敵がヒットすると、ゲームは別の画面に切り替わり、元に戻ります。私のキャラクターは、位置が変わっても画面から外れ、理由は分かりません

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

これは何が起こっているかを示すビデオで、約40または45秒の時点であなたは文字が滑り始める表示されます。

これを修正するために複数のことを試しましたが、敵とプレーヤーのバウンス係数は両方ともゼロに設定されています。私はプレーヤーの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

関連する問題