Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Array of sprites - Need help — Gideros Forum

Array of sprites - Need help

sdouglassdouglas Member
edited August 2012 in General questions
Hi experts - I am new to Lua and Gideros. I come from a C/Java background where I could have an array of datatypes. I am really struggling with LUA. Can someone point me in the right direction. Heres what i have so far. #WISH# debugger support!
drop={} -- The dropboard 
dTile=gideros.class(Sprite)-- Create a new drop class
dropgroup=dTile.new() -- I hav a class dTile.lua but did not include for this example
dbt = dTile.new("gfx/bluetile.jpg")
drt = dTile.new("gfx/redtile.jpg")
dyt = dTile.new("gfx/yellowtile.jpg")
dgt = dTile.new("gfx/greentile.jpg")
dpt = dTile.new("gfx/player.jpg")
 
for i=1,4 do
      drop[i] = {}     -- create a new row
      for j=1,4 do
		drop[i][j] = dTile.new 
		r=math.random(5)-- Get a random number then a color to use
		if r == 1 then drop[i][j] = drt drop[i][j].color =1 elseif
			r == 2 then drop[i][j] = dbt drop[i][j].color =2 elseif 
			r == 3 then drop[i][j] = dyt drop[i][j].color =3 elseif 
			r == 4 then drop[i][j] = dgt drop[i][j].color =4 elseif
			r == 5 then drop[i][j] = dpt drop[i][j].color =5 
		end
 
 
                drop[i][j]:setX(j * 48)
		drop[i][j]:setY(i * 24)	
	        drop[i][j].occ=1 -- Occupied or not 
		h=drop[i][j]:getHeight()
		w=drop[i][j]:getWidth()
		x=drop[i][j]:getX()
		y=drop[i][j]:getY()
                color = drop[i][j].color
 
		dropgroup:addChild(drop[i][j]) -- I cant seem to grasp how to add these objects to the group
		print( i,j, "Color", color, "h=", h, "w=", w, "x=",x, "y=",y, "Occ=",occ, drop [i][j]) --#WISH Debugger 		
	  end
end
 
 
----and then add them all to stage
stage:addchild(dropgroup)

Comments

  • Hello @sdouglas

    sorry but without dTile set we can't also test it. From the first look can't find anything wrong. Can you at least share what error do you get, or what is not working correctly? :)
  • @sdouglas,
    what is returned when you call the dTile.new() ?? Hopefully that is a Bitmap object.

    Secondly, you are trying to create 16 display objects from 4. you will need to create 16 objects. Otherwise the same object is just repositioned.

    Another thing that baffles me is, the line
    drop[i][j] = dTile.new
    this sets the contents of the drop array to the function dTile.new instead of a blank dTile object.
    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
  • wow nice catches @OZApps. You've got an eye for this kind of thing ;)
  • @ar2rsawseen, thanks for the complement.

    it is just years of being with IT ;)
    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
  • sdouglassdouglas Member
    edited August 2012
    Thanks guys I removed the .new from drop[i][j] The error I get at stage:addchild(dropgroP)(sorry should have mentioned this) is main.lua:212: attempt to call method 'addchild' (a nil value)
    stack traceback:

    I guess in a nutshell I am looking for the correct way to display say 64 sprites on a screen at the same time and be able to access variables for each sprite in a array. Each sprite in the 64 group would be assigned of of the 6 availbale bitmap images.



    and here is the dTile object - nothing pretty
     
    --=================
    --- tiles class --
    --=================
    dTile = gideros.class(Sprite)
     
     
    --=================
    -- init tiles class 
    --=================
    function dTile:init(texture)
     
    	local bitmap = Bitmap.new(Texture.new(texture))
    	self:addChild(bitmap)
    	--print("dTile New")
    	--self:setX(1)
    	--self:setY(40) 
    	self.width = self:getWidth()
    	self.height = self:getHeight()
    	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
     
    end
     
    function dTile:onEnterFrame(event)
    	-- before entering each frame, we update the position 
    	-- self:bounce() -- Only way to call a function inside an object class
    	-- add code if drop tile or move tile
     
    	local x,y = self:getPosition()
    	--print("Enter Frame xy ", x,y)
    	y= y+1
    	self:setPosition(x, y)
     
    end--
  • I figured it out. Note to self! Never come off a marathon coding session in a language your fluent in, then try to learn a new language brute force method in 3 hours.
    ...
    drop[i][j] = dTile.new("gfx/redtile.jpg")
    ..
    iterate thru and you have a wonderfull array of objects.

    Doog
  • @SDouglas

    :)

    Glad you got it working...

    Just a few tips in case you want
    If you want to simply move an object on one axis, then rather than setting the position using setPosition(), you can just use the setY() as
    self:setY(self:getY() + 1)
    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
  • Just to follow on this slightly..
    I have a group of Sprite, and then I group:addchild(childsprites)
    But when I do eventlistener, I get the group sprite properties, whats the right method to get the childsprite properties?
    REAL programmers type copy con filename.exe
    ---------------------------------------
  • ar2rsawseenar2rsawseen Maintainer
    @Cyberience could you show us the code you use to add event listener?
    You probably add it to the whole group and not each individual child right?
  • bowerandybowerandy Guru
    edited March 2013
    #WISH# debugger support!
    @sdouglas, there are several options for debugger support. Here's a free one: ZeroBrane Studio.

    best regards
  • CyberienceCyberience Member
    edited March 2013
    Yes your right I go it assigned to the wrong group, found that, but I am still stuck between the two!

    Like this, I move the group around with an event on the group.
    But if I dont move, just select, I want the individual sprite to respond.
    Where I am getting stuck is passing through an event from the parent to the child.
    I can put some sample code if it helps.

    Typical, the minute I ask the question I find the answer.
    I found that, event:stopPropagation() stops the events traveling up the tree, when I remove this, all the events trigger and I can solve my problem.
    REAL programmers type copy con filename.exe
    ---------------------------------------
Sign In or Register to comment.