Super Simple Script Snippets – Part 2

This is part two of my previous post of fun code trickery. Click here.
I’m thrilled with how development is going. It will definitely be a fun mobile game.

Like I said, you can find more in the Open Source Directory. Click here.

So… I should hit “Publish” now before I get distracted too much with my blog. :)

Random Sound From Array Of Strings
Download Here
[swfobj src=”http://nathalielawhead.com/sourcefiles/Haxed_By_Megahurtz/snippets_AS3/random_sound_from_array.swf” height=”456″ width=”608″]

If Removed From Stage Remove All Listeners
Download Here
[swfobj src=”http://nathalielawhead.com/sourcefiles/Haxed_By_Megahurtz/snippets_AS3/remove_from_stage_remove_all_listeners.swf” height=”80″ width=”428″]

Code:

//loop through an array of functions/listeners that you pass it
//removes them
//call this from wherever the listeners are added
//usage: remove_listener(clip, [listener1, listener2]);
function remove_listener(clip:Object, list:Array){
function removed(event:Event){
trace(clip.name+" removed listener");
for(var i:Number = 0; i < list .length; ++i){ event.currentTarget.removeEventListener(Event.ENTER_FRAME, list[i]); //delete this: txt_list.text += "\n"+String(event.currentTarget+" removed "+i+" listeners"); } event.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, removed); } clip.addEventListener(Event.REMOVED_FROM_STAGE,removed); }

Remove All Children Of Certain Class
Download Here
[swfobj src="http://nathalielawhead.com/sourcefiles/Haxed_By_Megahurtz/snippets_AS3/removeAllChildren.swf" height="148" width="273"]

Code:

//Removes all childrens of a Class.
//Loops through container
//Removes what it finds
function removeAllChildren(obj:Class){
//pushed to array because if you remove
//while you loop through the stage
//then the loop terminates prematurely
var removeArray:Array=new Array();
for(var i:Number=0;i < this .numChildren;i++){ if(this.getChildAt(i) is obj){ removeArray.push(this.getChildAt(i)); } } //after you got it all, remove for (var j:Number = 0; jReverse Snow
DOWNLOAD SOURCE
[swfobj src="http://nathalielawhead.com/sourcefiles/Haxed_By_Megahurtz/snippets_AS3/reverse_snow.swf" height="400" width="500"]

Code:

//SNOW//
function initSnowFunc(clip:MovieClip, windSpeed:Number) {
//
var radius:Number = 0;
var xpos:Number = clip.x;
var i:Number = 1+Math.random()*2;
var k:Number = -Math.PI+Math.random()*Math.PI;
//set up first run
clip.x = Math.random()*stage.stageWidth;
clip.y = Math.random()*stage.stageHeight;
gotoAndStopRand(clip);
//move
function this_onEnterFrame(event:Event) {
var this_clip = event.currentTarget;
//horizontal
radius -= (k/180)*Math.PI;
this_clip.x += Math.ceil(windSpeed*(Math.cos(radius)));
//vertical
this_clip.y -= Math.ceil(i);
//re-start
if (this_clip.y< =-30) { gotoAndStopRand(this_clip); this_clip.y = stage.stageHeight; this_clip.x = Math.random()*stage.stageWidth; } } //if snow has been removed then clear all listeners function removed(event:Event){ event.currentTarget.removeEventListener(Event.ENTER_FRAME, this_onEnterFrame); event.currentTarget.removeEventListener(Event.REMOVED_FROM_STAGE, removed); } clip.addEventListener(Event.REMOVED_FROM_STAGE,removed); clip.addEventListener(Event.ENTER_FRAME, this_onEnterFrame); // }; //Call this to make function createSnow(snowAmount:Number, windSpeed:Number) { for ( var i:Number = 0; i

-----------
Button Over And Out Animation With Enter Frame
DOWNLOAD SOURCE
[swfobj src="http://nathalielawhead.com/sourcefiles/Haxed_By_Megahurtz/snippets_AS3/rollOver_rollOut_EnterFrame.swf" height="286" width="340"]

Code:

//onRollOver mc animations
function onRollOverMC(myBtn:Object, myMc:MovieClip) {
//
function myMc_onEnterFrame_Over(event:Event) {
myMc.gotoAndStop(myMc.currentFrame+1);
if (myMc.currentFrame == myMc.totalFrames) {
myMc.removeEventListener(Event.ENTER_FRAME, myMc_onEnterFrame_Over);
}
};
//
function myMc_onEnterFrame_Out(event:Event) {
myMc.gotoAndStop(myMc.currentFrame-1);
if (myMc.currentFrame == myMc.totalFrames) {
myMc.removeEventListener(Event.ENTER_FRAME, myMc_onEnterFrame_Out);
}
};
//
function myBtn_onRollOver(event:MouseEvent) {
myMc.addEventListener(Event.ENTER_FRAME, myMc_onEnterFrame_Over);
myMc.removeEventListener(Event.ENTER_FRAME, myMc_onEnterFrame_Out);
};
//
function myBtn_onRollOut(event:MouseEvent) {
myMc.addEventListener(Event.ENTER_FRAME, myMc_onEnterFrame_Out);
myMc.removeEventListener(Event.ENTER_FRAME, myMc_onEnterFrame_Over);
};
//
if (!myBtn.hasEventListener(MouseEvent.MOUSE_OVER)) {
myBtn.addEventListener(MouseEvent.MOUSE_OVER, myBtn_onRollOver);
}
if(!myBtn.hasEventListener(MouseEvent.MOUSE_OUT)){
myBtn.addEventListener(MouseEvent.MOUSE_OUT, myBtn_onRollOut);
}
}

onRollOverMC(mc_play, mc_play_back);