1

I'm creating a game in ActionScript 3.0 using the FlashPunk game engine and the FlashDevelop SDK. I've created my own MovieClip class that takes a preloaded movie clip file.

public function MyMovieClip(file:MovieClip, posX:int, posY:int, frameRate:int) 
    {
        movieClip = file;
        movieClip.x = posX;
        movieClip.y = posY;
        movieClip.stop();
        FP.stage.addChild(movieClip);


        timer = new Timer((1 / frameRate) * 1000);
        timer.start();
        timer.addEventListener(TimerEvent.TIMER, onTick);
    }

The update for my movie clip is as follows:

private function onTick(e:TimerEvent):void
    {
        if (isRepeating)
        {
            if (movieClip.currentFrame == movieClip.totalFrames )
            {
                movieClip.gotoAndStop(0);
            }
        }
        movieClip.nextFrame();
    }

The problem that I'm having is that when I have multiple instances of the MyMovieClip class using the same swf file, only the last instance is rendered and is updated for each instance of the class that I have.(e.g 3 instances of MyMovieClip, the last instance is updates at 3 times the speed.)

If anymore info is needed I'll be happy to supply it.

1
  • Post the code where you are calling instantiating and calling MyMovieClip. I have a feeling you are overwriting the objects each time you call the function. Commented Jun 6, 2011 at 16:35

2 Answers 2

2

You can create a new instance of the same loaded swf by doing this:

  // re-use a loaded swf
  var bytes:ByteArray = existingLoader.content.loaderInfo.bytes;
  var loader:Loader = new Loader();
  loader.loadBytes(bytes);

where existingLoader is the Loader that you used to load the swf in the first place.

The Loader used with loadBytes will dispatch another COMPLETE Event, so when you make a listener for that, you can use the 'cloned' version:

  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSWFClonedComplete);
Sign up to request clarification or add additional context in comments.

4 Comments

What if I don't have access to the existingLoader?
I've played around with my code so that I have access to my existing loader and your example works. I'm not sure this is what I want to be doing though as I don't want to be doing anymore loading. Is loading the bytes from an existing loader much quicker than creating a new one. I tried doing movieClip = existingLoader.content as the loader contains a MoveiClip Object but I got the following error: Error: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type flash.display:MovieClip.
you have to cast to movieClip = existingLoader.content as MovieClip.
Ok that worked but not how I thought. Still gives me the same problem. Looks like I'll have to reload the Bytes like you first mentioned. Thanks for the help.
1

You may have multiple instances of MyMovieClip, but what's with file:MovieClip which you are adding to the stage. If this is always the same instance of a MovieClip you will have this result, regardless how often you instantiate your MyMovieClip class, because you are adding the same instance multiple times to the stage.

You may have to load the "preloaded clip" multiple times or, if you are able to (you know the class name etc.), create a new instance of the desired class with getDefinitionByName() from your loaded clip and attach this new instance.

6 Comments

Yes you're right. How would I then load in this swf so that I can use it multiple times. At the moment I'm loading in all my swfs into a dictionary as MovieClips. I don't want to have to reload the swf each time I want to make a movie clip with it.
I've edited my post some seconds ago. You have to know the class definitions inside the loaded clip.
I'm new to Actionscript, is there any chance you could explain the getDefinitionByName() method you mentioned.
have a look at the docs
I'm not sure I understand how this helps me. With that I can make a new instance of a class object of my choosing but how do then attach my swf without having the same problem as before?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.