Add Mask to Display List when using StageScaleMode.SHOW_ALL

I spent a few hours going nuts with this one today, so save yourself some time by reading this post. Suppose that you have some content that you want to scale when the flash player is re-sized. Suppose also that you have added this line in order to accomplish it:

stage.scaleMode = StageScaleMode.SHOW_ALL;

Now imagine that you test that program and are stretching the window around. Everything seems to be working great, except that there are some items that you have used masks on and the mask is floating around all over the place!

There is a VERY simple fix for this. In AS3 projects you MUST add the mask itself to the display list, or it will NOT scale with your project.

So, for example:

var myMask:Sprite = new Sprite();
myMask.graphics.beginFill(0x0000FF);
myMask.graphics.drawRect(0,0,100,200);
myMask.graphics.endFill();

// YOU MUST FIRST ADD IT TO THE DISPLAY LIST OR IT WILL NOT SCALE WITH THE CONTENT!!!
addChild(myMask);
myContent.mask = myMask

I figured this out by just trying it on a hunch. I’m guessing that by adding it to the display list it has something to reference for what size it is ‘supposed’ to be. When you simple add a mask to a display object’s mask property, it does not make it a child of that object automatically; therefore it does not scale with it. Weird if you ask me, but there it is. I hope this helps!

How to Remove Spaces from a String

Removing spaces from a string is really easy in Actionscript 3. One method is to simply convert the string to an array using the String.split() function using a space as the delimiter. Then you can use the Array.join function to put it all back together again without the spaces.

Here is a simple function that will do the trick:

public function removeSpaces(input:String):String
{
    var split:Array = input.split(" ");
    var output:String = split.join("");
    return output;
}

That’s it!

example:

trace(removeSpaces("ABCD EFG HI    JKLMNOP QRSTU      VWXYZ"));
// outputs: ABCDEFGHIJKLMNOPQRSTUVWXYZ

Comic-Con 2011 Can Kiss My Ass

I realize that this is completely off subject, but I am so pissed at Comic-con that I must rant.

First of all, there are just too many people, period. I went to the convention for the first time about 4 years ago, and there were a lot of people then. I had no problems getting into see my favorite panels back then. It was quite an enjoyable experience which I recommended to all of my friends!

Well, times have changed and the greed of Comic-con International has shined through with a ferocious vigor. First off, there were a ton of irrelevant panels for shows such as Glee and lame vampire BS. This of course has attracted all of the annoying fans who really don’t care about comics or real science fiction.

What it also introduced was a slew of selfish fans who filled all of the ballrooms to capacity, making it so that real fans could not see their favorite panel. Keep in mind that once you are in a ballroom, comic-con staff will not make you leave. You can camp there all day, taking the seat of someone who really wants to see the panel, just so that you can be an ass.

I arrived at about 8:30am to see The Big Bang Theory panel. I got in line, which wrapped and zig-zagged across the entire length of the convention center. I got in line about 1 hour BEFORE the PANEL BEFORE the Big Bang Theory. It was some other vampire panel. I was still in the line, which had hardly moved an inch up until the vampire panel ended.

A total of “100” people left the ball room. There were over 10,000 people in line. I didn’t realize this at the time of course. So I waited and I waited. Another hour passed. Then another.

The next thing that I realized was that I had missed my panel. No one so much as gave us a warning that there was no way we were ever going to get in to see anything. You see, I was deceived by the line steadily moving ahead for about an hour. This was because people were constantly LEAVING the line. Wow.. was I pissed. Finally, my friend went to ask someone what was going on, and they said ‘Sorry man, you’re not getting in’… ‘Anyway, the panel is almost over anyway.’

It turned out that some of the people who were in line behind me never got anywhere. They missed every single panel. Don’t ask me why they were so crazy as to stay in line all day.

I was so frustrated. I felt completely duped. These people just took everyone’s money knowing damn well that there were too many people, and that only a small fraction would get into the ballrooms. I feel that the pressure needs to be put on. I Think that the celebrities should boycott the show, and start a new convention that is only for sci-fi/comic related material. Make it fun again for those of us who actually love this stuff.

The vampires and the Glee folks have their own fans. Go get your own convention. Stop ruining ours.

A Simple Pause Function in Actionscript 3

Just a quick function that you can use in your library of tricks to simulate pausing in Actionscript 3.

public static function pause(timeInSeconds:int, functionToCall:Function):void {
    var timer:Timer = new Timer(timeInSeconds * 1000);
    timer.addEventListener(TimerEvent.TIMER, callFunction, false, 0, true);
    timer.start();
    function callFunction(event:TimerEvent):void {
        timer.stop();
        timer.removeEventListener(TimerEvent.TIMER, callFunction);
        timer = null;
        functionToCall();              
    }
}

The function takes 2 arguments:

timeInSeconds – how many seconds to wait before calling the function
functionToCall – the function to call after the given time has passed

example usage (assuming that you drop this function into a class called Utilities:

private function gameWinTriggered():void{
 
     // player just dropped the last piece on the game board,
     // but we want to wait a second before showing the win screen
   
    Utilities.pause(1,showWinScreen);
}

private function showWinScreen):void{
     trace('You Win!');
}

That’s all there is to it. Really handy for instances like these when you want to wait before a transition as to not jar the player with instant changes.

Adjust Global Sound Volume in Actionscript 3

There is a simple way to adjust all global sound in Actionscript 3. This encompasses all playing loops, sound effects and such.

All you need is the following code:

import flash.media.SoundMixer;
import flash.media.SoundTransform;

// volume limits are 0 to 1 (0.1,0.2,0.3,...,1)
SoundMixer.soundTransform = new SoundTransform(0);

Of course change the number sent to the SoundTransform constructor to anything between 0 and 1 (0.5 would be half max volume, 0 is muted.)