How to ‘smooth’ an FLV / F4V video for Scaling

Have you noticed that your embedded Flash video looks like crap if you scale your flash file up or down?

A lot of people don’t realize this, but you can actually apply smoothing to flash video, just as you can to a bitmap image.

All you have to do is apply the following Actionscript 3 code to your video clip instance:

for example, say your video clip instance is called ‘video’

video.getVideoPlayer(video.activeVideoPlayerIndex).smoothing = true;

That’s it! Now you can scale your flash video along with your other graphics. If you plan to scale up, be sure to use a video with a greater resolution than you need, so that you can get the best results. Scaling down will just simply work.

Easily sort an Array of Strings: Longest to Shortest Word

So let’s say you are making some sort of a game or program in Actionscipt 3 that requires you to sort words in order from longest to shortest. for example:

var word:Array = new Array("this", "is", "a", "list", "of", "randomly","chosen","words", "to", "sort");

if you try something like:

trace(word.sort());

you will get the following:

a,chosen,is,list,of,randomly,sort,this,to,words

That’s not what we wanted! A cool little thing that you can do in as3 is write what’s called a ‘compare function’. As I love to do, I have made a simple class with a static function that you call:

package com.frigidfish{

    public class Sort {

        public function Sort() {
            // constructor code
        }

        public static function hiToLow(a:String, b:String):Number {


            if (a.length < b.length) {
                return 1;
            }
            else if (a.length > b.length) {
                return -1;
            }
            else {
                return 0;
            }
        }
    }
}

Now just save the class under your project in the correct folder (ie c:\myproject\com\frigidfish\Sort.as).

then, you can easily sort your array like so:

import com.frigidfish.Sort;

var word:Array = new Array("this", "is", "a", "list", "of", "randomly","chosen","words", "to", "sort");

trace(word.sort(Sort.hiToLow));

The output will now be:

randomly,chosen,words,list,this,sort,of,is,to,a

Yay!

You can use compare functions to tell the built in sort function how you would specifically like your data sorted.

The function takes 2 variables that are passed by the sort function (you don’t pass anything manually to the function. it’s automatic).

Because our array contains nothing but strings, we made up 2 variable names, ‘a’ and ‘b’; both of type string.

the function can return -1, 0 or 1. Depending on the return value, the sort will be affected. Experiment with different sorting methods and see what you can come up with..

For example, to sort low to high (short word to long word in our case) you would just add this function to the Sort class:

public static function lowToHigh(a:String, b:String):Number {


            if (a.length > b.length) {
                return 1;
            }
            else if (a.length < b.length) {
                return -1;
            }
            else {
                return 0;
            }
        }

of course you will also need to call the proper function now when you sort:

trace(word.sort(Sort.lowToHigh));

I hope this helps. Just had to use it in a project myself!

Advanced Function Work

In this post I’m going to show you a few more advanced features of AS3 functions. I think you’ll find they certainly help, at least when it comes to coding an API for the general public.

…rest Parameter

The rest parameter is cool little parameter type in AS3. It allows for varying sizes of parameter lists to be passed into functions. Let’s look at a quick example:

public function foo(bar:String, foobar:Object, ...rest):void

In this example, we declare function called foo, which has two defined parameters called bar and foobar, and then as the last parameter we have this thing called …rest. As you can tell, …rest is the rest parameter. The rest parameter is declared by three¬†ellipses or dots (…) and then followed by the parameter’s name. In this case I’ve named it rest, but you could name whatever you like. For example:

public function foo(bar:String, foobar:Object, ...someBigListOfWhoKnowsWhat):void

What does this look like for the person who’s calling the function? Let’s take a look:

foo("bar", {foobar: null}, "some", "Big", "List", "Of", "Strings"):void

As you can see, when we call this function we pass in the two defined parameters, and then comes a big long list of parameters that we didn’t define; this is how the rest parameter works, the user can put in as many parameters as they like. It’s important to note, that the user can also pass in parameters of any type, so you’ll have to do some checking if you want the parameters to be of a certain type.

Now we’ll get onto how you can actually access these parameters passed in. When the function actually starts executing it’s statements, the rest parameter has been converted into an array. So we can access the parameters like this:

public function foo(bar:String, foobar:Object, ...otherParams):void {
trace(otherParams.length);
trace(otherParams[0]);
trace(otherParams.shift());
otherParams.unshift("tom");
}

