dcsimg
A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: dynamic instances names

  1. #1
    Junior Member
    Join Date
    May 2007
    Posts
    12

    dynamic instances names

    I've got a problem...

    I have a database of clients, updated via server side script, that generates an XML with a list of clients.
    From Flash CS3, I read it.
    The problem is:

    How should I create a Sprite for every client in the list that is clickable?
    The problem is the instance name. I can create the list only with actionscript (obviously) but the only thing that do not accept variables is the name of the sprite....
    Everything is inside a for loop, so I have the key to call them in different ways...but how?

    this is the code:

    Code:
    var numeroClienti:int = dataXML.item[0].quantita;
    		var posIniziale:Number = 0;
    		for (var i:int=1; i<= numeroClienti ; i++) {
    			//trace(dataXML.item[i].nome);
    			var nomeMovie:String = "cliente" + i;
    			var cliente:Sprite = new Sprite;
    			var nome:TextField = new TextField;
    			cliente.addChild(nome);
    			cliente.scaleX = 50;
    			nome.text = dataXML.item[i].nome;
    			nome.selectable = false;
    			cliente.x = 0;
    			cliente.y = posIniziale;
    			this.addChild(cliente);
    			cliente.addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
         spriteClicked(event, i);
    });
    			posIniziale += 20;
    		}
    	}
    function spriteClicked(event:MouseEvent,nCliccato):void {
    				trace('cliccato' + nCliccato);
    			}
    I would like to call every movie client1, client2 with the number given by the "i" in the for cycle....is there a way to do this?

    Thanks.

  2. #2
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    Im not sure if i understand your question, but you culd go two ways:
    1 create an array
    var clients:Array = new Array();
    and put the clients in
    I mean, instead of
    var cliente:Sprite = new Sprite;

    clients[i] = new Sprite();
    and replace cliente for clients[i] in the rest of the code

    The other way is to go
    this["client"+i] = new Sprite();
    and replace cliente for this["client"+i]

    I would rather go the first way cos arrays are faster and you can do more things withit

  3. #3
    Junior Member
    Join Date
    May 2007
    Posts
    12
    you've understand exactly what I mean.....
    I've chosen the first way, with array....
    so I've added this code that create an array with the names:

    Code:
    for (var a:int=1 ; a<=numeroClienti ; a++) {
    			clients[a - 1] = "cliente" + a;
    		}

    then I've replaced cliente with clients[i] in the for cycle...

    Code:
    for (var i:int=0 ; i< numeroClienti ; i++) {
    			//trace(dataXML.item[i].nome);
    			var nomeMovie:String = "cliente" + i;
    			clients[i] = new Sprite;
    			var nome:TextField = new TextField;
    			clients[i].addChild(nome);
    			clients[i].scaleX = 50;
    			nome.text = dataXML.item[i].nome;
    			nome.selectable = false;
    			clients[i].x = 0;
    			clients[i].y = posIniziale;
    			this.addChild(clients[i]);
    			trace(i);
    			clients[i].addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
         spriteClicked(event, i);
    });
    			posIniziale += 20;
    		}
    everything seems to work....BUT:

    look at this line....

    Code:
    clients[i].addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
         spriteClicked(event, i);
    });
    it should trigger the spriteClicked function with two arguments....the MouseEvent and the i.

    the function is:

    Code:
    function spriteClicked(event:MouseEvent,nCliccato:int):void {
    	trace('clicked' + nCliccato);
    }
    when I clic on a sprite it gives me back always "clicked16". Where 16 is exactly the number of elements of the list (and so the last number that i becomes at the end of the cycle). What is the problem?
    It seems like the eventListener is attached to every sprite on every loop of the cycle....but I use:

    Code:
    clients[i].addEventListener(.....);
    so what's the point???

    Thanks again (you rock ).

  4. #4
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    Cos in the end of the loop thats the value of i
    Now the bad news, i dont know how to solve that
    Im learning too, by the way

  5. #5
    Junior Member
    Join Date
    May 2007
    Posts
    12
    Quote Originally Posted by Cimmerian
    Cos in the end of the loop thats the value of i
    Now the bad news, i dont know how to solve that
    Im learning too, by the way
    what a mess.....

    anyway, if I'll find a solution I will post it here....
    If you have some idea....do the same....

    thanks....

  6. #6
    Flash Your Flex
    Join Date
    Apr 2007
    Location
    Provo, Utah
    Posts
    45
    I would need to look more in depth of it, but a quick solution would be to change clients[i] from a sprite to a movie clip and then give your clients[i] a var to keep for himself by saying clients[i].i = i, and then using that as a ref.

    Code:
    clients[i].i = i;
    clients[i].addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
         spriteClicked(event, event.target.i);
    });
    Last edited by malihk; 05-09-2007 at 12:34 PM.

  7. #7
    Junior Member
    Join Date
    May 2007
    Posts
    12
    Quote Originally Posted by malihk
    I would need to look more in depth of it, but a quick solution would be to change clients[i] from a sprite to a movie clip and then give your clients[i] a var to keep for himself by saying clients[i].i = i, and then using that as a ref.

    Code:
    clients[i].i = i;
    clients[i].addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
         spriteClicked(event, event.target.i);
    });

    argh.......it seems nice....never knowed about this type of variable....

    BUT

    it gives me an error.....ReferenceError: Error #1069: Impossible find property in flash.text.TextField and no default value.
    at MethodInfo-27()

    (it's a translation from my language)

    It seems like the compiler is looking for the i variable in the textfield....and not in the movieclip....

    What does it means?????


    (thank you again guys....)

  8. #8
    Flash Your Flex
    Join Date
    Apr 2007
    Location
    Provo, Utah
    Posts
    45
    did you change the object type of clients[i] from sprite to MovieClip?

  9. #9
    Junior Member
    Join Date
    May 2007
    Posts
    12
    sure....
    this is the complete code now....

    Code:
    function completeHandler(event:Event):void
    	{
    		var dataXML:XML = XML(event.target.data);
    		//trace(dataXML.toXMLString());
    		//trace(dataXML.item[0].nome);
    		var numeroClienti:int = dataXML.item[0].quantita;
    		var posIniziale:Number = 0;
    		var clients:Array = new Array();
    		for (var a:int=1 ; a<=numeroClienti ; a++) {
    			clients[a - 1] = "cliente" + a;
    		}
    		//trace(clients);
    		for (var i:int=0 ; i< numeroClienti ; i++) {
    			//trace(dataXML.item[i].nome);
    			var nomeMovie:String = "cliente" + i;
    			clients[i] = new MovieClip;
    			var nome:TextField = new TextField;
    			clients[i].addChild(nome);
    			clients[i].scaleX = 50;
    			nome.text = dataXML.item[i].nome;
    			nome.selectable = false;
    			clients[i].x = 0;
    			clients[i].y = posIniziale;
    			this.addChild(clients[i]);
    			clients[i].i = i;
    			//trace(i);
    			clients[i].addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
         spriteClicked(event, event.target.i);
    });
    			posIniziale += 20;
    		}
    	}
    function spriteClicked(event:MouseEvent,nCliccato:int):void {
    	trace('cliccato' + nCliccato);
    }

  10. #10
    Flash Your Flex
    Join Date
    Apr 2007
    Location
    Provo, Utah
    Posts
    45
    clients[i] = new MovieClip;

    should be clients[i] = new MovieClip();

  11. #11
    Junior Member
    Join Date
    May 2007
    Posts
    12
    oh...yes.....that was an error....

    but I've got the same problem.....

    The code (0.3)

    Code:
    var request:URLRequest = new URLRequest("listaclienti.xml");
    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, completeHandler);
    try
    {
    	loader.load(request);
    }
    catch (error:ArgumentError)
    {
    	trace("An ArgumentError has occurred.");
    }
    catch (error:SecurityError)
    {
    	trace("A SecurityError has occurred.");
    }
    function completeHandler(event:Event):void
    	{
    		var dataXML:XML = XML(event.target.data);
    		//trace(dataXML.toXMLString());
    		//trace(dataXML.item[0].nome);
    		var numeroClienti:int = dataXML.item[0].quantita;
    		var posIniziale:Number = 0;
    		var clients:Array = new Array();
    		for (var a:int=1 ; a<=numeroClienti ; a++) {
    			clients[a - 1] = "cliente" + a;
    		}
    		//trace(clients);
    		for (var i:int=0 ; i< numeroClienti ; i++) {
    			//trace(dataXML.item[i].nome);
    			var nomeMovie:String = "cliente" + i;
    			clients[i] = new MovieClip();
    			var nome:TextField = new TextField;
    			clients[i].addChild(nome);
    			clients[i].scaleX = 50;
    			nome.text = dataXML.item[i].nome;
    			nome.selectable = false;
    			clients[i].x = 0;
    			clients[i].y = posIniziale;
    			this.addChild(clients[i]);
    			clients[i].i = i;
    			//trace(i);
    			clients[i].addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
         spriteClicked(event, event.target.i);
    });
    			posIniziale += 20;
    		}
    	}
    function spriteClicked(event:MouseEvent,nCliccato:int):void {
    	trace('cliccato' + nCliccato);
    }

  12. #12
    Flash Your Flex
    Join Date
    Apr 2007
    Location
    Provo, Utah
    Posts
    45
    var nome:TextField = new TextField;

    should be

    var nome:TextField = new TextField();

  13. #13
    Junior Member
    Join Date
    May 2007
    Posts
    12


    still the same problem anyway.....

    Code:
    var request:URLRequest = new URLRequest("listaclienti.xml");
    var loader:URLLoader = new URLLoader();
    loader.addEventListener(Event.COMPLETE, completeHandler);
    try
    {
    	loader.load(request);
    }
    catch (error:ArgumentError)
    {
    	trace("An ArgumentError has occurred.");
    }
    catch (error:SecurityError)
    {
    	trace("A SecurityError has occurred.");
    }
    function completeHandler(event:Event):void
    	{
    		var dataXML:XML = XML(event.target.data);
    		//trace(dataXML.toXMLString());
    		//trace(dataXML.item[0].nome);
    		var numeroClienti:int = dataXML.item[0].quantita;
    		var posIniziale:Number = 0;
    		var clients:Array = new Array();
    		for (var a:int=1 ; a<=numeroClienti ; a++) {
    			clients[a - 1] = "cliente" + a;
    		}
    		//trace(clients);
    		for (var i:int=0 ; i< numeroClienti ; i++) {
    			//trace(dataXML.item[i].nome);
    			var nomeMovie:String = "cliente" + i;
    			clients[i] = new MovieClip();
    			var nome:TextField = new TextField();
    			clients[i].addChild(nome);
    			clients[i].scaleX = 50;
    			nome.text = dataXML.item[i].nome;
    			nome.selectable = false;
    			clients[i].x = 0;
    			clients[i].y = posIniziale;
    			this.addChild(clients[i]);
    			clients[i].i = i;
    			//trace(i);
    			clients[i].addEventListener(MouseEvent.CLICK, function(event:MouseEvent):void {
         spriteClicked(event, event.target.i);
    });
    			posIniziale += 20;
    		}
    	}
    function spriteClicked(event:MouseEvent,nCliccato:int):void {
    	trace('cliccato' + nCliccato);
    }

  14. #14
    Flash Your Flex
    Join Date
    Apr 2007
    Location
    Provo, Utah
    Posts
    45
    hehe, ok, let me pull it into my AS editor and I will get back to you in a minute

    *warning* I am going to crop your code down so I can work with it!

  15. #15
    Junior Member
    Join Date
    May 2007
    Posts
    12
    Quote Originally Posted by malihk
    hehe, ok, let me pull it into my AS editor and I will get back to you in a minute

    *warning* I am going to crop your code down so I can work with it!

    I don't know if "thank you" is enough!

  16. #16
    Flash Your Flex
    Join Date
    Apr 2007
    Location
    Provo, Utah
    Posts
    45
    Alright, here ya go. I didn't have all your XML and stuff so this will have to do. It has all the concepts and syntax that you should follow. Lemme know if you need anything else though.

    Code:
    init();
    function init():void{
    	var totalClients:int = 10;
    	var clients:Array = new Array();
    	for (var i:int = 0 ; i < totalClients ; i++) {
    		var clip:MovieClip = new MovieClip();
    		var circle = doDrawCircle();
    		clip.name = "cliente" + i;
    		clip.x = i * 25;
    		clip.i = i;
    		clients.push(clip);
    		clip.addChild(circle);
    		addChild(clip);
    		clip.addEventListener(MouseEvent.CLICK, handleMouseClick);
    	}
    }
    function handleMouseClick(e:Event){
    	trace(e.target.i);
    }
    function doDrawCircle():Shape {
    	var child:Shape = new Shape();
    	child.graphics.beginFill(0xFF0000);
    	child.graphics.lineStyle(0, 0x000000);
    	child.graphics.drawCircle(10, 10, 10);
    	child.graphics.endFill();
    	return child;
    }

  17. #17
    Flash Your Flex
    Join Date
    Apr 2007
    Location
    Provo, Utah
    Posts
    45
    btw, what language is that? Italian?

  18. #18
    Junior Member
    Join Date
    May 2007
    Posts
    12
    Quote Originally Posted by malihk
    btw, what language is that? Italian?

    yes it is!

    btw....I'm astounded....(like )


    the code is shorter, better, and mostly......IT WORKS!

    Now...the problem is that I have to create moviclips with textfields in it and not simple shapes....

    But I'll try and ask if I'm in trouble...

    again:

  19. #19
    Flash Your Flex
    Join Date
    Apr 2007
    Location
    Provo, Utah
    Posts
    45
    no worries! I am glad that I could help!

  20. #20
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    I dont understand one thing
    this:
    clip.i = i;
    should create something like that in, lets say, 10 movieClips
    clip.1 = 1;
    clip.2 = 2;
    clip.3 = 3;

    rigth?
    Now, how
    trace(e.target.i);
    knows what is the value of i?
    wich of those is

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center