Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Sprites/textures :Instantiate in a loop and load into an array — Gideros Forum

Sprites/textures :Instantiate in a loop and load into an array

oldschoololdschool Member
edited March 2012 in General questions
Hi,
I would like to create sprites inside a loop. Is this possible?
I've tried with jumping balls but got nowhere fast. I got attempt to index global

now I've tried with Hierarchy example and get the error main.lua:30: bad argument #1 to 'addChild' (Sprite expected, got nil)

Is what I want possible with sprites or textures? If not then how do you setup scenes with multiple instances of the same object quickly?
a={}
dot1tex = Texture.new("dot-1.png")
 
group1 = Sprite.new()
 
group1:addChild(Bitmap.new(group1tex))
 
for i=0,3 do
	for j=0,3 do
		local dot = Bitmap.new(dot1tex)
		dot:setPosition(i * 45 + 10, j * 45 + 60)
		a[i]=group1:addChild(dot)
	end
end
 
group1:setPosition(10, 10)
for i=0,3 do
stage:addChild(a[i])  -- line 30
end

Comments

  • ar2rsawseenar2rsawseen Maintainer
    group1:addChild(dot) does not return sprite, you should do
    group1:addChild(dot)
    a[i]=dot
    for it to work, but all in all, you can simply add whole group to the stage
    stage:addChild(group1)
    And second loop is not needed
  • evsevs Member
    Hello,

    Remember Lua table indexing starts at 1 and not at 0


    Cheers

    evs
Sign In or Register to comment.