Im trying to make a wave system using the game loop. However, I am encountering some problems.
The main objective of this minigame is to deliver the imaginary supplies to the box that is told to go to. Once the number of supplies is 0, you will go to the next wave where the number of supplies has increased by 1.
Here is my code:
local buttonup = display.newCircle(display.contentCenterX+125,display.contentCenterY+150, 30) local uptext = display.newText("up",buttonup.x,buttonup.y,native.systemFont) uptext:setFillColor(0,0,0) local buttondown = display.newCircle(display.contentCenterX+125,display.contentCenterY+220, 30) local downtext = display.newText("down",buttondown.x,buttondown.y,native.systemFont) downtext:setFillColor(0,0,0) --Boxes and Rectangle local box1 = display.newRect(0,0,124,88) box1.x=display.contentCenterX-98 box1.y=display.contentCenterY+219 box1:setFillColor(math.random(),math.random(),math.random()) local box1text = display.newText("Box 1",box1.x,box1.y,native.systemFont) box1text:setFillColor(math.random()/2,math.random()/2,math.random()/2) local boxline1 = display.newLine(box1.x,box1.y+10,box1.x+200,box1.y+10) local box2 = display.newRect(0,0,124,88) box2.x=display.contentCenterX-98 box2.y=display.contentCenterY+109 box2:setFillColor(math.random(),math.random(),math.random()) local box2text = display.newText("Box 2",box2.x,box2.y,native.systemFont) box2text:setFillColor(math.random()/2,math.random()/2,math.random()/2) local boxline2 = display.newLine(box2.x,box2.y+10,box2.x+200,box2.y+10) local box3 = display.newRect(0,0,124,88) box3.x=display.contentCenterX-98 box3.y=display.contentCenterY box3:setFillColor(math.random(),math.random(),math.random()) local box3text = display.newText("Box 3",box3.x,box3.y,native.systemFont) box3text:setFillColor(math.random()/2,math.random()/2,math.random()/2) local boxline3 = display.newLine(box3.x,box3.y+10,box3.x+200,box3.y+10) local box4 = display.newRect(0,0,124,88) box4.x=display.contentCenterX-98 box4.y=display.contentCenterX-30 box4:setFillColor(math.random(),math.random(),math.random()) local box4text = display.newText("Box 4",box4.x,box4.y,native.systemFont) box4text:setFillColor(math.random()/2,math.random()/2,math.random()/2) local boxline4 = display.newLine(box4.x,box4.y+10,box4.x+200,box4.y+10) local rectangle = display.newRect(0,0,60,80) rectangle.x = display.contentCenterX+25 rectangle.y = box1.y+10 rectangle:setFillColor(math.random(),math.random(),math.random()) --Button functions and hold function local function moveup(event) rectangle.y = rectangle.y-1 end local function movedown(event) rectangle.y = rectangle.y+1 end local function handleEnterFrame( event ) if ( needToup == true ) then moveup() end end local needTodown = false local function handleEnterFrame2( event ) if ( needTodown == true ) then movedown() end end Runtime:addEventListener( "enterFrame", handleEnterFrame ) Runtime:addEventListener( "enterFrame", handleEnterFrame2 ) local function handleupButton( event ) if ( event.phase == "began" ) then needToup = true elseif ( event.phase == "ended" and needToup == true ) then needToup = false end return true end local function handledownButton( event ) if ( event.phase == "began" ) then needTodown = true elseif ( event.phase == "ended" and needTodown == true ) then needTodown = false end return true end buttonup:addEventListener("touch", handleupButton) buttondown:addEventListener("touch", handledownButton) local wave = 1 local supplies = wave local boxes = 4 local gotobox = math.random(1,boxes) local waveText = display.newText(wave, buttonup.x, 100, native.systemFontBold, 25) local waveTitle = display.newText("Wave:", waveText.x, waveText.y-20, native.systemFontBold, 25) local suppliesText = display.newText(supplies, buttonup.x, 175, native.systemFontBold, 25) local suppliesTitle = display.newText("Supplies:", suppliesText.x, suppliesText.y-20, native.systemFontBold, 20) local boxesText = display.newText(boxes, buttonup.x, 250, native.systemFontBold, 25) local boxesTitle = display.newText("Boxes:", boxesText.x, boxesText.y-20, native.systemFontBold, 20) local gotoboxText = display.newText(gotobox, buttonup.x, 325, native.systemFontBold, 25) local gotoboxTitle = display.newText("Go to:", gotoboxText.x, gotoboxText.y-20, native.systemFontBold, 25) local function nextp() buttonup:addEventListener("touch", handleupButton) buttondown:addEventListener("touch", handledownButton) end --EventListener/GameLoop local function gameLoop() if rectangle.y == box1.y+10 then print("Parallel to Box 1") if gotobox == 1 and rectangle.y==box1.y+10 then supplies=supplies-1 buttonup:removeEventListener("touch", handleupButton) buttondown:removeEventListener("touch", handledownButton) timer.performWithDelay( 2000, nextp ) if supplies\>0 then gotobox = math.random(1,boxes) elseif supplies==0 then wave=wave+1 rectangle.y=box1.y+10 boxes = 4 gotobox = math.random(1,boxes) end end end end if rectangle.y == box2.y+10 then print("Parallel to Box 2") if gotobox == 2 and rectangle.y==box2.y+10 then supplies=supplies-1 buttonup:removeEventListener("touch", handleupButton) buttondown:removeEventListener("touch", handledownButton) timer.performWithDelay( 2000, nextp ) if supplies\>0 then gotobox = math.random(1,boxes) elseif supplies==0 then wave=wave+1 rectangle.y=box2.y+10 boxes = 4 supplies=wave gotobox = math.random(1,boxes) end end end if rectangle.y == box3.y+10 then print("Parallel to Box 3") if gotobox == 3 and rectangle.y==box3.y+10 then supplies=supplies-1 buttonup:removeEventListener("touch", handleupButton) buttondown:removeEventListener("touch", handledownButton) timer.performWithDelay( 2000, nextp ) if supplies\>0 then gotobox = math.random(1,boxes) elseif supplies==0 then wave=wave+1 rectangle.y=box3.y+10 boxes = 4 gotobox = math.random(1,boxes) end end end if rectangle.y == box4.y+10 then print("Parallel to Box 4") if gotobox == 4 and rectangle.y==box4.y+10 then supplies=supplies-1 buttonup:removeEventListener("touch", handleupButton) buttondown:removeEventListener("touch", handledownButton) timer.performWithDelay( 2000, nextp ) if supplies\>0 then gotobox = math.random(1,boxes) elseif supplies==0 then wave=wave+1 rectangle.y=box4.y+10 boxes = 4 gotobox = math.random(1,boxes) end end end Runtime:addEventListener("enterFrame", gameLoop)
Here is what it looks like in the simulator:
Ill tell you what all the code means:
local buttonup = display.newCircle(display.contentCenterX+125,display.contentCenterY+150, 30) local uptext = display.newText("up",buttonup.x,buttonup.y,native.systemFont) uptext:setFillColor(0,0,0) local buttondown = display.newCircle(display.contentCenterX+125,display.contentCenterY+220, 30) local downtext = display.newText("down",buttondown.x,buttondown.y,native.systemFont) downtext:setFillColor(0,0,0) --Boxes and Rectangle local box1 = display.newRect(0,0,124,88) box1.x=display.contentCenterX-98 box1.y=display.contentCenterY+219 box1:setFillColor(math.random(),math.random(),math.random()) local box1text = display.newText("Box 1",box1.x,box1.y,native.systemFont) box1text:setFillColor(math.random()/2,math.random()/2,math.random()/2) local boxline1 = display.newLine(box1.x,box1.y+10,box1.x+200,box1.y+10) local box2 = display.newRect(0,0,124,88) box2.x=display.contentCenterX-98 box2.y=display.contentCenterY+109 box2:setFillColor(math.random(),math.random(),math.random()) local box2text = display.newText("Box 2",box2.x,box2.y,native.systemFont) box2text:setFillColor(math.random()/2,math.random()/2,math.random()/2) local boxline2 = display.newLine(box2.x,box2.y+10,box2.x+200,box2.y+10) local box3 = display.newRect(0,0,124,88) box3.x=display.contentCenterX-98 box3.y=display.contentCenterY box3:setFillColor(math.random(),math.random(),math.random()) local box3text = display.newText("Box 3",box3.x,box3.y,native.systemFont) box3text:setFillColor(math.random()/2,math.random()/2,math.random()/2) local boxline3 = display.newLine(box3.x,box3.y+10,box3.x+200,box3.y+10) local box4 = display.newRect(0,0,124,88) box4.x=display.contentCenterX-98 box4.y=display.contentCenterX-30 box4:setFillColor(math.random(),math.random(),math.random()) local box4text = display.newText("Box 4",box4.x,box4.y,native.systemFont) box4text:setFillColor(math.random()/2,math.random()/2,math.random()/2) local boxline4 = display.newLine(box4.x,box4.y+10,box4.x+200,box4.y+10) local rectangle = display.newRect(0,0,60,80) rectangle.x = display.contentCenterX+25 rectangle.y = box1.y+10 rectangle:setFillColor(math.random(),math.random(),math.random())
Above is the code display the buttons, boxes, trigger lines, rectangle etc.
--Button functions and hold function local function moveup(event) rectangle.y = rectangle.y-1 end local function movedown(event) rectangle.y = rectangle.y+1 end local function handleEnterFrame( event ) if ( needToup == true ) then moveup() end end local needTodown = false local function handleEnterFrame2( event ) if ( needTodown == true ) then movedown() end end Runtime:addEventListener( "enterFrame", handleEnterFrame ) Runtime:addEventListener( "enterFrame", handleEnterFrame2 ) local function handleupButton( event ) if ( event.phase == "began" ) then needToup = true elseif ( event.phase == "ended" and needToup == true ) then needToup = false end return true end local function handledownButton( event ) if ( event.phase == "began" ) then needTodown = true elseif ( event.phase == "ended" and needTodown == true ) then needTodown = false end return true end buttonup:addEventListener("touch", handleupButton) buttondown:addEventListener("touch", handledownButton)
Above gives the touch and hold function for the buttons.
local wave = 1 local supplies = wave local boxes = 4 local gotobox = math.random(1,boxes) local waveText = display.newText(wave, buttonup.x, 100, native.systemFontBold, 25) local waveTitle = display.newText("Wave:", waveText.x, waveText.y-20, native.systemFontBold, 25) local suppliesText = display.newText(supplies, buttonup.x, 175, native.systemFontBold, 25) local suppliesTitle = display.newText("Supplies:", suppliesText.x, suppliesText.y-20, native.systemFontBold, 20) local boxesText = display.newText(boxes, buttonup.x, 250, native.systemFontBold, 25) local boxesTitle = display.newText("Boxes:", boxesText.x, boxesText.y-20, native.systemFontBold, 20) local gotoboxText = display.newText(gotobox, buttonup.x, 325, native.systemFontBold, 25) local gotoboxTitle = display.newText("Go to:", gotoboxText.x, gotoboxText.y-20, native.systemFontBold, 25)
Above displays the wave number, number for boxes, amount of supplies, and the box to go to.
local function nextp() buttonup:addEventListener("touch", handleupButton) buttondown:addEventListener("touch", handledownButton) end --EventListener/GameLoop local function gameLoop() if rectangle.y == box1.y+10 then print("Parallel to Box 1") if gotobox == 1 and rectangle.y==box1.y+10 then supplies=supplies-1 buttonup:removeEventListener("touch", handleupButton) buttondown:removeEventListener("touch", handledownButton) timer.performWithDelay( 2000, nextp ) if supplies\>0 then gotobox = math.random(1,boxes) elseif supplies==0 then wave=wave+1 rectangle.y=box1.y+10 boxes = 4 gotobox = math.random(1,boxes) end end end end if rectangle.y == box2.y+10 then print("Parallel to Box 2") if gotobox == 2 and rectangle.y==box2.y+10 then supplies=supplies-1 buttonup:removeEventListener("touch", handleupButton) buttondown:removeEventListener("touch", handledownButton) timer.performWithDelay( 2000, nextp ) if supplies\>0 then gotobox = math.random(1,boxes) elseif supplies==0 then wave=wave+1 rectangle.y=box2.y+10 boxes = 4 supplies=wave gotobox = math.random(1,boxes) end end end if rectangle.y == box3.y+10 then print("Parallel to Box 3") if gotobox == 3 and rectangle.y==box3.y+10 then supplies=supplies-1 buttonup:removeEventListener("touch", handleupButton) buttondown:removeEventListener("touch", handledownButton) timer.performWithDelay( 2000, nextp ) if supplies\>0 then gotobox = math.random(1,boxes) elseif supplies==0 then wave=wave+1 rectangle.y=box3.y+10 boxes = 4 gotobox = math.random(1,boxes) end end end if rectangle.y == box4.y+10 then print("Parallel to Box 4") if gotobox == 4 and rectangle.y==box4.y+10 then supplies=supplies-1 buttonup:removeEventListener("touch", handleupButton) buttondown:removeEventListener("touch", handledownButton) timer.performWithDelay( 2000, nextp ) if supplies\>0 then gotobox = math.random(1,boxes) elseif supplies==0 then wave=wave+1 rectangle.y=box4.y+10 boxes = 4 gotobox = math.random(1,boxes) end end end Runtime:addEventListener("enterFrame", gameLoop)
And above is the game loop. Most of the code is the same so I will explain what the main part means.
if rectangle.y == box1.y+10 then print("Parallel to Box 1") if gotobox == 1 and rectangle.y==box1.y+10 then supplies=supplies-1 buttonup:removeEventListener("touch", handleupButton) buttondown:removeEventListener("touch", handledownButton) timer.performWithDelay( 2000, nextp ) if supplies\>0 then gotobox = math.random(1,boxes) elseif supplies==0 then wave=wave+1 rectangle.y=box1.y+10 boxes = 4 gotobox = math.random(1,boxes) end end end end
Above is the main part of the code which is repeated for each box. Above is the code for box 1.
if gotobox == 1 and rectangle.y==box1.y+10 then supplies=supplies-1 buttonup:removeEventListener("touch", handleupButton) buttondown:removeEventListener("touch", handledownButton) timer.performWithDelay( 2000, nextp )
Aboves code means that if the game tells you to go to box 1 and that the rectangle is parallel to the box
the number of supplies is decreased by 1.
buttonup:removeEventListener("touch", handleupButton) buttondown:removeEventListener("touch", handledownButton) timer.performWithDelay( 2000, nextp )
this is supposed to stop the box for 2 seconds but I havent gotten this to work.
nextp referes to a function I made earlier:
local function nextp() buttonup:addEventListener("touch", handleupButton) buttondown:addEventListener("touch", handledownButton) end
This is to add back the event listener to the buttons
Going back to the code:
if supplies\>0 then gotobox = math.random(1,boxes) elseif supplies==0 then wave=wave+1 rectangle.y=box1.y+10 boxes = 4 gotobox = math.random(1,boxes)
Above means that if the amount of supplies is over 0, find another box to go to.
If the number of supplies equals to zero, then add a 1 to the number of waves and send the rectangle back to previous area.
And thats all to it.
My issues are that:
-
Game does not recognize when the rectangle is parallel to the boxes 2,3, and 4
-
Will not subtract the number of supplies when it is parallel to belonging box
3)Will change to wave 2
What am I doing wrong here?!
If anyone has any question, feel free to ask.
Thanks