LUA碰撞,多个对象

时间:2014-02-12 17:16:09

标签: lua corona collision

我有一个程序,玩家将躲避坠落的小行星,小行星将不得不穿过地面并相互撞击而不会结束游戏。但是,如果一个asateroid击中一个玩家,游戏将需要结束并在屏幕上显示游戏。如何区分联系人。

到目前为止,这是我的代码:

--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

1 个答案:

答案 0 :(得分: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

相关问题