Alright, so I applied most of what saifuls.ctg89 recommended. I was able to control the speed by varying the bulletTimer (“t”). I didn’t do the invisible button yet, I will work on that next. The issue I am currently having is that after holding the “touch” for various times (between half second and 1 second typcially) it starts to cut my bullet travel distance (transition.to distance) to… like really small (maybe 1/4th of the screen". Any idea on why that is happening?
Lastly, I am wanting to keep the drag function as well, but use this newly defined BOX to control the travel of the ship. Currently, I have it so that when clicking in the box I can drag it, but if I stop touching and then retouch, it restarts my ship into the center of the screen (teleports it there if it isn’t already). Any ideas?
Below is my code.
--This is the code for my onTouch, which is triggered when touching the box. local function onTouch(event) local phase = event.phase local newLaser if phase == "began" then local function fireLaser() newLaser = display.newImageRect( mainGroup, objectSheet, 5, 14, 40 ) physics.addBody( newLaser, "dynamic", { isSensor=true } ) newLaser.isBullet = true newLaser.myName = "laser" newLaser.x = ship.x newLaser.y = ship.y newLaser:toBack() -- Play fire sound! audio.play( fireSound, {loops=-1} ) transition.to( newLaser, { y=-40, time=1000, onComplete = function() display.remove( newLaser ) end } ) end bulletTimer = timer.performWithDelay(200, fireLaser, -1) elseif phase == "ended" then timer.cancel(bulletTimer) end end
-- This is the info for the fireButton (or invisible rectangle) located in the function scene:create( event ) local fireButton = display.newRect(display.contentCenterX, display.actualContentHeight / 3 \* 2, display.actualContentWidth, display.actualContentHeight +100) fireButton.isVisible = false fireButton.isHitTestable = true fireButton:addEventListener( "touch", dragShip ) fireButton:addEventListener("touch", onTouch)
--This is the code I am currently using for dragShip local function dragShip( event ) local box = event.target local phase = event.phase if ( "began" == phase ) then -- Set touch focus on the ship display.currentStage:setFocus( box ) -- Store initial offset position ship.touchOffsetX = event.x - box.x elseif ( "moved" == phase ) then -- Move the ship to the new touch position ship.x = event.x - ship.touchOffsetX elseif ( "ended" == phase or "cancelled" == phase ) then -- Release touch focus on the ship display.currentStage:setFocus( nil ) end return true -- Prevents touch propagation to underlying objects end
--Lastly, this is how ship is defined (in the scene:create(event) section) ship = display.newImageRect( mainGroup, objectSheet, 4, 98, 79 ) ship.x = display.contentCenterX ship.y = display.contentHeight - 100 physics.addBody( ship, { radius=30, isSensor=true } ) ship.myName = "ship"