Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How do I get 2 projectiles at once — Gideros Forum

How do I get 2 projectiles at once

MinstrelMinstrel Member
edited June 2015 in General questions
Hello! I have a bit of a query. I'm making a space invaders type game and iv'e got one bullet firing from my ship but when i press the fire button (Z) repeatedly the bullet shoots comes back and then shoots again. What I need to know is how to make it so I can fire one bullet then fire another straight away without the first one coming back. Here is my code:

stage:addEventListener(Event.KEY_DOWN,function(e)
if e.keyCode==KeyCode.LEFT then
left=true
playerDir=-1
elseif e.keyCode==KeyCode.RIGHT then
right=true
playerDir=1
elseif e.keyCode==KeyCode.Z then

print("fire")
bx,by=playership1:getPosition()
bullet:setPosition(bx,by)
bDir=-1

end

end)

I've been told I need to use an array but I am pretty in experienced with gideros. If you could help and tell me what to do I would really appreciate it.
Thanks for your support! :D

Likes: SinisterSoft

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • piepie Member
    Hi, @Minstrel
    I think that you have only one instance of bullet at a time, so everytime you press Z you reset its position.
    elseif e.keyCode==KeyCode.Z then
     
    print("fire")
    bx,by=playership1:getPosition()
    bullet:setPosition(bx,by) --here you call a global bullet instance and set its position "back to the ship"
    bDir=-1
     
    end
    What I would do is create a new bullet instance (or better, draw it out from a pool http://giderosmobile.com/forum/discussion/4769/simple-item-pool-class/p1) everytime Z is pressed.

    You can use an array to keep track of your bullets: every lua table is an array. You just need to name each bullet with a unique name (you could use a counter, or os.timer() ) and then you can "access" the bullet (as long as you can access the table that contains it).

    assuming that somewhere before you placed a "bullets" array and a counter,
    bullets = {} 
    counter = 1
    I believe it should look almost like this:
     
     
    elseif e.keyCode==KeyCode.Z then
     
    print("fire")
    bx,by=playership1:getPosition()
    bullets[counter] = bullet.new() --create a new bullet instance (or draw it from pool) named as a number (counter)
    stage:addChild(bullets[counter]) --if you're adding the bullets to stage
    bullets[counter]:setPosition(bx,by) 
    bDir=-1
     
    counter = counter+1 --increase the counter for the next bullet
     
    end
    you will have to remove each bullet at the end of its course.

    However, have you noticed this raw space invader clone by SinisterSoft? it may give you some hints or ideas :) http://giderosmobile.com/forum/discussion/5615/space-invader-clone/p1



    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • freesspfreessp Member
    edited June 2015
    First off, I think your code is telling the same "bullet" to reposition itself every time you press the Z key. You need to produce a new "bullet" instance each time you press the key. You can do this with an array (table) with bullet1, bullet2, etc., or you can try creating an external class (.lua file) that is a sprite, then you can call that class everytime you press the button. Sorry I don't have more time to go into detail right now, but maybe that will steer you in the right direction.

    Edit: @pie must have hit enter before me. He provided much more detail than me too.

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • Thank you I have been able to resolve the issue and my project now works fully as intended.
    Thanks! :D
    BTW I checked out the sinister soft thing and it turns out the person who runs sinister soft is the person who often helps me with coding. his son goes to the same school as me and we are quite close friends.

    Likes: SinisterSoft

    +1 -1 (+1 / -0 )Share on Facebook
  • SinisterSoftSinisterSoft Maintainer
    edited June 2015
    @Minstrel Hi, great to see you are getting on with coding outside of Code Club!

    (Everyone else: Thanks for helping 'Minstrel', he is new to Gideros and I know that any help you have given will be greatly appreciated. :) )
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • Paulo777Paulo777 Member
    edited February 2018
    I don't know if it's on time, but it is simpler than you think.

    Firstly you can create a separate class only for that, that you may reuse it for other purposes. This sprite (class) must have initial x and y (kick off positions). And you manage its movement inside an enter_frame event, like:
    y = y - 1
     
    fire:setY(y)
     
    -- this is for the bullet go up each frame, this inside an enter frame event.
    you can set your 'init' sprite like this
    function fire:init(x, y)
    When you're about to call your fire, just create an instance inside an enter_frame event, then you manage the best way to 'shoot' your fire.

    in the objects that will 'fire' you can create specific 'shoot' behaviours.
  • @Paulo777 dude.. the thread is three years old.. it's so not on time :D
  • OMG! I noticed it is long time but not too long :D
    Nevermind it may help others :D
    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.