As you can see, we can perform various array methods on it, access the length property, and access the elements in the array. We could also do a for each or for loop on the array, and so on. But what if we need to pass these parameters on to another function, expecting a rest parameter (not an array) itself? That’s what we’ll discuss next.

Function.apply()

When you define a function, you are creating a Function object. The object itself has a number of handy methods. One of those methods we are going to talk about today, and that it the apply() method.

The apply method allows you to call a function, but pass in the parameters needed as an array. Let’s write some setup code:

public function foo(bar:String, ...otherParams):void {
// bar is a string we need to do some processing on
var bar2:String = bar.replace(/\$\d/, someArray[0]);
}
 
public function bar(foo:String, ...otherParams):void {
// some code to do something with these parameters
}

Now what we need to do is when someone calls foo, we want to do some processing on the bar string as seen in foo(), and then pass on the processed string and all the parameters the …otherParams rest parameter to bar() and return whatever bar() returns. How do we that when, inside the actual function, otherParams is an array (meaning we can’t just go

return bar(bar2, otherParams);

). This is where apply() comes in:

public function foo(bar:String, ...otherParams):void {
// bar is a string we need to do some processing on
var bar2:String = bar.replace(/\$\d/, someArray[0]);
var applyParams:Array = otherParams;
applyParams.unshift(bar2);
return bar.apply(this, applyParams);
}

So what we’re doing that’s different is, creating an array called applyParams; this is the array of parameters to pass to apply(). Then we are adding the processed string (bar2) to the front of the applyParams array. Finally, we are calling the apply method of the bar object (which is a Function), passing in this (this object) and the applyParams array and returning the returned value. The apply method will use the array as the list of parameters for the function and everything will work smoothly. But why do need to pass in the “this” reference? The first parameter of apply, (so far as I understand it) is what object to point to wherever “this” is referred to in the function. Therefore, be careful with that one and take into account where the function is located.

Anyway, that wraps up this post, hope you learnt a thing or two about functions. I could say more, but I’ll leave it at that. Please feel free to post your thoughts below.

Hidden Games in as3blog.org

I just realized that there are some hidden games in my banner. This may seem like a strange statement since I made the banner and all the artwork. Well, originally the banner was a concept design for a company that we do projects for called “ThinkFun”. I stripped down the banner but apparently left some ‘hidden’ games.. See if you can find the games hidden in the header banner of this site! I believe there are anywhere from 3 to 6 or so.

By the way, for anyone that has read my Game Grid article, these games all use the concept, so you can see it in action.

Enjoy!

Load SWF via Actionscript 3 with this class

Here is a class that you can use to load swf’s into an object that you can display or do whatever you’d like with:

// SWFLoader Class by Rick Nuthman
// 2.10.10
//
// Usage:
/*
var mySWF:Object;
mySWF = SWFLoader.create("urlToSWF.swf", 0,0);
mySWF.dispatcher.addEventListener("ready", displaySwf);
function displaySwf(event:Event){
    addChild(mySWF.swf);
}
*/


package com.frigidfish{

    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.events.EventDispatcher

    public class SWFLoader{        

        public function SWFLoader() {
        }

        public static function create(url, x=0,y=0):Object {
           
            var loaded:Event                = new Event("ready");
            var dispatcher:EventDispatcher  = new EventDispatcher();
            var swf:Sprite                  = new Sprite  ;
            var mLoader:Loader              = new Loader  ;
            var obj:Object                  = new Object;
           
            obj.swf = swf;
            obj.dispatcher = dispatcher;
           
            swf.x = x;
            swf.y = y;
           
            var mRequest:URLRequest = new URLRequest(url);
            mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,swfLoaded);

            mLoader.load(mRequest);

            function swfLoaded(event:Event) {
                var target = event.currentTarget.content;              
                swf.addChild(target);  
                dispatcher.dispatchEvent(loaded);
            }
           
            return obj;
        }
    }
}

You don’t need to make an instance of the class, just use the static function ‘create’ like so:

var mySWF:Object;
mySWF = SWFLoader.create("urlToSWF.swf", 0,0);
// You can replace 0,0 in the previous line with
// initial x,y coordinates for your swf
mySWF.dispatcher.addEventListener("ready", displaySwf);
function displaySwf(event:Event){
    addChild(mySWF.swf);
}

I have been using this class to load a bunch of swfs into an array of objects.. I built a preloader that says something like “loading foo.swf…” for each swf as they are loading. Then when the “ready” event has fired for all of the swfs I add them all to a container sprite and display them.