Hello!
As you can see by the title, I am trying to control my turret using a slider. I created this game from a cannonball tutorial that used buttons to control movement.
I have created a working slider that moves up and down within a certain area, but I don’t know how to make it control the turret.
Code:
[spoiler]
--Oliver Duce --Shooty game --2015 local background = display.newImage("bg2.png",0,0) --Define the variables rotateAmt = 5 rotateMin = -90 rotateMax = 40 turretRotation = 0 turretForce = 1200 playerPoints = 0 targetHit = false timeLeft = 500 --Define the functions function closeDownApp(event) --os.exit() end function update( ) --decrease the time counter timeLeft = timeLeft - 1 scoreDisplay.text = 'Score: ' .. playerPoints .. ' Time: ' .. timeLeft --check if the time has run out if (timeLeft \<= 0) then --display the final score scoreDisplay.text = 'Final Score: ' .. playerPoints --remove all of the screen objects display.remove(turretBase) display.remove(turretBarrel) display.remove(target) display.remove(upButton) display.remove(downButton) display.remove(fireButton) --display the 'game over' sign gameOver = display.newText("GAME OVER!", 140, 135, "Segoe UI", 60 ) gameOver:setFillColor( 200,0,0 ) --gameOver:addEventListener('touch', closeDownApp) end --check if the target has been hit if (targetHit == true) then targetHit = false playerPoints = playerPoints + 5 timeLeft = timeLeft +100 Hit = display.newImage('Hit.png', target.x-65, target.y-65) Hit:scale( 0.3, 0.3) --replace the target with the hit picture transition.dissolve(Hit, target, 1000, 0) display.remove(bullet) --put the target back to a random position target.x = math.random(200, 450 ) target.y = math.random(100, 290 ) end end function onCollide(event) targetHit=true end function fire(event) --only fire at the beginning of a touch event if (event.phase == 'began') then media.playSound('bang.wav') bullet = display.newImage('cannonball.png') bullet:scale( 0.3, 0.3 ) --move the image bullet.x, bullet.y = turretBarrel:localToContent(70, 0) bullet.rotation = turretRotation --apply physics to the cannonball physics.addBody( bullet, { density=1.5, friction=0.2, radius=15 } ) --determine the appropriate ratio of horizontal to vertical force force\_x = math.cos(math.rad(turretRotation)) \* turretForce force\_y = math.sin(math.rad(turretRotation)) \* turretForce --fire the turretball bullet:applyForce( force\_x, force\_y, bullet.x, bullet.y ) end end function moveDown(event) --only move the barrel if the touch event started if (event.phase == 'began') then turretRotation = turretRotation + rotateAmt if (turretRotation \>= rotateMax) then turretRotation = rotateMax end turretBarrel.rotation = turretRotation fireButton.rotation = turretRotation end end function moveUp(event) --only move the barrel if the touch event started if (event.phase == 'began') then turretRotation = turretRotation - rotateAmt if (turretRotation \<= rotateMin) then turretRotation = rotateMin end turretBarrel.rotation = turretRotation fireButton.rotation = turretRotation end end function makeTarget( ) target = display.newImage('Plane.png') target:scale( 0.3, 0.3) target.x = math.random(200, 450) target.y = math.random(100, 290) physics.addBody(target,{density=1.0, friction=0.5, bounce=0.05, radius=15}) target.bodyType = 'static' target:addEventListener('collision', onCollide) end function Slider( ) --Slider back sliderBack=display.newImage('Slider back.png', -30, -80) sliderBack:scale(0.3, 0.3) --Slider slider=display.newImage('Slider.png', -15, 140) slider:scale(0.3, 0.3) yMin = 107; yMax = 233; local function onTouch( event ) local t = event.target local phase = event.phase if "began" == phase then -- Make target the top-most object local parent = t.parent parent:insert( t ) display.getCurrentStage():setFocus( t, event.id ) t.isFocus = true -- Store initial touch position on the actual object - prevents jumping when touched t.xStart = event.x - t.x t.yStart = event.y - t.y elseif t.isFocus then if "moved" == phase then print(t.yStart) t.y = event.y - t.xStart if (t.y \< yMin) then t.y = yMin end if (t.y \> yMax) then t.y = yMax end --t.y = event.y - t.yStart elseif "ended" == phase or "cancelled" == phase then display.getCurrentStage():setFocus( t, nil ) t.isFocus = false end end --This tells the system that the event -- should not be propagated to listeners of any objects underneath. return true end slider:addEventListener('touch', onTouch) --Slider control slider:addEventListener(' ', moveDown) --space for event listener to go slider:addEventListener(' ', moveUp) --space for event listener to go end function makeInterface( ) --up button -- upButton = display.newImage('up\_button.png') -- upButton:translate(10, 30) -- upButton:addEventListener('touch', moveUp) --down button -- downButton = display.newImage('down\_button.png') -- downButton:translate(10, 155) -- downButton:addEventListener('touch', moveDown) --fire button fireButton = display.newImage('fire\_button.png') fireButton:translate(500, 110) fireButton:addEventListener('touch', fire) --display turret parts turretBarrel = display.newImage('turretTop.png', 0, 150) turretBarrel:scale( 0.2, 0.2 ) turretBarrel.anchorX = 0.2813127413127 turretBarrel.anchorY = 0.805 turretBarrel.x = display.contentCenterX turretBarrel.x = display.contentCenterY turretBase = display.newImage('turretB.png', -230, 180) turretBase:scale( 0.2, 0.2 ) --turretBase:translate(70, 220) turretBarrel.x = display.contentCenterX turretBarrel.x = display.contentCenterY --display score scoreDisplay = display.newText( ('Points: ' .. playerPoints), 130, 0, "Segoe UI", 20 ) scoreDisplay:setTextColor( 0,0,100 ) scoreDisplay:translate(display.contentHeight, 0, 0) end --Define control structure function init( ) display.setStatusBar(display.HiddenStatusBar) background = display.newImage('bg2.png',0,10) physics = require('physics') physics.setDrawMode('normal') physics.start( ) makeInterface( ) makeTarget( ) Runtime:addEventListener('enterFrame', update) Slider( ) end --Call the code init( )
[/spoiler]
I have commented out the parts I don’t need for now, and I am just keeping them there for reference. I have also added the space for the Event Listeners to go to make the slider control the turret.
Thank you for your help.
-Oliver
P.S If it isn’t too much to ask, is there a way to get the slider (and turret) to move back to the default position when you let go of the slider, instead of staying where it is?
Thanks again!
