Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Array help — Gideros Forum

Array help

iWattsiWatts Member
edited June 2014 in Code snippets
I have a game where you basically have two "water" bullets to put out fire coming out of windows. I set it up so that the whole system works with on bullet/ variable called water1. What would I have to do if I wanted to add a second water bullet that performs the exact same? so that the user can have two bullets on the screen at a time.

here is the code I use for the current bullet:
-- Create Water
local water1 = Bitmap.new(Texture.new("Water.png"))
local water2 = Bitmap.new(Texture.new("Water.png"))


water1:setScale(.015)
water1:setPosition(0,0)
stage:addChild(water1)
shoot = false

if shoot == false then

water1:setVisible(false)
water1:setPosition(20,water1:getY())
water1.xSpeed = 0
water1.ySpeed = 0

end
if shoot == true then


water1:setY(hose:getY() + 5)
water1.xSpeed = 2
water1:setVisible(true)
water1X = water1X + water1.xSpeed
water1:setX(water1X)

end

Comments

  • PaulHPaulH Member
    edited June 2014 Accepted Answer
    You're on the right track. You would just need to do the same steps (setScale, setPosition, addChild, etc) with water2 as you do with water1.

    One of the benefits of Lua and Gideros is that you don't really need to have a variable for every bitmap in the scene, like water1, water2, etc. You can just create a new one and add it to the stage. You may find it's helpful to create a new class, maybed called WaterBullet, that uses the Bitmap base class, and it wouldn't hurt to use the same texture to create the bitmap for each, just to save memory. You might try something like this, and call it WaterBullet.lua:

    -------------------------------------
    WaterBullet = Core.class(Sprite)

    function WaterBullet:init(texture)
    self.bitmap = Bitmap.new(texture)
    self:addChild(self.bitmap)
    self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
    end -- of init()

    function WaterBullet:onEnterFrame()
    -- Do all the stuff you want to do every frame for your bullet, like
    -- move it, detect if it hit the fire, etc.
    -- If the bullet has left the screen or hit something and you want to
    -- get rid of it,
    if (???) then -- however you want to detect the bullet hitting
    -- something or timing out, etc.
    self:remove() -- Take the bullet off the stage, etc.
    end -- of if it's time to remove this bullet
    end -- of onEnterFrame()

    function WaterBullet:remove()
    -- Note that we have one fewer active bullets now:
    number_of_active_bullets = number_of_active_bullets - 1

    -- take this bullet off the stage so we don't see it after this:
    stage:removeChild(self)
    collectgarbage() -- so the system can free up the memory for a bullet that's
    -- no longer on the stage, and to keep bullets that are no longer part of the
    -- scene from hitting things, etc.
    end -- of function remove()
    -------------------------------------


    In your main module you would add new bullets something like this, possibly in a function called "shoot" just to keep things organized:

    function shoot()
    if number_of_active_bullets == nil then
    number_of_active_bullets = 0
    end -- of if we haven't set number of bullets yet

    -- Note: just change the 2 below to allow more bullets at once
    if number_of_active_bullets < 2 then
    -- Keep track of how many bullets are on the stage:
    number_of_active_bullets = number_of_active_bullets + 1

    -- Get the texture for the water bullet if we haven't already:
    if water_texture == nil then
    water_texture = Texture.new("Water.png")
    end -- of if we don't have the water texture yet

    -- Make a new bullet using the existing texture:
    local new_bullet = WaterBullet.new(water_texture)
    new_bullet:setPosition(???) -- plug in the position of your hose
    new_bullet:setScale(???) -- whatever scale you like

    -- Set your speed, alpha and anything else you want for the new bullet here.

    stage:addChild(new_bullet)
    end -- of if we are allowed another bullet
    end -- of shoot()

    Hope that helps,

    Paul

    Likes: Harrison, iWatts

    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.