Super Simple Script Snippets – Part 2

Share Button

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

If Removed From Stage Remove All Listeners
Download Here

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

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

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

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);

This monster belongs in Games, Web/Game Dev: Tutorials & Sourcecode. Bookmark the permalink. Follow any flames or other attacks to it with the RSS feed for this abominable creature. Voice your rage or leave a /b/roback: /b/roback URL.

Post a /B/romment

Chill, /b/ro. Your email is probably not, but most likely impossibly going to be not shared. Required fields are marked, yo... *

*
*

Use these /b/ro HTML haxxx: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Beerme: