Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

onFrameNumber.as

Name: onFrame# [MX] - two new events onFrame# and onFrameLived#
Author: senocular: www.senocular.com
Date: 1899-12-31T00:03:18.300
Documentation:
MovieClip ONFRAME# and ONFRAMELIVED#: two new movieclip
events based on what frame the movieclip is on currently.
onFrame# (replacing # with a frame number of your choice) will
call the set function when the movieclip is within that frame.
onFrameLived# (replacing # with a number of your choice) will
call the set function when the clip has been on the screen or
having been in existance for the set number of frames. In doing
this, a new property is created _frameslived.  This represents
the number of frames which have passed since the MovieClip was
created or appeared on the screen.

Arguments:
none

Returns:
none

Note: None of the events are deleted from the clip and continue to exist after being called.
You may want to delete them after running - namely the onFramesLived calls since they are 
only reached once, at least not unless you reset _frameslived which will go through and
recall any pre-existing onFrameLived events (which is why I didn't add the auto-deletion
in this posted example)

Example:
// call to be run when the movieclip
// named 'myClip' reaches frame 24
myClip.onFrame24 = function(){
	trace("Frame 24 reached!");
}

// call to be run when the movieclip
// named 'myClip' has been on the screen
// or in existance for 100 frames
myClip.onFrameLived100 = function(){
	trace("100 frames have passed since I was created!");

	// and the following will happen after 10 more frames
	this["onFrameLived"+(this._frameslived+10)] = function(){
		trace("and this... 10 frames after that!");
	}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
MovieClip.prototype.$onEnterFrame = function(){
	if (!this._frameslived++) ASSetPropFlags(this,["$onEnterFrame","_framesLived"],1);
	this["onFrame"+this._currentframe]();
	this["onFrameLived"+this._frameslived]();
}
MovieClip.prototype.addProperty("onEnterFrame",
	function(){
		return this.$onEnterFrame;
	},function(f){
		this.$onEnterFrame = function(){
			MovieClip.prototype.$onEnterFrame.call(this);
			f.call(this);
		}
	}
);