Hi All,
I am very new to the community, in fact this is probably my first post here. I have made myself quite familiar with Corona SDK for over a month now and I have made good progress on the current “game” I’m developing but I do have an issue that I’m hoping you guys can help me out.
I’m not sure if this has been discussed before, but I can’t seem to find an exact topic about my problem, so here goes:
Currently, my “gun” only allows player to fire again if the current bullet has gone off the top of the screen or hits a target (in any case it is set to nil ). My “bullet” speed is slow, such that it approximately takes about .6 seconds from the bottom up.
I want to allow the player to be trigger-happy, eg. fires at will on each tap (not the tap-and-hold kind of continuous firing, but one tap-one-bullet thing). But if I did that, I am facing the problem of how to keep track of all those bullets, how to remove them, and how to manage the collision process. The bullet:removeSelf() cannot seem to pinpoint which one currently flying on screen to clear.
To illustrate my problem, pls see the example code below:
\_W = display.contentWidth \_H = display.contentHeight \_SoundON = true local physics = require("physics") physics.start() local bulletActive = false local bullet local bulletReach = -5 local bulletSpeed = 600 local okToFire = true local fireButton = display.newCircle(\_W-40, \_H-40, 40) ------------------- Gun Listener -------------------- local function handleGun(event) -- remove this IF block to allow continuous firing--- if bulletActive or not okToFire then return end ----------------------------------------------------- bullet = display.newCircle (0, 0, 5) bullet.x = \_W / 2 bullet.y = \_H - 5 physics.addBody ( bullet, "kinematic", {density=0.1, friction=0, bounce=0.0} ) bullet.trans = transition.to( bullet, { time=bulletSpeed, x=bullet.x, y=bulletReach, onStart = function() bulletActive = true -- if \_SoundON then audio.play( bang ) end end , onCancel = function() bulletActive = false bullet:removeSelf() bullet = nil print("bullet removed onCancel") end , onComplete = function() bulletActive = false bullet:removeSelf() bullet = nil print("bullet removed onComplete") end }) end fireButton:addEventListener( "tap", handleGun )
If I remove the if-then block at the start of the function, player can fire rapidly at will but will introduce the problem when cleaning the bullets (bullet:removeSelf() ).
What is the better way of handling this and still able to remove each bullet?
Thanks!