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

Multiple instances of one object ?

LEGEND ,
Mar 25, 2008 Mar 25, 2008
Hi,

Does anybody know how to create many instances of loaded object? I need to
add preloaded image from external source (Loader class) to many MCs (using
addChild()). When I call method addChild() do second object, image
dissapears from first object because its name of instance is exactly always
the same. In order to avoid this problem I decided to clone this image each
time I adding it to MC. Unfortunately I don't know how to create another
instance of image without repeating loading it again from the network.

Thanks for the help,
Marek


TOPICS
ActionScript
1.5K
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
Enthusiast ,
Mar 26, 2008 Mar 26, 2008
Hi,
you can store instanced clip in array. Than you can do what you want with instances.
I look into your site and see wath the problem is. The way to solve it dependent of implementation of animation.
If I must do this effect I will make a dummy MovieClip ( to store the chossen bitmat ) and make a MovieClip( for example: animClip ) with this effect. When a bitmap is choosen i will create a instance of animClip and store its reference into array( if I need to hold this reference for some reason ). Then I will place a bitmap in dummy clip. Into last frame of animation I will call a function ( or rise an event ) to now which animation is ended.
I think you do someting like this.
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 ,
Mar 26, 2008 Mar 26, 2008
Hi Kalisto,

> Hi,
> you can store instanced clip in array. Than you can do what you want with
> instances.
> I look into your site and see wath the problem is. The way to solve it
> dependent of implementation of animation.

Did you mean:

arr=new Array();
arr.push(myMc);
arr.push(myMc);
arr.push(myMc);
...

I think that it will not working because
arr[0].name==arr[1].name==arr[2].name...
All cells of array will refer to the same MC with the same instance name.


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
Enthusiast ,
Mar 26, 2008 Mar 26, 2008
no I mean something like this
private var m_arr: Array = new Array(); ( m_ for member of class )

private function attachClip( animMC: MovieClip )
{
var mc:MovieClip = MovieClip( this.addChild( animMC ) ); // You may not cast if you no need of MovieClip than use displayObject as type for mc;
m_arr.push( mc );
}


animMC is off-screen element ( if you find this way heplful I think I can write a function returns animMC )

Of course this is an example for my previous post. I don't know how you make animations and for this reason i may confuse you instead helping 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
LEGEND ,
Mar 26, 2008 Mar 26, 2008
> private function attachClip( animMC: MovieClip )
> {
> var mc:MovieClip = MovieClip( this.addChild( animMC ) ); // You may
> not
> cast if you no need of MovieClip than use displayObject as type for mc;
> m_arr.push( mc );
> }
>
> Of course this is an example for my previous post. I don't know how you
> make
> animations and for this reason i may confuse you instead helping you

The MovieClip class constructor has no parameters so I can't use it as a
method of clonning MCs.

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/display/MovieClip.html#MovieClip()


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
Enthusiast ,
Mar 26, 2008 Mar 26, 2008
HI, please tell me you what you mean under "clone"? In AS3 every item in library is associated with class ( can be custom class or can be created automatically by flash ). You may have many INSTANCES of this class in stage with different names.
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 ,
Mar 27, 2008 Mar 27, 2008
> HI, please tell me you what you mean under "clone"? In AS3 every item in
> library is associated with class ( can be custom class or can be created
> automatically by flash ). You may have many INSTANCES of this class in
> stage
> with different names.


Ok. Lets analyse example below. loadFileClass will be a class loading, for
example, picture file with passed as argument name.

class myClass extends MovieClip
{
private var myState:Number;

public function setState(state:Number)
{
var externalFileObject;
myState=state;
externalFileObject=new loadFileClass("file"+myState.".jpg");
addChild(externalFileObject);
externalFileObject=new loadFileClass("file"+(myState+1).".jpg");
addChild(externalFileObject);
}

public function myClass()
{

}
}

var mcB, mcA=new myClass();
mc.setState(5);

mcB=clone(mcA);

I expect here to have mcB object having the same attached another object,
having the same myState value but with different instance name.


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 ,
Mar 28, 2008 Mar 28, 2008
The OOP way of cloning is to have the Object clone itself. Using your
example:

public interface ICloneable
{
function clone():Object;
}

class myClass extends MovieClip implements ICloneable
{
private var myState:Number;

public function setState(state:Number)
{
var externalFileObject;
myState=state;
externalFileObject=new loadFileClass("file"+myState.".jpg");
addChild(externalFileObject);
externalFileObject=new loadFileClass("file"+(myState+1).".jpg");
addChild(externalFileObject);
}

public function myClass()
{
}
public function clone():Object
{
var result:myClass = new myClass();

// set member values

// in this case, copying myState to the clone would
// not produce the desired effect, so we'll call the setState
// method.
result.setState(myState);

return myClass;
}
}


var mcB, mcA=new myClass();
mcA.setState(5);

mcB = myClass(mcA.clone());

//or

var mcB:MovieClip
var mcA:MovieClip = new myClass();
mcA.setState(5);

mcB = (mcA is IClonable) ? MovieClip(IClonable(mcA).clone()) : new
myClass();




Another look at cloning in AS3:
http://niko.informatif.org/blog/2007_07_20_clone_an_object_in_as3





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 ,
Mar 28, 2008 Mar 28, 2008
Thanks Raymond for your help. This is what I looked for 🙂


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 ,
Mar 26, 2008 Mar 26, 2008
Maybe something like this:

function getLoaderImageCopy(loader:Loader):Bitmap
{
var o:DisplayObject = loader.content;
// see BitmapData ctor for additional args
var img:BitmapData = new BitmapData(o.width, o.height);
img.draw(o);
var bmp:Bitmap = new Bitmap(img);
return bmp;
}
var newImg:Bitmap = getLoaderImageCopy(myLoader);
this.addChild(newImg);





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 ,
Mar 26, 2008 Mar 26, 2008

U¿ytkownik "Raymond Basque" <nospam-rbasque-at-jednm-dot-com@nospam.com>
napisa³ w wiadomo¶ci news:fsdik3$ht8$1@forums.macromedia.com...
> Maybe something like this:
>
> function getLoaderImageCopy(loader:Loader):Bitmap
> {
> var o:DisplayObject = loader.content;
> // see BitmapData ctor for additional args
> var img:BitmapData = new BitmapData(o.width, o.height);
> img.draw(o);
> var bmp:Bitmap = new Bitmap(img);
> return bmp;
> }
> var newImg:Bitmap = getLoaderImageCopy(myLoader);
> this.addChild(newImg);

Thanks Raymond. It works fine and solves my problem :-)
By the way - is any way to clone not bitmap movie clips? Sometimes MC has
complex structure and contains attached a couple of another MC's including
bitmaps and other objects.



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 ,
Mar 28, 2008 Mar 28, 2008
I came across Object.constructor in the docs today -- I don't know if you could use it usefully for a DisplayObject that came from a loader.

Bob
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 ,
Mar 28, 2008 Mar 28, 2008
LATEST
>I came across Object.constructor in the docs today -- I don't know if you
>could use it usefully for a DisplayObject that came from a loader.

Hi Bob,

Raymond's answer solved my problem and it works fine. It seems that each
object type must be cloned in separate way.


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