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.

John Lilley
  • 159
  • 1
  • 3
  • 13
  • 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. – The_asMan Jun 06 '11 at 16:35

2 Answers2

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);
frankhermes
  • 4,720
  • 1
  • 22
  • 39
  • What if I don't have access to the existingLoader? – John Lilley Jun 06 '11 at 11:27
  • 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. – John Lilley Jun 06 '11 at 12:19
  • you have to cast to `movieClip = existingLoader.content as MovieClip`. – DanielB Jun 06 '11 at 15:09
  • 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. – John Lilley Jun 06 '11 at 16:30
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.

DanielB
  • 19,910
  • 2
  • 44
  • 50
  • 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. – John Lilley Jun 06 '11 at 11:13
  • I've edited my post some seconds ago. You have to know the class definitions inside the loaded clip. – DanielB Jun 06 '11 at 11:16
  • I'm new to Actionscript, is there any chance you could explain the getDefinitionByName() method you mentioned. – John Lilley Jun 06 '11 at 11:28
  • have a look at the [docs](http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html#getDefinitionByName()) – DanielB Jun 06 '11 at 15:10
  • 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? – John Lilley Jun 06 '11 at 16:25
  • Every SWF contains a "root" `MovieClip` or `Sprite`. If this one is of a special type (you can set this stage class in Flash) you may create a new instance of this class with the class you get from `var cl:Class = getDefinitionByName("ClassName"); movieClip = new cl() as MovieClip`. – DanielB Jun 06 '11 at 16:29