Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Adding and removing multiple Line shape on the stage — Gideros Forum

Adding and removing multiple Line shape on the stage

Sush19Sush19 Member
edited August 2013 in General questions
Hello all, I wanna create a scale kind of thing, in which there is a horizontal line and below this line there are many small vertical lines, which forms scale kind of thing.

To create this I've written following code:
       function newLine(xPos, yPos, x, y, thikness)
		line = Shape.new()
		line:setLineStyle(thikness, 0x000000, 1)
		line:beginPath()
		line:moveTo(0,0)
		line:lineTo(x, y)
		line:endPath()
		stage:addChild(line)
		line:setPosition(xPos, yPos)
	end
 
        --for horizontal line
	newLine(0, 255, 480, 0, 1)
 
	--for vertical lines
	newLine(150, 256, 0, 5, 2) --line for 0
	newLine(190, 256, 0, 3, 2) --line for 0.5
	newLine(230, 256, 0, 5, 2) --line for 1
	newLine(270, 256, 0, 3, 2) --line for 1.5
	newLine(310, 256, 0, 5, 2) --line for 2
	newLine(350, 256, 0, 3, 2) --line for 2.5
	newLine(390, 256, 0, 5, 2) --line for 3
	newLine(430, 256, 0, 3, 2) --line for 3.5
	newLine(470, 256, 0, 5, 2) --line for 4
 
and so on...
I want to clear the stage, So can anyone help me out whether what I'm doing is correct, and how to remove all the lines at once from the stage...

There is only one variable 'line', when i say
stage:removeChild(line)
it removes only the last drawn line from the stage....


Hope my question is clear to all

thank you...

Comments

  • In each line newLine call you create another Shape object and overwrite previous one inside line variable.
    That is why when you remove it from stage, only last one is removed, because line holds reference to the last created Shape object.

    you could create lineLayer and add all lines to it, then remove this line layer from Stage like this:
    local lineLayer = Sprite.new()
    stage:addChild(lineLayer)
     function newLine(xPos, yPos, x, y, thikness)
    	line = Shape.new()
    	line:setLineStyle(thikness, 0x000000, 1)
    	line:beginPath()
    	line:moveTo(0,0)
    	line:lineTo(x, y)
    	line:endPath()
    	lineLayer:addChild(line)
    	line:setPosition(xPos, yPos)
    end
     
    --and to remove
    stage:removeChild(lineLayer)
  • Thank you for the reply.....
    similar king of code i was searching for...

    thanks alot =D>
  • john26john26 Maintainer
    Also, if you don't have a reference to a sprite you can access by index, so to delete all sprites on stage you can do
       for i=1,stage:getNumChildren() do
           stage:removeChildAt(1)
       end
    each iteration of the loop removes the first child on the stage until none are left.
  • Hello @john26, thank you for your response.... :)
Sign In or Register to comment.