Alright, so I have this code in my main drawFrame event:
if shoot == true then -- If the shoot button is pressed
xLaser = xLaser + 5
end
Laser:setPosition(xLaser, yLaser)
if xLaser > MaxX then -- if the laser goes off the screen
shoot = false
Laser:setVisible(false)
Laser:setPosition(50, MaxY / 2 - 15)
end
And this in my ButtonClick event:
if shootBtn:hitTestPoint(x, y) then -- if shoot button is pressed
shoot = true
Laser:setVisible(true)
xLaser = xLaser + 5
yLaser = by + 10
end
Laser:setPosition(xLaser, yLaser)
Yet I can only fire the laser or bullet one time then I have to restart the program.
Any advice?
Comments
Paul
As for the issue, PaulH has replied to that, in your score that checks for the bullet having gone off-screen, you are setting the bullet to 50, MaxY/2 - 15 but your xLaser and yLaser are still whatever they are, xLaser being off screen (> MaxX). So replace the line
Likes: JDoef
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
Is there a way to get it so I can fire more than one laser before the laser goes off screen? Because I plan on adding objects that fly towards the Blaster and the player has to shot them.
stage:addChild(Bullet.new(bulletimage))
whenever the player wants to fire the laser, as many times as you like, and each would would create a new bullet and add it to the scene. Of course somewhere you'll still need to handle detecting a bullet going off the screen and removing that from the scene, and checking for hits, etc., but the technique above allows you to add all the bullets you want.
Paul
function Laser1:init(Laser1)
--add bullet to stage and set intial values of its properties like shooting
shoot = false
Laser1 = Bitmap.new(Texture.new("Laser.png"))
Laser1:setPosition(50, MaxY / 2 - 15)
stage:addChild(Laser1)
Laser1:setVisible(false)
--later define event listener Enter_frame
Laser1:addEventListener(Event.ENTER_FRAME, Laser.onEnterFrame, self)
end
--define onenterframe function in bird.lua again
function Laser1:onEnterFrame()
if shoot == true then
xLaser1 = xLaser1 + 5
end
Laser1:setX(xLaser1)
end
I added that to a new .lua file but is not working...sorry I am really new, any advice?
Make sure the new lua file is included in your Gideros project.
Make sure everything builds.
Then try running it. Do you get an error message in the console indicating what went wrong?
PaulH
if you got a chance to read my book then you might have seen one probable solution to your question on page 357 and 358 under the section Shooting Bullets. This can be seen in the sample game Chopper Rescue also available on the app stores for free.
You can create self contained bullets that move and destroy themselves automatically and could raise events when they collide with something or you can have a main loop that manages all of that. There are advantages and disadvantages of both.
Likes: HubertRonald
Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
Cool Vizify Profile at https://www.vizify.com/oz-apps
if shootBtn:hitTestPoint(x, y) then
shoot = true
Laser:setVisible(true)
xLaser = xLaser + 5
yLaser = by + 10
end
Laser:setPosition(xLaser, yLaser)
Alright @iWatts
@OZApps where can I find your book?
PaulH
http://www.apress.com/9781430246626
[-] Liasoft
local yLaser = MaxY / 2 - 15
local xLaser = 50
Won't that set the start point of the laser?
To fix this you need to repeat those lines:
xLaser = 50
yLaser = MaxY / 2 -15
You need to do that again every time you want the laser beam to start over, acting like it's a new laser beam the player created by firing again.
Paul