Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Creating dynamic instance of objects

New Here ,
May 17, 2013 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

  var clickedShape:DisplayObject = event.target as DisplayObject;

  var clickedShape2:DisplayObject = new pat1_2mc();

  var clickedShape3:DisplayObject = new pat1_3mc();

TOPICS
ActionScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 17, 2013 May 17, 2013

use:

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

var c:* = new C();

Translate
Community Expert ,
May 17, 2013 May 17, 2013

what's your question?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 17, 2013 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 17, 2013 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 17, 2013 May 17, 2013

use:

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

var c:* = new C();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 17, 2013 May 17, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 17, 2013 May 17, 2013

in nut shell

i have

var clickedShape:DisplayObject = event.target as DisplayObject;

if i trace (clickedShape) output will be [object pat1_1]

now buy gettin the name of this object i do string manupulation and try to declare

var clickedShape2:DisplayObject = new pat1_2();

var clickedShape3:DisplayObject = new pat1_3();

i have to do this co the single clicked event will be used buy manny objects

pat2_1,pat2_2,pat2_3

pat3_1,...................

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2013 May 18, 2013

put that code in your event listener function.  use event instead of e, if that's what you use for your event instance.

anyInteractiveObjectThatIsExportedForActionScript.addEventListener(MouseEvent.CLICK,f);

function f(e:Event):void{

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

var c:* = new C();

// do whatever with c (another instance from the same class as the clicked instance)

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 18, 2013 May 18, 2013

Hey that's all right help me a bit and now you getting the poing,still not what i'm looking for.your code is good if i have same class defination.

but i'm working with 3 movieclips and they have 3 diff class defination.

I'll try my best to explain here again . thanks for your patience my frind.

there are 3 symbols   pat1_1, pat1_2, pat1_3 have same class name as their symbol(movie clip) name.

falsh.png

pat1_1mc is its instance name and i'm adding eventlistener to it.

pat1_1mc.addEventListener(MouseEvent.CLICK, pattile1);

now in onclick function bellow

function pattile1 (event:MouseEvent):void

{

         var clickedShape:DisplayObject = event.target as DisplayObject;  // making a copy(instance) of pat1_1 coz the one is clicked that's perfectly good.

     now based on that at first i was making two different instance of two diffeent symbol statically like bellow,

       both are two seperate symbols have diff class names and i'm making instances statically.

     var clickedShape2:DisplayObject = new pat1_2();

     var clickedShape3:DisplayObject = new pat1_3();

and you can see in picture i'm using 2 dimention loop to arrange these 3 instances on screen bu using addchild and .X .Y values.

this is all works quite nice.but this is for only one ,i want to now add more symbols and want to make it generelize. you can see 3 more tiles in gray shade on right side of picture.

-------------------------------------------------------------------------------------------------------------------------------------------------

now here i'm adding one more eventlinster to another sysmbol whitch is pat2_1  (so the patterne will be parX_1 where X=1 to n ) calling to the same function

which will do the same 1) create a new instance of the clicked event and use it to display

                                    2) create two more instance of two diff symbols 

                                          var clickedShape2:DisplayObject = new pat2_2();

                                          var clickedShape3:DisplayObject = new pat2_3();

so eventlistener will be added to instance of symbol will be created on Onclick patX_1  where (X=1 to n)
pat1_1pat1_2  pat1_3
pat2_1pat2_2  pat2_3
pat3_1pat3_2  pat3_3

notice all pat1_1  pat1_2 and pat1_3 are different movieclip have different class defination.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 18, 2013 May 18, 2013

if you want to create an instance for every class EXCEPT the clicked object, use an array:

var classA:Array=["pat1_1","pat2_1","pat3_1"];  //assuming these are your class names

yourclassinstance.addEventListener(MouseEvent.CLICK,f);

function f(e:Event):void{

var classS:String = getQualifiedClassName(e.currentTarget);

for(var i:int=0;i<classA.length;i++){

if(classA!=classS){

var C:Class=Class(getDefinitionByName(classA));

var c:* = new C();

}

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 20, 2013 May 20, 2013

hey thanks a ton,

I got exactly what i wanted,really apriciate you time and patience.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 20, 2013 May 20, 2013
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines