Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How do I get more than one of the same sprite. — Gideros Forum

How do I get more than one of the same sprite.

MinstrelMinstrel Member
edited June 2015 in General questions
Hello forums! I have a question that is so basic you'll probably laugh at it, but I know you will help:
How do I get more than one of the same sprite. And don't say repeatedly doing
(sprite) = Bitmap.new(Texture.new("ship.png"))
stage:addChild(playership1)
I mean writing one piece then if you were to say do:
(sprite):setScale(2.0)
It would change the scale of all the sprites.

Thanks in advance. :D

Edit: BTW The sprite is being made in an animation and ideally I would like all childs of the sprite to be animated.
Animate code:
local anim1 = {}
anim1[1] = Bitmap.new(Texture.new("invader2.png"))
anim1[2] = Bitmap.new(Texture.new("invader.png"))
 
local animation = MovieClip.new{
{1,40, anim1[1]},
{40,100, anim1[2]},
anim1[1]:setScale(0.3)
}
 
animation:setPosition(0,-5)
stage:addChild(animation)
animation:setGotoAction(100, 1)
 
animation:gotoAndPlay(1)

Comments

  • piepie Member
    you can write an Invader class inheriting from Sprite where you put the animation code, then with a for loop you place them all on screen.
    Invader = Core.class(Sprite)
     
    function Invader:init() --this method corresponds to Invader.new()
    --I assume the animation code is working
    local anim1 = {}
    anim1[1] = Bitmap.new(Texture.new("invader2.png"))
    anim1[2] = Bitmap.new(Texture.new("invader.png"))
     
    local animation = MovieClip.new{
    {1,40, anim1[1]},
    {40,100, anim1[2]},
    anim1[1]:setScale(0.3)
    }
     
    animation:setPosition(0,-5)
    self:addChild(animation) --adding animation to Invader instance, not to stage
    animation:setGotoAction(100, 1)
     
    animation:gotoAndPlay(1)
    end
     
    local maxInvaders  = 5 --how many invaders to place on screen
     
     
    local invaders = {} --as we did for bullets: an array collecting a serie of sprites references
    invadersLayer = Sprite.new() -- a sprite to put on stage, where you can add all your invaders together so that you can just edit this, and move/scale all sprites together
    stage:addChild(invaderLayer)
     
    local paddx = 50 --x distance between invaders
    for i=1, maxInvaders do --from 1 to maxInvaders, place on invadersLayer
    invaders[i] = Invader.new() --create one instance of Invader class
    invaders[i]:setX(paddx*i) --set its x position according to its "number" i
    invadersLayer:addChild(invaders[i]) --add this instance of Invader to invadersLayer
     
    end
    I didn't test this but I think it should work.
    I leave to you the code to place them on different lines :)
  • Do I write this in a separate .lua? And what do you mean by "I leave to you the code to place on different line"?
  • Ok NVM Prev comment. I used your code as a temp than made several tweaks to it: etc. all sprites moving across the screen scaling eg.

    Thanks for your time! :)
Sign In or Register to comment.