Flixel: FlxMovieClip
-
FlxMovieClip is a tool for Flixel programmers.
With it you can draw a Flash MovieClip just like an FlxSprite (with a couple exceptions).
–> DOWNLOAD HERE <–
Demo
Usage
To create a new FlxMovieClip and add it to the state:
var fmc:FlxMovieClip = new FlxMovieClip(x,y); fmc.loadMovieClip(movieclip, width, height); add(fmc);
To create a new looping FlxMovieClip and add it to the state:
fmc.loadMovieClip(movieclip, width, height, true);
To create a new FlxMovieClip that starts playing automatically and add it to the state:
fmc.loadMovieClip(movieclip, width, height, false, true);
To create a new FlxMovieClip that calls a function at the end of its animation and add it to the state:
fmc.loadMovieClip(movieclip, width, height, false, false, function);
To start/stop MovieClip:
fmc.playing = true; fmc.playing = false;
FAQ
What’s the point of this?
- To draw a Flash MovieClip as an FlxSprite, so you can use it in Flixel like any other FlxSprite.
Should I use this instead of FlxSprites or FlxG.stage.addChild()/addChildAt()?
- Using FlxMovieClips is more processing intensive than the other methods. Don’t use this if you can avoid it!
Why not just use FlxSprites instead of MovieClips?
- It’s true that MovieClips can be rendered as sprite sheets and imported into Flixel. This should be the first thing you try!
- However, sometimes FlxSprites just don’t meet the requirements – e.g. complicated menus with lots of animations, or long high-quality cutscenes that are simply too large to render as a sprite sheet.
Why not just use FlxG.stage.addChild()/addChildAt()?
- It’s true that addChild can be used to draw a MovieClip on top of or below the Flixel camera. This should be the second thing you try!
- However, sometimes addChild just doesn’t meet the requirements – e.g. if you want the Flixel cursor to appear on top, or draw the MovieClip at a specific order within the Flixel display list.
Can I really do everything I can do with a Flash MovieClip with an FlxSprite?
- No! Some method and properties are unsupported.
- Supported: x, y, angle, scale, offset, velocity, acceleration, antialiasing, visible, flash movieclip animations
- Unsupported: alpha, blend, replaceColor, stamp, drawLine, fill, active, flixel animations
Can I use this in my game?
- Yes. Free of charge.
- Please thank me in the credits and link back to me if possible – but this is not a requirement!
Where can I download the source code?
Is the demo’s source code included in the download?
- Yes
Is anything else included in the download?
- Yes, the Flixel engine from http://flixel.org that is required to run the demo.
- See included flixel-license.txt file for licensing information.
FlxMovieClip
package org.flixel.plugin.funstorm { /** * FlxMovieClip * * With this you can draw a Flash MovieClip just like an FlxSprite! * (with a couple of exceptions) * * WARNING! Processing intensive - only use when desired effect is * impossible to achieve with FlxSprite or FlxG.stage.addChild(). * * Supported methods / properties: * - x ,y, angle, scale, offset, velocity, acceleration * - antialiasing, visible, flash movieclip animations * * Unsupprted methods / properties: * - alpha, blend, replaceColor, stamp, drawLine, fill * - active, flixel animations * * @version 1.0 - September 25th 2011 * @link http://www.funstormgames.com/ * @author Wolfgang @ Funstorm Ltd */ import org.flixel.FlxSprite; import org.flixel.FlxG; import org.flixel.FlxCamera; import flash.display.BitmapData; import flash.display.MovieClip; public class FlxMovieClip extends FlxSprite { /** * Internal, the <code>MovieClip</code> being rendered. */ protected var _mc:MovieClip; /** * Internal, bitmapdata which the <code>MovieClip</code> is rendered to. */ protected var _mcBitmapData:BitmapData; /** * Internal, used to keep track of the last <code>MovieClip</code> frame shown. */ protected var _mcLastFrame:int; /** * Change this to false/true to start/stop the <code>MovieClip</code>. * Can also use to check whether or not currently playing. */ private var _isPlaying:Boolean = false; /** * Whether or not the <code>MovieClip</code> should restart at tbe beginning when it finishes playing. */ public var isLooping:Boolean; /** * A function that gets called every time the <code>MovieClip</code> reaches the end of the timeline. */ public var callbackOnComplete:Function; /** * Creates an empty <code>FlxMovieClip</code> at the specified position. * * @param X The initial X position of the sprite. * @param Y The initial Y position of the sprite. */ public function FlxMovieClip(X:Number=0,Y:Number=0) { super(X, Y); } /** * Load a <code>MovieClip</code> from an embedded file. * * @param TheMovieClip The <code>MovieClip</code> you want to use. * @param Width The <code>MovieClip</code>'s width. * @param Height The <code>MovieClip</code>'s height. * @param IsLooping Whether or not to loop the <code>MovieClip</code> when playing. * @param AutoPlay Whether or not to start playing the <code>MovieClip</code> right away. * @param CallbackOnComplete This function gets called every time the <code>MovieClip</code> reaches the end of its timeline. * * @return This <code>FlxMovieClip</code> instance (nice for chaining stuff together, if you're into that). */ public function loadMovieClip(TheMovieClip:MovieClip, Width:int, Height:int, IsLooping:Boolean = false, AutoPlay:Boolean=false, CallbackOnComplete:Function = null):FlxMovieClip { _mc = TheMovieClip; width = Width; height = Height; isPlaying = AutoPlay; isLooping = IsLooping; callbackOnComplete = CallbackOnComplete; return this; } /** * Start / stop playing the <code>MovieClip</code>. */ public function set isPlaying(Play:Boolean):void { if (Play) _mc.play(); else _mc.stop(); _isPlaying = Play; } /** * Check whether the <code>MovieClip</code> is playing. */ public function get isPlaying():Boolean { return _isPlaying; } /** * Check whether the <code>MovieClip</code> has finished playing. */ override public function update():void { if (isPlaying) { if (_mc.currentFrame == _mc.totalFrames || _mc.currentFrame < _mcLastFrame) { if (isLooping) _mc.gotoAndPlay(0); // loop back to beginning else { _mc.gotoAndStop(_mc.totalFrames-1); // go to last frame isPlaying = false; // and stop } if (callbackOnComplete != null) { callbackOnComplete(); } } _mcLastFrame = _mc.currentFrame; } } /** * Draw the <code>MovieClip</code> to the camera buffer. */ override public function draw():void { // This class is mostly copied over from FlxSprite. // Comments are next to any lines changed. if (_mc != null) { // Only draw if a movieclip has been loaded // create a new bitmap data with a transparent background // this is necessary because otherwise the movieclip frames get drawn on top of each other // (drawing on top works fine for movieclips with an opaque background but of course is a major problem for MCs with a transparent BG) _mcBitmapData = new BitmapData(width, height, true, 0); // draw the movieclip to the bitmapdata so we can draw it to the camera later _mcBitmapData.draw(_mc); if(cameras == null) cameras = FlxG.cameras; var camera:FlxCamera; var i:uint = 0; var l:uint = cameras.length; while(i < l) { camera = cameras[i++]; if(!onScreen(camera)) continue; _point.x = x - int(camera.scroll.x*scrollFactor.x) - offset.x; _point.y = y - int(camera.scroll.y*scrollFactor.y) - offset.y; _point.x += (_point.x > 0)?0.0000001:-0.0000001; _point.y += (_point.y > 0)?0.0000001: -0.0000001; _matrix.identity(); _matrix.translate(-origin.x,-origin.y); _matrix.scale(scale.x,scale.y); if((angle != 0) && (_bakedRotation <= 0)) _matrix.rotate(angle * 0.017453293); _matrix.translate(_point.x + origin.x, _point.y + origin.y); camera.buffer.draw(_mcBitmapData,_matrix,null,blend,null,antialiasing); // where the magic happens: the bitmap data gets drawn to the camera } } } /** * Clean up memory. */ override public function destroy():void { _mc = null; _mcBitmapData = null; callbackOnComplete = null; super.destroy(); } /** * Can't use graphics of type Class with FlxMovieClip. */ override public function loadGraphic(Graphic:Class, Animated:Boolean = false, Reverse:Boolean = false, Width:uint = 0, Height:uint = 0, Unique:Boolean = false):FlxSprite { return null; } /** * Can't use graphics of type Class with FlxMovieClip. */ override public function loadRotatedGraphic(Graphic:Class, Rotations:uint = 16, Frame:int = -1, AntiAliasing:Boolean = false, AutoBuffer:Boolean = false):FlxSprite { return null; } /** * Can't use graphics of type Class with FlxMovieClip. */ override public function makeGraphic(Width:uint, Height:uint, Color:uint = 0xffffffff, Unique:Boolean = false, Key:String = null):FlxSprite { return null; } } }

14 Responses and Counting...
I was trying to make a small Flash ap that would show a small video clip when interacting with a character but could never manage to get the Flx elements and MovieClips to play nice together. This looks like it’ll be just what I was looking for.
WOW! nice staff dude!
Great class, I tested it with the example MC_Smiley and it worked perfectly in my project. Unfortunately, I have an animation that I doesn’t show up no matter what I do.
I added a class to my symbol, exported to SWC, tried using the SWF… Can anyone shed some light on what has to be done to the animation in order to be accepted by FlxMovieClip?
Woops sorry didn’t see your comment until now. Did you get an error message, or does it just show a blank sprite?
- Did you set a linkage name on the movieclip’s properties in Flash?
- Are you sure your movieclip is in the visible portion of the sprite? Eg if your movieclip origin is at 0,0 but your actual graphics are at like -500,0 it’ll show up transparent, same if your sprite is only 500 wide but your movieclip graphics are beyond that.
- Try pressing ~ on your keyboard and then the little cube that appears in the top-right corner. You should now see a red box being drawn around the movieclip sprite (even if it’s transparent). Is this showing up?
Sorry but without more info there’s like a million things that could be causing this
Upload your file somewhere if you can’t figure it out and I’ll take a look.
Does this work with buttons and mouse over states? I’m trying to add a button to a Flixel game but it doesn’t seem to trigger any of the mouse over animations.
Nope. It just draws it.
Thks for making this plugin, it really did help but I have been trying to rotate it since this morning but i just couldn’t make it rotate.
i tried setting its angularVelocity, the angle gets updatd however the image(movieclip) on the screen doesn’t get updated
Its kinda Urgent …. Please help
Thanks,
Candiie
Woops, my mistake. Looks like you’re right.
—
To fix it, you will need to add 1 line to FlxMovieclip. Open up FlxMovieclip.as and find lines 84-86 which should say:
—
public function loadMovieClip(TheMovieClip:MovieClip, Width:int, Height:int, IsLooping:Boolean = false, AutoPlay:Boolean=false, CallbackOnComplete:Function = null):FlxMovieClip
{
_mc = TheMovieClip;
—
Now you need to add this next line before the _mc = TheMovieclip line:
—
_bakedRotation = 0;
—
So now it should look like:
—
public function loadMovieClip(TheMovieClip:MovieClip, Width:int, Height:int, IsLooping:Boolean = false, AutoPlay:Boolean=false, CallbackOnComplete:Function = null):FlxMovieClip
{
_bakedRotation = 0;
_mc = TheMovieClip;
—
And now it should rotate properly.
Thks a lot though I forget to post back but this prob was solved awhile after I posted, but you doubled confirm my edit thanks a lot for replying!!!
Once again really thanks for the plugin
Candiie
this is a really good plugin, but the width and height have their limitations, I can’t make my movieclip appear in 1024 x 600 or even 800 x 600 it have its own limitations, is there any way I can fix it?
The plugin doesn’t have any special width/height limitations. If your movieclip isn’t appearing, check to make sure it’s positioned correctly.
Hi Wolf, seems like a pretty useful plugin. However, looking at the demo’s code, I have a question: How or where do you set your movieclip reference url? I saw this:
_demoFlxMovieClip.loadMovieClip(new MC_Smiley(), 105, 262, true, false, increment_bounce_text);
... however, I didnt find the reference for "MC_Smiley()", an url, nor a file named exactly like this on the demo nor the project folders. How do you attach one to another?
Thanks!
Hi Wolf, seems like a pretty useful plugin. However, looking at the demo’s code, I have a question: How or where do you set your movieclip reference url? I saw this:
_demoFlxMovieClip.loadMovieClip(new MC_Smiley(), 105, 262, true, false, increment_bounce_text);
… however, I didnt find the reference for “MC_Smiley()”, an url, nor a file named exactly like this on the demo nor the project folders. How do you attach one to another?
Thanks!
Hello and thanks a ton for this.
I have the same question as Nestorvc, though.
Being a bit of a Flash noob I have no idea who you actually load the movieclip into the project. I get the compile error call to a possibly undefined method MC_Smiley() as well since I feel like there is no url or path or anything setting up the movieclip and this is the part I’m stuck on in my own work as well.
Thanks for any help you can offer on this.