[lua]module(…, package.seeall);
system.activate( “multitouch” )
function new()
local localGroup2 = display.newGroup();
local Planets = display.newGroup();
local physics = require(“physics”)
physics.start()
physics.setGravity( 0, .5 )
--------------------Ship Picture--------------------
local function newShip()
local s = display.newGroup()
local ship = display.newImage(“ship.png”)
ship.x = 0; ship.y = 0;
local ship_body = {}
ship_body[“ship”] = {
{
density = 2, friction = 0, bounce = 0,
filter = { categoryBits = 1, maskBits = 65535 },
shape = { -23.9285714626312, 13.5 , -14.9285717010498, 3.78571510314941 , -8.71428489685059, 17.7142858505249 , -24.2857142686844, 26.2857143878937 }
} ,
{
density = 2, friction = 0, bounce = 0,
filter = { categoryBits = 1, maskBits = 65535 },
shape = { 6.92856979370117, -21.6428565979004 , 12.3571434020996, -12.6428565979004 , 13.7857131958008, 4.21428489685059 , 7.28571319580078, 17.7142858505249 , -14.9285717010498, 3.78571510314941 , -10.2142858505249, -17.0714302062988 , -0.714284896850586, -28.5714302062988 }
} ,
{
density = 2, friction = 0, bounce = 0,
filter = { categoryBits = 1, maskBits = 65535 },
shape = { -14.9285717010498, 3.78571510314941 , 7.28571319580078, 17.7142858505249 , -8.71428489685059, 17.7142858505249 }
} ,
{
density = 2, friction = 0, bounce = 0,
filter = { categoryBits = 1, maskBits = 65535 },
shape = { 7.28571319580078, 17.7142858505249 , 13.7857131958008, 4.21428489685059 , 21.7142868041992, 10.2857151031494 , 25.7857131958008, 18.2142858505249 , 24.8571434020996, 27.5714285969734 }
}
}
physics.addBody(s, “static”, unpack(ship_body[“ship”]))
s:insert(ship)
s:setReferencePoint(display.CenterReferencePoint)
return s
end
ship = newShip()
ship.x = _W/2; ship.y = _H/1.3
localGroup2:insert(ship);
-----------------------Cross hair right side--------------------------------
local crosshair = display.newImage(“crosshair.png”, {alpha = 0})
crosshair.x = _W/1.22; crosshair.y = _H/1.08;
crosshair.xScale = .5; crosshair.yScale = .5;
localGroup2:insert(crosshair);
function crosshair:touch(e)
if(e.phase == “began”) then
transright = transition.to( ship, { time= 500, x=(_W/1.18), y=(_H/1.3), transition = easing.linear} )
else
if(e.phase == “ended”) then
transition.cancel(transright)
end
end
end
------------------------------Cross hair right side ------------------------------
local crosshair2 = display.newImage(“crosshair.png”, {alpha = 0})
crosshair2.x = _W/5.22; crosshair2.y = _H/1.08;
crosshair2.xScale = .5; crosshair2.yScale = .5;
localGroup2:insert(crosshair2);
function crosshair2:touch(e)
if(e.phase == “began”) then
transleft = transition.to( ship, { time= 500, x=(_W/6.5), y=(_H/1.3), transition = easing.linear } )
else
if(e.phase == “ended”) then
transition.cancel(transleft)
end
end
end
-----Listeners-----------
crosshair:addEventListener(“touch”, crosshair);
crosshair2:addEventListener(“touch”, crosshair2);
------------------------Make The Asteroid Custom Physics Body----------------------------------------------
local function newAsteroid()
local g = display.newGroup()
local body = display.newImage(“asteroid2.png”)
body.x = 0; body.y = 0;
–body.xScale = .5; body.yScale = .5;
local physics_body = {}
physics_body[“asteroid2”] = {
{
density = 2, friction = 0, bounce = 0,
filter = { categoryBits = 1, maskBits = 65535 },
shape = { 19.2166862487793, 12.2166848182678 , 8.43336868286133, 19.1083423495293 , -9.73293590545654, 19.9918744461611 , -15.8661971092224, 15.0162510871887 , -19.5666304826736, -11.8916568756104 , 1.7253532409668, -21.0417137145996 , 12.8586120605469, -16.2248115539551 , 21.9750823974609, -4.37486457824707 }
} ,
{
density = 2, friction = 0, bounce = 0,
filter = { categoryBits = 1, maskBits = 65535 },
shape = { -19.5666304826736, -11.8916568756104 , -15.8661971092224, 15.0162510871887 , -21.3499457985163, -6.98374938964844 }
}
}
physics.addBody(g, unpack(physics_body[“asteroid2”]))
g:insert(body)
g:setReferencePoint(display.CenterReferencePoint)
return g
end
----------------------------Spawn in the asteroids-------------------------------------------------------
local p = {};
local function spawnPlanets(number)
local function spawn(e)
local planet = p[e.count]
planet = newAsteroid()
planet.x = math.random((_W * .5)-100, (_W *.5)+100)
planet.y = -50
Planets:insert(planet)
if(e.count == number) then
timer.cancel(tmr)
tmr = nil
end
end
tmr = timer.performWithDelay(2000, spawn, number)
end
spawnPlanets(10)
------------------------------------------------COLISION DETECTOR----------------------------------------------
local function onLocalCollision( self, event )
if ( event.phase == “began” ) then
print “collision started”
elseif ( event.phase == “ended” ) then
----Stop all Listeners----
ship:removeEventListener( “collision”, ship )
crosshair:removeEventListener(“touch”, crosshair);
crosshair2:removeEventListener(“touch”, crosshair2);
Planets:removeSelf()
local overlay = display.newImage(“blackoverlay.png”, _W, _H, 132);
overlay:setReferencePoint(display.CenterReferencePoint);
overlay.x = _W/2; overlay.y = _H/2;
localGroup2:insert(overlay)
local lose = display.newImage(“lose.png”, _W, _H, 132);
lose:setReferencePoint(display.CenterReferencePoint);
lose.x = _W/2; lose.y = _H/4;
localGroup2:insert(lose)
----------------------------------------Menu Button PopUp--------------------
local menu = ui.newButton{
default = “menubutton.png”,
over = “menubuttonin.png”,
}
menu.x = _W/2.7; menu.y = _H/2;
menu.xScale = .7; menu.yScale = .7;
localGroup2:insert(menu)
menu.scene = “menu”
----------------------------------------------Retry Button PopUp-------------------
local retry = ui.newButton{
default = “retry.png”,
over = “retryin.png”,
}
retry.x = _W/1.6; retry.y = _H/2;
retry.xScale = .7; retry.yScale = .7;
localGroup2:insert(retry)
retry.scene = “level1”
menu:addEventListener(“touch”, changeScene)
retry:addEventListener(“touch”, changeScene)
end
end
ship.collision = onLocalCollision
ship:addEventListener( “collision”, ship )
------------------------------------Test Code-----------------------------------------------
–local function win()
–local win = display.newImage(“win.png”)
–win.x = _W/2; win.y = _H/4;
–localGroup2:insert(win);
–end
----------------Change Scene Director Class Code-----------------------------------
function changeScene(e)
if(e.phase == “ended”) then
if(e.target.scene) then
director:changeScene(e.target.scene);
end
end
end
---------------Test Code-----------------------------
–timer.performWithDelay(28000, win )
----Director Class—
return localGroup2;
end[/lua]
This is my code for a game that I’m making. I need help! I’m using the director class. I want to make it so the asteroids disappear from the screen once the collision happens. If anyone can help me with this problem they will be my best friend forever.
Please help
[import]uid: 29181 topic_id: 9388 reply_id: 309388[/import]