2014-02-12 157 views
-1

我有一個程序,玩家會躲避墜落的小行星,小行星將不得不穿過地面,並在沒有結束遊戲的情況下擊中對方。但是,如果一個asateroid擊中一個玩家,遊戲將需要結束並在屏幕上顯示遊戲。我如何區分聯繫人?LUA碰撞,多個物體

這裏是我到目前爲止的代碼:

--Start the physics engine! 
local physics = require ("physics") 

--Get a background image! 
local background = display.newImage("Images/Background.png") 

-- Hide status bar 
display.setStatusBar(display.HiddenStatusBar); 

--Is this guy even alive? 
local PlayerAlive = true; 

--What size is our screen? 
_H = display.contentHeight; 
_W = display.contentWidth; 


--Play some tunes! 
local music = audio.loadStream("Sounds/bf3.mp3"); 

--World is flat and small! 
local leftWall = display.newRect (0, 0, 1, display.contentHeight); 
local rightWall = display.newRect (display.contentWidth, 0, 1, display.contentHeight); 
physics.addBody (leftWall, "static", { bounce = 0.1 }); 
physics.addBody (rightWall, "static", { bounce = 0.1 }); 
leftWall.myName = "Left wall" 
rightWall.myName = "Right wall" 


--Discover Gravity! 
physics.setGravity(0, 1) 

--5 Random numbers! 
local Rock1Y = Math.Random(20) 
local Rock2Y = Math.Random(20) 
local Rock3Y = Math.Random(20) 
local Rock4Y = Math.Random(20) 
local Rock5Y = Math.Random(20) 

if (Rock1Y == Rock2Y)then 

end 

--Create our rock! 
local MyRock1 = display.newImage("Images/Rock.png", 25, 25) 
local MyRock2 = display.newImage("Images/Rock.png", 25, 25) 
local MyRock3 = display.newImage("Images/Rock.png", 25, 25) 
local MyRock4 = display.newImage("Images/Rock.png", 25, 25) 
local MyRock5 = display.newImage("Images/Rock.png", 25, 25) 

--Add physics to our rock! 
physics.addBody(MyRock1, "dynamic", {density = 1, friction = 0.0, bounce = 0.9, radius=10) 
    MyRock1.myName = "Rock 1" 
physics.addBody(MyRock2, "dynamic", {density = 1, friction = 0.0, bounce = 0.9, radius=10) 
    MyRock2.myName = "Rock 2" 
physics.addBody(MyRock3, "dynamic", {density = 1, friction = 0.0, bounce = 0.9, radius=10) 
    MyRock3.myName = "Rock 3" 
physics.addBody(MyRock4, "dynamic", {density = 1, friction = 0.0, bounce = 0.9, radius=10) 
    MyRock4.myName = "Rock 4" 
physics.addBody(MyRock5, "dynamic", {density = 1, friction = 0.0, bounce = 0.9, radius=10) 
    MyRock5.myName = "Rock 5" 

--Where should MyRock be? 
MyRock1.x = Random(10, _W-50) --Rock 1 
MyRock1.y = Random(_H+Rock1Y) 

MyRock2.x = Random(10, _W-50) --Rock 2 
MyRock2.y = Random(_H+Rock2Y) 

MyRock3.x = Random(10, _W-50) --Rock 3 
MyRock3.y = Random(_H+Rock3Y) 

MyRock4.x = Random(10, _W-50) --Rock 4 
MyRock4.y = Random(_H+Rock4Y) 

MyRock5.x = Random(10, _W-50) --Rock 5 
MyRock5.y = Random(_H+Rock5Y) 

--Here goes the character! 
local character = display.newImage("Images/Character.png", display.contentWidth/2, 0) 
physics.addBody(character, "dynamic", {density = 0, friction = 0.0, bounce = 0.9, radius = 2) 
character.myName = "Character" 

--Check for collision with character 
local function onCollision(event) 
if (event.phase == "began") then 

     --IF THE PLAYER IS HIT ADD THIS CODE BRENDAN DO IT TONIGHT 
    end 
end 
+0

看起來不像你已經嘗試任何事情,請註明您曾嘗試在onCollision什麼。你可以編寫僞代碼。 – Schollii

+0

您可以使用碰撞過濾器。 corona API中也指定了相同的情況。閱讀此文檔以獲取更多信息:http://docs.coronalabs.com/guide/physics/collisionDetection/#collision-filtering –

回答

0

碰撞事件對象的有關相撞對象的信息。你可以做這樣的事情:

if event.object1 == character or event.object2 == character then 
    print("character was hit!") 
    --end game and display game over code 
end 

在這裏閱讀更多: http://docs.coronalabs.com/api/event/collision/index.html