Hi,
I got a really big problem and I don’t know how to resolve it … so I really need your help !
I am creating a simple game for a school project.
I would like to finalize my game by adding an end when the armadillo touch a meteorite, however I don’t know how. Ideally when the armadillo creates a meteor collision, the game stops, displays the score and brings up the button “play again” which allows you to replay, but I really do not know how to do that … At the end of the program, I put a green text what I had found on another program I made, I don’t know if it is really helpful, but you never know …
I’ll be very grateful if you can help me finish my game.
I joined you my program, any help will help me.
Thank you already for your great help.
Lucas
[lua] --Game scene
local composer = require(“composer”)
local scene = composer.newScene()
–Physic function
local physics = require (“physics”)
physics.start ()
physics.setGravity(0, 0)
–physics.setDrawMode(“hybrid”)
local leftWall = display.newRect(0, display.contentCenterY, 1, 700)
local rightWall = display.newRect(320, display.contentCenterY, 1, 700)
local flooring = display.newRect (display.contentCenterX, 520, 500, 1 )
local ceiling = display.newRect(display.contentCenterX, -44, 500, 1 )
physics.addBody(leftWall, “static”, { bounce = 0.1})
physics.addBody(rightWall, “static”, { bounce = 0.1})
physics.addBody(ceiling, “static”, { bounce = 0.1})
physics.addBody(flooring, “static”, { bounce = 0.1})
–createMeteor()
function scene:create(event)
–Background
local background = display.newImage(“fond.png”, 200, 250, 480, 70)
–Player
local armadillo = display.newImage (“arm.png”, 160, 200, 30)
physics.addBody(armadillo, “dynamic”,{ bounce =0.8,friction = 1.0})
armadillo.gravityScale = 0
armadillo.isFixedRotation = true
armadillo.ID = “arma”
_W = display.contentWidth;
_H = display.contentHeight;
motionx = 0;
motiony = 0
speed = 3;
–create button
local gauche = display.newImage(“boutongauche.png”)
gauche.x = 50; gauche.y = 460;
local droit = display.newImage(“boutondroit.png”)
droit.x = 150; droit.y = 460
local bas = display.newImage(“boutonbas.png”)
bas.x = 100; bas.y = 460
local haut = display.newImage(“boutonhaut.png”)
haut.x = 100; haut.y = 410
–move character
function gauche:touch()
motionx = -speed;
end
gauche:addEventListener(“touch”,gauche)
function droit:touch()
motionx = speed;
end
droit:addEventListener(“touch”,droit)
function haut:touch()
motiony = -speed;
end
haut:addEventListener(“touch”, haut)
function bas:touch()
motiony = speed;
end
bas:addEventListener(“touch”, bas)
local function movearmadillo (event)
armadillo.x = armadillo.x + motionx;
armadillo.y = armadillo.y + motiony
local function stop (event)
if event.phase ==“ended” then
motionx = 0;
motiony = 0
end
end
Runtime:addEventListener(“touch”, stop )
end
Runtime:addEventListener(“enterFrame”, movearmadillo)
local function spawn( objectType, x, y )
local meteor
local sizeXY = math.random( 20, 100 )
local collisionFilter = { categoryBits = 4, maskBits = 2 }
local body = { filter=collisionFilter, isSensor=true }
local meteor = display.newImage(“meteor.png”)
physics.addBody(meteor, “dynamic”,{friction = 1.0, radius={200}})
meteor.x = math.random (5, 315)
meteor.y = math.random (-40, 515)
function moveMeteor()
transition.to(meteor, {time=2000, x=math.random(5, 315), y=math.random(-40,515), onComplete=moveMeteor})
end
moveMeteor()
end
local meteor = spawn (“meteor.png”)
local function spawn2( objectType, x, y )
local meteor2
local sizeXY = math.random( 20, 100 )
local collisionFilter = { categoryBits = 4, maskBits = 2 }
local body = { filter=collisionFilter, isSensor=true }
local meteor2 = display.newImage(“meteor.png”)
physics.addBody(meteor2, “dynamic”,{friction = 1.0, radius={200}})
meteor2.x = math.random (5, 315)
meteor2.y = math.random (-40, 515)
function moveMeteor2()
transition.to(meteor2, {time=2000, x=math.random(5, 315), y=math.random(-40,515), onComplete=moveMeteor2})
end
moveMeteor2()
end
local meteor2 = spawn2 (“meteor.png”)
–Score
local score = 0
local scoreTxt = display.newText(score, 300, 30, font, 50)
scoreTxt.x = 160
scoreTxt.y = -23
local function updateScore()
score = score + 1
scoreTxt.text = score
end
local scoreTimer = timer.performWithDelay(1000, updateScore, -1)
–collision
–end game
–function endGame ()
–armadillo:removeEventListener( “collision”, armadillo)
–Runtime:removeEventListener (“enterFrame”, onUpdate)
–background:removeEventListener (“touch”, flapBird)
–local promptEnd = display.newText (“You Lost !”, 160, 100, font, 32)
–promptEnd:setFillColor (0,0,0)
–local promptReplay = display.newText (“Play Again”, 160, 240, font, 32)
–promptReplay:setFillColor(0,0,0)
–sceneGroup:insert (promptEnd)
–sceneGroup:insert (promptReplay)
–local function replay(event)
–if (event.phase == “began”) then
–composer.gotoScene (“mainMenu”, {effect = “fade”, time = 1000})
–end
–end
–promptReplay:addEventListener (“touch”, replay)
–end
–armadillo.collision = onLocalCollision
–armadillo:addEventListener(“collision”, armadillo)
end
scene:addEventListener(“create”, scene)
return scene[/lua]