Skip to main content
Participating Frequently
May 17, 2013
Answered

Creating dynamic instance of objects

  • May 17, 2013
  • 3 replies
  • 1307 views

Ok so i want to create  instace of the two different  object at onclick event of the first object

normally it was

  var clickedShape:DisplayObject = event.target as DisplayObject;

  var clickedShape2:DisplayObject = new pat1_2mc();

  var clickedShape3:DisplayObject = new pat1_3mc();

This topic has been closed for replies.
Correct answer kglad

use:

var C:Class = Class(getDefinitionByName(getQualifiedClassName(e.currentTarget)));

var c:* = new C();

3 replies

akki209Author
Participating Frequently
May 17, 2013

Ok so i want to create  instace of the two different  object at onclick event of the first object

normally it was

this code is in onclick event function of an instance of pat1_1()

  var clickedShape:DisplayObject = event.target as DisplayObject; (pat1_1() clicked object )

  var clickedShape2:DisplayObject = new pat1_2();

  var clickedShape3:DisplayObject = new pat1_3();

so i want to create two more instance at on click event wich will be

always onclick will be of index 1 coz that's the only is on stage.and that click will create two more instace of index 2 and 3, ie.

pat2_1();  (clicked obj)

pat2_2();

pat2_3();

pat3_1();  (clicked obj)

pat3_2();

pat3_3(); ......... etc

si at first it was only one object but now for multileple object i was trieng to get the name of the clicked object and using chatAt[] amd other string mauplation function to get the desiered pattern(Code Bellow),

var clickedShape:DisplayObject = event.target as DisplayObject;    (Creating instance of clicked object)

     

                   var pattern:RegExp = /_/g;                     ()creating reg exp to change value after "_")

                    var str:String = clickedShape.name;      // pat1_1

                    var str2:String;

                    var str3:String;

                  //  trace(str.search(pattern));                   

                    var num:Number = str.search(pattern);    //   for "pat1_1" it'll be index 4

                    trace(str);                                                   // pat1_1

                 

                              str2=str.replace(str.charAt(num+1), "2");  // this will change the 5th element to 2 so i'll get pat2_2

                             

                              trace(str2);                                              pat2_2

                              //var clickedShape2:DisplayObject =  new pat1_2();  // original static declaration whi was working

                                var clickedShape2:DisplayObject = new str2();       // dynamic declaration where i'm getting error.

                         // now i'm getting error at compile time (i'm not sure why coz this is in on clikc dunction) but still it says

Scene 1, Layer 'Layer 1', Frame 1, Line 811180: Call to a possibly undefined method str2.

  and i understand that str 2 is string i have to conver/type cast it to obj to make it's new copy

                    so i tried this

                    var str_obj:Object = str2;

                       Still dident work,

                         Please tell me what i'm doing it wrong.

                      

                         var clickedShape2:DisplayObject = new str_obj();     

         

                 Thanks You.

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
May 17, 2013

use:

var C:Class = Class(getDefinitionByName(getQualifiedClassName(e.currentTarget)));

var c:* = new C();

akki209Author
Participating Frequently
May 18, 2013

I'm sorry but i diddnet get it,

by this code i get the class defination and that i understand but it the same out put if i trace it for

var clickedShape:DisplayObject = event.target as DisplayObject;

and 

var C:Class = Class(getDefinitionByName(getQualifiedClassName(e.currentTarget)));

var c:* = new C();

Output:

[object pat1_1]

[object pat1_1]


and then if i do string manupulation it'll be the same thing.

or do you want mto use this after i get desired string and then use to decalre new pat1_2 object.

in simple words:

i'm trying to get the name of the clicked object and just changing its last char to get two name of two new movie clip name and then declaring those two. i have to do this because i'm dealing with mutiple object who has a set of 3 so i have to g odynamic.

hope this will help you.

AgainThanks.

Ned Murphy
Legend
May 17, 2013

That code only creates new objects and has no relatiionship to clicking other than the naming.  What part of what you are trying to do are you having a problem with?

kglad
Community Expert
Community Expert
May 17, 2013

what's your question?