Snippet Name: Shuffle Array (Action Script 3)
Keywords: Flash,Shuffle,Array,AS3
Description: This reorders the elements of an array into a random order.
Code:
var anArray:Array = [1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4,4,4];
anArray=shuffleArray(anArray);
function shuffleArray(arr:Array):Array{
var arr2:Array = [];
while (arr.length > 0){
arr2.push(gendersArray.splice(Math . round(Math.random() * (arr.length - 1)), 1)[0]);
}
return arr2;
}
Snippet Name: Convert a String into an Array Using Action Script 3
Keywords: String To Array, AS3
Description: Converts the string into an array using the actionscript "split" function.
Code:
//the string to turn into an array
var arrayString:String="1,2,3";
//convert the string to an array using the "," as a divider (aka delimiter)
var newArray:Array= arrayString.split(",")
//convert the array items from strings to numbers
var n=newArray.length;
while (n--) {
newArray[n]=Number(newArray[n])
}
trace(String(trainCubesArray[0]+1));
Snippet Name: Detecting when mouse leaves stage.
Keywords: Mouse Detection, AS, Flash, Rollover, Hover
Description: This is how you detect if the mouse leaves Flash and rolls over to web page:
Code:
var c:Sprite = new Sprite();
c.graphics.beginFill(0xFF);
c.graphics.drawRect(0, 0, 200, 200);
addChild(c);
stage.addEventListener(Event.MOUSE_LEAVE, cursorHide);
//there is no mouse exit so you need to use this
stage.addEventListener(MouseEvent.MOUSE_MOVE, cursorFollow);
function cursorHide(evt:Event):void {
c.alpha=.2
}
function cursorFollow(evt:MouseEvent):void {
c.alpha=1
evt.updateAfterEvent();
}
Snippet Name:Blur Using AS3
Keywords: Blur,Flash,AS3,AS4
Description:Creates a blur effect on movie clips.
Code:
var blur:BlurFilter = new BlurFilter();
blur.blurX=20;
blur.blurY=20;
blur.quality=BitmapFilterQuality.LOW;
displayObjectToBlur_mc.filters=[blur];
Snippet Name:AS3 full screen mode
Keywords: AS3, FullScreen, Mode
Description:Toggles full screen mode using AS3.
Code:
function toggleFullScreenMode(e:MouseEvent):void {
if (viewingFullScreen==false) {
viewingFullScreen=true;
navsNorthMultiPage_mc.tween_mc.eastButtons_mc.fullScreenAnim_mc.gotoAndPlay("full");
navsSouthMultiPage_mc.tween_mc.eastButtons_mc.fullScreenAnim_mc.gotoAndPlay("full");
stage.displayState="fullScreen";
} else {
viewingFullScreen=false;
navsNorthMultiPage_mc.tween_mc.eastButtons_mc.fullScreenAnim_mc.gotoAndPlay("out");
navsSouthMultiPage_mc.tween_mc.eastButtons_mc.fullScreenAnim_mc.gotoAndPlay("out");
stage.displayState="normal";
}
}
Snippet Name:AS3 Disable Yellow Tab Rectangles
Keywords:Disable, AS3, Tab, Rectangles
Description:Are you wondering why when you hit tab in your Flash site you get those nasty yellow rectangles around your links? Here's how to disable them:
If you have a movieclip with a bunch of buttons inside it and you want to disable all of them with one line of code use this:
Code:
MC_Container_Name.tabChildren = false;
Or to just disable the yellow box on a single movieclip use this:
Code:
MC_Name.tabEnabled = false;
Snippet Name:AS3 Smooth Loaded Bitmaps
Keywords:AS3,Smooth,Loaded,Bitmaps
Description: Loaded bitmaps (jpgs, pngs, etc) look bad in flash when scaling unless you smooth them. This function seems to replicate the bitmap "smoothng" setting in the library pallette. I still see tons of Flash sites out there that arent aware of this awesome setting. It makes bitmaps look great when they scale.
Code:
//create a new loader object and put it on the stage
var slideLoader:Loader=new Loader();
this.addChild(slideLoader);
//load picture into loader object
slideLoader.contentLoaderInfo.addEventListener(Even*****MPLETE, scaleContentForPicture);
slideLoader.load(new URLRequest("https://www.mpgh.net/forum/images/tree.jpg"));
//smooth it once loaded
function scaleContentForPicture() {
var smoother_bm=Bitmap(slideLoader.content);
smoother_bm.smoothing=true;
}
Snippet Name:Slideshow with autoplay and reset delay
Keywords:AS3,Slideshow,autoplay
Description: This code starts with a long pause then starts looping through images. If the user interacts with the slideshow, the timer is reset then starts playing again later if the user hasnt interacted.
Code:
var resetTime:uint=5000;
var pauseBetweenSlides:uint=5000;
var slideCounter:uint=1;
var prevSlide:uint=0;
var startShowTimer:Timer=new Timer(resetTime,1);
startShowTimer.addEventListener(TimerEvent.TIMER, setUpTimer);
var slideShowTimer:Timer=new Timer(pauseBetweenSlides,0);
slideShowTimer.addEventListener(TimerEvent.TIMER, autoAdvance);
//auto advance
function autoAdvance(e:TimerEvent):void {
nexttSlide();
}
//next
function nexttSlide():void {
prevSlide=slideCounter;
slideCounter++;
if (slideCounter==6) {
slideCounter=1;
}
goToSlide();
}
//prev
function prevvSlide():void {
prevSlide=slideCounter;
slideCounter--;
if (slideCounter==0) {
slideCounter=5;
}
goToSlide();
}
//display next slide and hide previous
function goToSlide():void {
slideshow_mc["slide"+slideCounter+"_mc"].gotoAndPlay("show");
if (prevSlide>0) {
slideshow_mc["slide"+prevSlide+"_mc"].gotoAndPlay("hide");
}
}
//pause slideshow and reset timer
function resetTimer():void {
slideShowTimer.stop();
startShowTimer.reset();
startShowTimer.start();
}
//called when reset pause is finished
function setUpTimer(e:TimerEvent):void {
startShowTimer.reset();
slideShowTimer.start();
}
resetTimer();
goToSlide();
//Controls--------
function showControls():void {
slideShowPrev_mc.gotoAndPlay("show");
slideShowNext_mc.gotoAndPlay("show");
}
function hideControls():void {
slideShowPrev_mc.gotoAndPlay("hide");
slideShowNext_mc.gotoAndPlay("hide");
}
slideShowNext_mc.addEventListener(MouseEvent.CLICK, invokeNextSlide);
function invokeNextSlide(e:MouseEvent):void {
resetTimer();
nexttSlide();
}
slideShowPrev_mc.addEventListener(MouseEvent.CLICK, invokePrevSlide);
function invokePrevSlide(e:MouseEvent):void {
resetTimer();
prevvSlide();
}