Hi,
I created a game in which there are meteorites that move and just stay alive and not getting hit but I have a problem that, if I manage to solve it, allow me to finish my game totally !!!
When 30 seconds is passed in game (my score will be 30 too) I want a third meteorite appears, however, such as is my code there, the third meteorite bug and begins to move in any direction very quickly. … so there is a problem with my moveMeteor3() function and harder() but I can not seem to solve the …
Can you help me please, I have to project normally on Tuesday and I will like it ended well …
Thank you very much, I attached my code:
--Game scene local composer = require("composer") local scene = composer.newScene() local musicGame = audio.loadStream("musicGame.wav") musicGame = audio.play( musicGame , {loops=200 }) --audio.stop(musicMenu) --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, 524, 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}) local armadillo local function spawnMeteor() local meteor = display.newImage("meteor.png") physics.addBody(meteor, "dynamic",{friction = 1.0, radius={200}}) meteor.x, meteor.y = math.random (5, 315), math.random (-40, 200) meteor.myName = "meteor" return meteor end function scene:create(event) local sceneGroup = self.view --Background local background = display.newImage("fond.png", 200, 250, 480, 70) armadillo = display.newImage("arm.png") meteor1 = spawnMeteor() meteor2 = spawnMeteor() meteor3 = spawnMeteor() meteor3.x = -10 gauche = display.newImage("boutongauche.png") droit = display.newImage("boutondroit.png") bas = display.newImage("boutonbas.png") haut = display.newImage("boutonhaut.png") score = 0 scoreTxt = display.newText(score, 300, 30, font, 50) scoreTxt.x = 160 scoreTxt.y = -23 sceneGroup:insert(background) sceneGroup:insert(scoreTxt) sceneGroup:insert(meteor1) sceneGroup:insert(meteor2) sceneGroup:insert(meteor3) sceneGroup:insert(gauche) sceneGroup:insert(droit) sceneGroup:insert(bas) sceneGroup:insert(haut) sceneGroup:insert(armadillo) end function scene:show (event) local phase = event.phase if (phase == "will") then local sceneGroup = self.view \_W = display.contentWidth; \_H = display.contentHeight; motionx = 0; motiony = 0 speed = 3; armadillo.x = 160 armadillo.y = 400 physics.addBody(armadillo, "dynamic",{ bounce =0.8,friction = 1.0}) armadillo.gravityScale = 0 armadillo.isFixedRotation = true armadillo.ID = "arma" armadillo.myName = "player" gauche.x = 50; gauche.y = 460; droit.x = 150; droit.y = 460 bas.x = 100; bas.y = 460 haut.x = 100; haut.y = 410 local meteor1 local meteor2 local meteor3 elseif (phase =="did") then local sceneGroup = self.view local function movearmadillo (event) --if gauche.x == 50 then 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 end Runtime:addEventListener("enterFrame", movearmadillo) 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) function moveMeteor() transition.to(meteor1, {time=2000, x=math.random(5, 315), y=math.random(-40,515), onComplete=moveMeteor}) end moveMeteor() function moveMeteor2() transition.to(meteor2, {time=2000, x=math.random(5, 315), y=math.random(-40,515), onComplete=moveMeteor2}) end moveMeteor2() function moveMeteor3() transition.to(meteor3, {time=2000, x=math.random(5, 315), y=math.random(-40,515), onComplete=moveMeteor3}) end function harder (event) if score \>= 5 then moveMeteor3() end end Runtime: addEventListener("enterFrame", harder) local function updateScore() score = score + 1 scoreTxt.text = score end scoreTimer = timer.performWithDelay(1000, updateScore, -1) function onCollision(event) if(event.object1.myName == "player" and event.object2.myName == "meteor") or (event.object1.myName == "meteor" and event.object2.myName == "player") or (event.object1.myName == "player" and event.object2.myName == "meteor2") or (event.object1.myName == "meteor2" and event.object2.myName == "player") then print(event.object1.myName, event.object2.myName, score) Runtime:removeEventListener("enterFrame", movearmadillo) composer.gotoScene("gameOver", {effect = "fade", time = 200}) end end Runtime:addEventListener("collision", onCollision) end end function scene:hide (event) local sceneGroup = self.view local phase = event.phase if(phase == "will") then timer.cancel(scoreTimer) audio.pause("musicGame") composer.setVariable("score", score) moveMeteor = false moveMeteor2 = false elseif (phase == "did") then --composer.gotoScene ( "gameOver", {effect = "fade", time = 200}) end end scene:addEventListener("show", scene) scene:addEventListener("create", scene) scene:addEventListener("hide", scene) return scene
My code is not perfect, I am not a professional and this is not really important as long as it works. Moreover, in my code, I want the meteorite appear after 5 seconds so I could quickly see if it works.
Thank you for your help
Lucas