I’m going to be (presenting) at 360|Flex San Jose 2008, In August. You should be too!
Woo! Hoo! I'll be presenting on:
Advanced State management, the Heirarchical State Design Pattern
Cogs is a heirarchical statemachine library for use with AS3.0 projects. All interactive applications have a lifecycle of descrete states. Cogs extends the State Design pattern, supports nested states, heirarchical state transitions, enter exit and pulse actions. Freeing you to spend more time working on the core logic. We'll show the lifecycle of a media player.
http://troyworks.com/cogs/
http://troyworks.com/blog/?p=9
You should be attending so we can hang out! Tom and John are cool too! Register now!
Flash: 16000 Framelimit for SWF but not for MovieClips, Embedded Video
I've been working on a project that only supports embedded video. We are also using fairly long videos at broadcast frame rates (29.97) so we easily went over the 16000 frame limit of the SWF.
After dynamically attaching some Video Controls, I got curious if the Video objects (which appear in the Library) could support a linkage ID, and be thus controlled by actionscript via something like
-
-
var video:Video = new MyVideo();addChild(video);
Unfortunately it wasn't possible, because Video objects don't support linkageID's ...in fact they are rather wierd! Embedded video appears to be a second or third class citizen in flash, for reasons I don't quite understand. Flex apparently can't embed video at all, though it can embed most other things.
Embedded Video object don't generate ADDED or REMOVED from displaylist events. But they can be discovered by iterating over a DisplayObjectContainer's children seeing if it
-
-
for (var i:int= 0; i< acurTarget.numChildren; i++) {
-
var dO:DisplayObject = acurTarget.getChildAt(i) as DisplayObject;
-
var isVideo = dO is Video;
-
trace(dO + " is video " + isVideo);
-
if (isVideo) {
-
trace("turning video on");
-
(dO as Video).smoothing = true;
-
}
-
}
-
-
However looking at the docs, it said to use a container MovieClip instead of the main timeline, which did allow that sort of displaylist approach. That got me to thinking, maybe the MovieClip isn't constrained to the SWF limits...the Flash IDE's timeline can be extended well past 16000 frames, though I don't know the upper limit. This makes a certain degree of sense as the MainTimeline is closely related to the SWF file format. Which is somewhat locked into posterity.
So I created a small test, and was able to embed a 23000 frame long video. Publishing it took about 1.5 hours and tends to hang on the last bar of progress, making you wonder if it crashed or not, and occassionally Flash IDE runs out of memory, but if you restart the OS, and Flash, and publish it does make it.
Controlling it, if it's onstage means just targetting the inner movieclip, which can also be created via it's linkageID.
Flash: Evils of Flash9, Frames for state and Movieclips with embedded sound or video
One of the things that really bugs me about Flash9 is the rather shoddy use of Frames for state management. What happens is even though clips may removed from stage after a frame has passed, they still exist in memory and in the case of them playing sounds embedded on the timeline...continue to haunt the living clips on stage.
This is a huge departure from Flash8, and I've seen this affect components like SimpleButtons that use frames for statemanagement as well. Since Garbage Collection can't be forced in Flash9 what has to be done (at least for me) is use the new Event.REMOVED event, and clean up buttons, stop sounds, and MovieClips from playing.
-
-
view.addEventListener(Event.REMOVED, onChildRemoved);
-
-
protected function onChildRemoved(event : Event) : void {
-
var dO : DisplayObject = DisplayObject(event.target);
-
var isButton : Boolean = dO is SimpleButton;
-
-
if(isButton) {
-
dO.removeEventListener(Mouse.CLICK....)
-
}
-
if(dO is MovieClip){
-
(dO as MovieClip).stop();
-
}
-
}
-
Flash: Scaling images using viewport
LayoutUtil from Troyworks package supply you with important tools for resizing and aligning a DisplayObject to match another DisplayObject on stage. You can find LayoutUtil source code on Google Code.
One of the handiest things is that you can align an image according to a certain rectangular area of this image, but not the whole thing. Let’s call the swf that we will be importing “content.swf”.
So you need to add a movie clip called viewport_mc to the content.swf. You can make this movie clip transparent if you don’t want it to interfere with your image. The main image in content.swf should be called slug_mc.
Now lets work with the parent swf , where you are going to import your content, lets call it “stage.swf”. In the stage.swf you need to create a movie clip which will be a basis for aligning loaded content. Let’s call this rectangular movie clip background_mc. We add a loader object to load the content.swf:
-
-
var loader:Loader;
And a movie clip object, where the imported content.swf will be stored:
-
-
var mc:MovieClip;
Now we need something new. This will be a snap object – an object storing information about the image.
-
-
var snapObject:IDisplayObjectSnapShot;
Then we load the image and call the scale() function:
-
-
loadSWF();function loadSWF(swf:String) {
-
-
loader = new Loader();
-
-
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeSWFLoadHandler);
-
-
var Arequest:URLRequest = new URLRequest(“content.swf”);
-
-
loader.load(Arequest);
-
-
}
-
-
function completeSWFLoadHandler(event:Event):void {
-
-
mc = MovieClip(Loader(event.target.loader).content);
-
-
snapObject = LayoutUtil.snapshotDimensions(mc);
-
-
this.addChild(mc);
-
-
scale();
-
-
}
In the scale function we use functions from LayoutUtils package. Here the viewport will be used to scale the image to the background_mc. Moreover, we have several options for scaling: scale to center, scale and crop, scale and fill.
-
-
function scale():void {
-
-
LayoutUtil.scaleTo(background_mc, mc, snapObject, "CENTER");//or
-
-
LayoutUtil.scaleTo(background_mc, mc, snapObject, "CROP");
-
-
//or
-
-
LayoutUtil.scaleTo(background_mc, mc, snapObject, "FILL");
-
-
}
When we scale to center, the image is scaled according to the shortest side. Scale and crop scales the longest side of the image and then crops what’s left. And the scale and fill does not take into account the original ratio of the image and just scales width and height independently.
You can download an example swf to play with the scaling functions. Here's the source FLAYou can also download the LayoutUtil.as.
LayoutUtil.as
SWF Application
