So I have 4 labeled boxes:
And I have rectangle:
You can move the rectangle up and down by holding down the 2 labeled buttons at the side:
When the rectangle passes the center of each box, the console will print “Parallel to Box (number)”
The picture shows where the event is triggered:
(Sidenote:When the project is loaded, the rectangle is already set parallel to box1)
However, it seems that only box1 and box2 trigger the event where as box 3 and 4 doesn’t.
Why is it doing this?
My code is below:
It is all Plug and Play
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- Your code here --Buttons 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) --Lines,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-2 end local function movedown(event) rectangle.y = rectangle.y+2 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 -- Fire the weapon needToup = true elseif ( event.phase == "ended" and needToup == true ) then -- Stop firing the weapon needToup = false end return true end local function handledownButton( event ) if ( event.phase == "began" ) then -- Fire the weapon needTodown = true elseif ( event.phase == "ended" and needTodown == true ) then -- Stop firing the weapon needTodown = false end return true end buttonup:addEventListener("touch", handleupButton) buttondown:addEventListener("touch", handledownButton) --EventListener/GameLoop local function gameLoop() if rectangle.y == box1.y+10 then print("Parallel to Box 1") end if rectangle.y == box2.y+10 then print("Parallel to Box 2") end if rectangle.y == box3.y+10 then print("Parallel Box 3") end if rectangle== box4.y+10 then print("Parallel Box 4") end end Runtime:addEventListener("enterFrame", gameLoop)