Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Shoot Again Not Working (I am a beginner) — Gideros Forum

Shoot Again Not Working (I am a beginner)

JDoefJDoef Member
edited June 2014 in General questions
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

  • PaulHPaulH Member
    You probably want to set xLaser and yLaser back to a starting position when a shot is started. As it is now you first shot will make xLaser increase until it exceeds MaxX. Your second shot will just increase it more, but it's already greater than MaxX so it it will be off the screen before you even see it. So after Laser:setVisible(true) you'll want to do something like xLaser = LaserStartX and yLaser = LaserStartY, where LaserStartX and LaserStartY define where you want each shot to start from.

    Paul
  • OZAppsOZApps Guru
    Accepted Answer
    @JDoef, since you are shooting from Left to Right on the screen, you can also use
     if shoot == true then
      xLaser = xLaser + 5
     end 
     
     Laser:setX(xLaser)
    and you can change that 5 to a variable like
    speed
    so that if you had a powerup, you can speed it up or slow it down by simply altering the speed variable somewhere else.

    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
    Laser:setPosition(50, MaxY / 2 - 15)
    with
    xLaser = 50
    yLaser = MaxY/2-15
    Laser:setPosition(xLaser, yLaser)

    Likes: JDoef

    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    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
    +1 -1 (+1 / -0 )Share on Facebook
  • JDoefJDoef Member
    Thanks @OZApps and @PaulH!

    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.
  • talistalis Guru
    edited June 2014
    @JDoef what you need to do is write a class called bullet.lua .
    Bullet = Core.class(Sprite)
     
    function Bullet:init(picture)
    --add bullet to stage and set intial values of its properties like shooting
    self.shooting = false
    self.image= Bitmap.new(Texture.new(picture))
    self.x= 100
    self.y=100
    self.speed=10
    --and so on..
     
    --later define event listener Enter_frame
    self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)	
     
    end
     
    --define onenterframe function in bird.lua again
     
    function Bird:onEnterFrame()
     if self.shooting == true then
      self.x = self.x + 5
     
     end 
     
      self:setX(self.x)
    end
    After bullet.lua finished, in your main.lua in button click or whenever you need some Laser or bullet just create one instance like:
    local bulletimage="bulletimage.png"
     
    -- lets add two bullets
    local bullet1 = Bullet.new(bulletimage)
    local bullet2 = Bullet.new(bulletimage)
     
    -- add birds to the stage
    stage:addChild(bullet1)
    stage:addChild(bullet2)
     
     
    --you can easilly change the logic of adding bullets to a fuction also. For example create one more function like add bullet.
    Note: I write the code from my mind without any gideros so don't expect to be fully working i just try to give you a general idea of OOP programming in Gideros. To undertsand the logic better you can check birds example in the Gideros examples.
  • JDoefJDoef Member
    So adding that would allow me to shoot my laser more than once before it hits the edge of the screen?
  • PaulHPaulH Member
    Yes, with the code Talis suggested you'd be ready to do this:

    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

  • JDoefJDoef Member
    Laser1 = Core.class(Sprite)

    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?
  • PaulHPaulH Member
    What do you mean by "not working"?

    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
  • JDoefJDoef Member
    I can only shoot once then once the laser is of the screen I can shoot again, everything builds and no errors, just no second laser when I press the fire button.
  • PaulHPaulH Member
    Can you post the code that you execute when the fire button is pressed?
  • iWattsiWatts Member
    Josh, I figured out how to do it the way were used to after about an hour and a half of attempts. Basically you have to create a shooting flag, and add one to the flag after every shot. then you can change the max and what not. Im still working on it, cause it glitches once and a while but ill show you tomorrow in class.
  • @JDoef,
    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.
    IMG_2892.jpg
    1204 x 806 - 213K

    Likes: HubertRonald

    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    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
    +1 -1 (+1 / -0 )Share on Facebook
  • JDoefJDoef Member
    edited December 2014
    Here @PaulH
    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?
  • PaulHPaulH Member
    It looks like you're not setting xLaser and yLaser to the coordinates where you want each shot to start from. You're just moving them further in the direction they already moved. Rather than xLaser = xLaser + 5 (which just moves it further to the right, even though it's already off the right side of the screen) you'll want to set xLaser and yLaser to an appropriate starting point. Perhaps that's where you initially set them before the game loop started...

    PaulH
  • JDoefJDoef Member
    @PaulH I have this ouside my function right after I load all my images:

    local yLaser = MaxY / 2 - 15
    local xLaser = 50

    Won't that set the start point of the laser?
  • PaulHPaulH Member
    Yes, for the first shot, and it sounds like the first shot works fine. The problem is that code gets called once at the very start of the game, and never again. You fire once and the shot starts there and moves off the screen. You fire again and the shot resumes moving, but it's already off the screen.

    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
  • Thanks everyone for all the help!
Sign In or Register to comment.