ActionScript 的SoundLoader
import mx.events.EventDispatcher;
import mx.utils.Delegate;
/**
* Loads a list of sounds "in the background" without playing them
* Allows client to request a specific sound in priority
*
* Usage
*
* var soundLoader:SoundLoader = new SoundLoader(this,
* "soundLoader",
* this.getNextHighestDepth());
* soundLoader.addEventListener("loadProgress",
* Delegate.create(this,
* soundLoaderLoading));
* soundLoader.addEventListener("doneLoading",
* Delegate.create(this,
* soundLoaderLoaded));
* function soundLoaderLoading(event:Object)
* {
* trace("Loading " + Math.round(event.percent * 100) + " out of " + Math.round(event.globalPercent * 100));
* }
* function soundLoaderLoaded(event:Object)
* {
* trace("soundLoader doneLoading");
* }
*
* // Add sounds to queue
* soundLoader.push("audio/audio10.mp3");
* soundLoader.push("audio/audio11.mp3");
* soundLoader.push("audio/audio20.mp3");
*
* // Start the load queue
* soundLoader.startLoad();
*
* // Request a sound in priority
* var soundHandler:Object = {};
* soundHandler.soundLoaded = function(event:Object):Void
* {
* soundLoader.removeEventListener("soundLoaded", this);
* trace("Priority sound loaded");
* }
* soundLoader.addEventListener("soundLoaded", soundHandler);
* // Retrieve sound by file name (no path or extension)
* soundLoader.getSound("audio20");
*
*/
class com.orazal.SoundLoader
{
public var dispatchEvent:Function;
public var addEventListener:Function;
public var removeEventListener:Function;
private var sprite:MovieClip;
private var soundList:Array /* of Object */;
private var current:Number;
private var interval:Number;
private var prioritySound:Sound;
private var priorityPosition:Number;
private var loadingPriority:Boolean;
/**
* Constructor, assign sounds to a movieclip to be able to control volume independently
* of _root
*
* @param target
* @param name
* @param depth
*/
public function SoundLoader(target:MovieClip, name:String, depth:Number)
{
EventDispatcher.initialize(this);
sprite = target.createEmptyMovieClip(name, depth);
soundList = [];
loadingPriority = false;
}
/**
* Adds a sound url to load queue creating a new sound for the url
* and assigning the sound a specific id
* @param url
*/
public function push(url:String):Void
{
// Retrieve file name only
var id:String = url.slice(url.indexOf("/")+1, url.indexOf("."));
var newSound:Sound = new Sound(sprite);
newSound.onLoad = Delegate.create(this, onLoad);
soundList.push( { url:url, sound:newSound, loaded:false , id:id} );
}
/**
* Start sound loading
*/
public function startLoad():Void
{
current = 0;
Sound(soundList[0].sound).loadSound(String(soundList[0].url));
interval = setInterval(Delegate.create(this, checkProgress), 100);
}
/**
* Sets volume for sprite
*/
public function setVolume(value:Number):Void
{
sprite.setVolume(value);
// extra sure
for(var p:String in soundList)
{
Sound(soundList[p].sound).setVolume(value);
}
}
/**
* Retrieves a sound by id
*/
public function getSound(id:String):Void
{
// Retrieve sound position
for (var i:Number = 0; i < soundList.length; i++)
{
if (String(soundList[i].id) == id)
{
priorityPosition = i;
break;
}
}
// Either dispatch get sound event or load specific sound
// as a priority
if (soundList[priorityPosition].loaded)
{
dispatchGetSound();
}
else
{
loadSoundPriority(priorityPosition);
}
}
/**
* Changes load priority flag to stop loading other sounds
* Uses a specific sound and load handler to load sound
* Resumes load queue after sound has been loaded
*
* @param position
*/
private function loadSoundPriority(position:Number):Void
{
loadingPriority = true;
prioritySound = new Sound(sprite);
prioritySound.onLoad = Delegate.create(this, onLoadPriority);
prioritySound.loadSound(String(soundList[priorityPosition].url));
}
/**
* Priority sound onLoad handler
*/
private function onLoadPriority(success:Boolean):Void
{
continueLoading();
// update loading status
soundList[priorityPosition].loaded = true;
// replace original sound with priority sound
soundList[priorityPosition].sound = prioritySound;
dispatchGetSound();
}
/**
* Dispatches an event when a specific sound that has been requested has
* finishes loading
*/
private function dispatchGetSound():Void
{
var event:Object = { target:this, type:"soundLoaded" };
event.sound = Sound(soundList[priorityPosition].sound);
event.id = String(soundList[priorityPosition].id);
dispatchEvent(event);
}
/**
* Continues load queue
*/
private function continueLoading():Void
{
loadingPriority = false;
// If current is not loaded it will automatically load next
// when loaded or call finishLoading if it's last on list
if(soundList[current].loaded)
{
if (current < soundList.length - 1)
{
loadNext();
}
}
}
/**
* Loads next sound in queue
* @param success
*/
private function loadNext():Void
{
// Make sure that there's a next sound to load
if (current + 1 <= soundList.length - 1)
{
current++;
Sound(soundList[current].sound).loadSound(String(soundList[current].url), false);
}
}
/**
* Sound onLoad handler used for load queue
*
* @param success
*/
private function onLoad(success:Boolean):Void
{
// update loading status
soundList[current].loaded = true;
if (current == soundList.length - 1)
{
finishLoading();
return;
}
if (loadingPriority)
{
// onLoadPriority will resume loading
return;
}
if (current < soundList.length - 1)
{
loadNext();
}
}
/**
* Stop checking sound load and dispatches event
*/
private function finishLoading():Void
{
clearInterval(interval);
var event:Object = { target:this, type:"doneLoading" };
dispatchEvent(event);
}
/**
* Checks current sound loading percent and total percent
*/
private function checkProgress():Void
{
var currentSound:Sound = Sound(soundList[current].sound);
var percent:Number = currentSound.getBytesLoaded() / currentSound.getBytesTotal();
// Determine percentage
var loadedPercent:Number = current/soundList.length;
var globalPercent = (percent/soundList.length)+loadedPercent;
// Dispatch event
var event:Object = { target:this, type:"loadProgress" };
event.current = current;
event.percent = percent;
event.globalPercent = globalPercent;
dispatchEvent(event);
if(globalPercent == 1)
{
clearInterval(interval);
}
}
}
ActionScript AS2字距调整动态文本字段
function kernText(whichField, kerning)
{
whichField.html = true;
var newFormat:TextFormat = new TextFormat();
newFormat.letterSpacing = kerning;
//newFormat.font = "Arial";
whichField.setTextFormat(newFormat);
}
ActionScript ActionScript XML Parser
gallery = "xml/"+ _root.xmlFileVariable + ".xml";
photo = 0;
Photos_xml = new XML();
Photos_xml.onLoad = loadPhotos;
Photos_xml.load(gallery);
Photos_xml.ignoreWhite = true;
function loadPhotos(success) {
if (success == true) {
rootNodePhotos = Photos_xml.firstChild;
totalPhotos = rootNodePhotos.childNodes.length;
currentNodePhotos = rootNodePhotos.firstChild;
descriptions = new Array(totalPhotos);
PhotoUrl = new Array(totalPhotos);
BigUrl = new Array(totalPhotos);
links = new Array(totalPhotos);
titles = new Array(totalPhotos);
price = new Array(totalPhotos);
swf = new Array(totalPhotos);
dx = 0;
for (i=0; i < totalPhotos; i++) {
descriptions[i] = currentNodePhotos.firstChild.nodeValue;
PhotoUrl[i] = currentNodePhotos.attributes.PhotoUrl;
BigUrl[i] = currentNodePhotos.attributes.BigUrl;
links[i] = currentNodePhotos.attributes.link;
titles[i] = currentNodePhotos.attributes.title;
price[i] = currentNodePhotos.attributes.price;
swf[i] = currentNodePhotos.attributes.swf;
_root.images.attachMovie("photo","img"+i,i+300);
_root.images["img"+i]._x = dx;
_root.images["img"+i]._y = 0;
_root.images["img"+i].k = i;
dx = dx + 330;
_root.images["img"+i].Url = PhotoUrl[i];
_root.images["img"+i].BigUrl = BigUrl[i];
_root.images["img"+i].link = links[i];
_root.images["img"+i].title = titles[i];
_root.images["img"+i].swf = swf[i];
if (!price[i]){
_root.images["img"+i].price = "";
}else{
_root.images["img"+i].price = price[i];
}
currentNodePhotos = currentNodePhotos.nextSibling;
}
play();
_root.Move = true;
}
}
ActionScript AS2基本拖曳碰撞
#include "mc_tween2.as"
// if you don't have mc_tween, comment it out.
var startX:Number = 0; // original X position of item picked up
var startY:Number = 0; // original Y position of item picked up
var drawCollision:Boolean = true; // for debugging
box1_mc.onPress = function() { pickup(this, 1); };
box1_mc.onRelease = function() { dropoff(this, 1, box2_mc); };
//box1_mc.onRollOver = function() { rollover(this, 1); };
//box1_mc.onRollOut = function() { rollout(this, 1); };
function pickup(which_mc:MovieClip, intMC:Number):Void {
which_mc.swapDepths(this.getNextHighestDepth()); // bring picked-up object to foreground
which_mc.startDrag();
startX = which_mc._x;
startY = which_mc._y;
}
function dropoff(which_mc:MovieClip, intMC:Number, target_mc:MovieClip):Void {
which_mc.stopDrag();
if (isColliding(target_mc, which_mc)) // try removing the second parameter here. See function for more details.
{
status_txt.text += "\ncollided";
which_mc.slideTo(85, undefined, .5);
} else {
status_txt.text += "\nmissed";
which_mc.slideTo(startX, startY, .5);
}
}
function rollover(which_mc:MovieClip, intMC:Number):Void {
status_txt.text += "\nrolled over";
}
function rollout(which_mc:MovieClip, intMC:Number):Void {
status_txt.text += "\nrolled out";
}
function isColliding(target_mc:MovieClip, which_mc:MovieClip):Boolean
{
// If you do not send a "which_mc" this will assume you want to check on the mouse position (which will probably be within the target when the item is dropped)
obj1_x = target_mc._x;
obj1_y = target_mc._y;
if (which_mc) {
obj2_x = which_mc._x - Number(which_mc._width / 2);
obj2_y = which_mc._y - Number(which_mc._height / 2);
} else {
obj2_x = this._xmouse - 1;
obj2_y = this._ymouse - 1;
}
obj1_x2 = target_mc._width + obj1_x;
obj1_y2 = target_mc._height + obj1_y;
if (which_mc) {
obj2_x2 = which_mc._width + obj2_x;
obj2_y2 = which_mc._height + obj2_y;
} else {
obj2_x2 = 2 + this._xmouse;
obj2_y2 = 2 + this._ymouse;
}
if (drawCollision == true)
{
this.createEmptyMovieClip("rectangle_mc", 10);
rectangle_mc._x = obj2_x;
rectangle_mc._y = obj2_y;
if (which_mc) {
drawRectangle(rectangle_mc, which_mc._width, which_mc._height, 0x990000, 50);
} else {
drawRectangle(rectangle_mc, 2, 2, 0x990000, 50);
}
this.createEmptyMovieClip("rectangle2_mc", 11);
rectangle2_mc._x = obj1_x;
rectangle2_mc._y = obj1_y;
drawRectangle(rectangle2_mc, target_mc._width, target_mc._height, 0x99FF00, 50);
}
if ((obj2_x > obj1_x) && (obj2_x2 < obj1_x2) && (obj2_y > obj1_y) && (obj2_y2 < obj1_y2)) {
return true;
} else {
return false;
}
}
function drawRectangle(target_mc:MovieClip, boxWidth:Number, boxHeight:Number, fillColor:Number, fillAlpha:Number):Void {
with (target_mc) {
beginFill(fillColor, fillAlpha);
moveTo(0, 0);
lineTo(boxWidth, 0);
lineTo(boxWidth, boxHeight);
lineTo(0, boxHeight);
lineTo(0, 0);
endFill();
}
}
ActionScript actionscript - 日期对象
// create variable dateNow with the current date
var dateNow = new Date();
// create variable with specified date
var dateBirthday = new Date(1987,2,22);
// create variable with date from number
var dateFromNumber = new Date(543387600000);
// create variable with specified date+time
var dateBirthTime = new Date(1987,2,22,1,32);
// find a difference between two dates EG. find the number of days til next NYE:
// get current year, make date using that with December (month=11), day 31
var NYEdate = new Date((new Date()).getFullYear(), 11, 31);
var nowdate = new Date();
// convert difference in milliseconds to days
var nDiffDays = Math.floor((NYEdate - nowdate)/86400000);
trace(nDiffDays);
ActionScript actionscript - 加载XML [附带样式表信息]
// init TextArea component
blurb.html = true;
blurb.wordWrap = true;
blurb.multiline = true;
blurb.label.condenseWhite=true;
// load CSS
madrastyle = new TextField.StyleSheet();
madrastyle.load("madra.css");
blurb.styleSheet = madrastyle;
// load in XML
XMLcontent = new XML();
XMLcontent.ignoreWhite = true;
XMLcontent.load("kungfu.xml");
XMLcontent.onLoad = function(success)
{
if(success)
{
blurb.text = XMLcontent;
}
}
ActionScript actionscript 3 - 按钮动作[听一个单一的事件。例如。 MOUSE_UP]
// add an event listener to listen for a button release [mouse up] and then
// when it happens - trigger the function called 'buttonstuff'
this.stupidbutton.addEventListener(MouseEvent.MOUSE_UP, buttonstuff);
// buttonstuff function - triggered by the above listener
function buttonstuff(event:MouseEvent):void
{
// replace the trace with your own button actions
trace("button pressed");
}
// end buttonstuff function
ActionScript actionscript - 跟踪动作以获取有关FLV视频中提示点的信息
/* ||||||||||||||||| CUEPOINT TRACES ||||||||||||||||||||
these trace actions are handy for returning the info that flash has about any cuepoints it encounters, embedded in an FLV [flash video] file. put these traces inside a cuepoint listener function to test whether flash is picking up the cuepoints in the first place, before you add any more complicated code. these traces will display:
- the 'event' the listener has detected [ie. a cuepoint]
- the name of the cuepoint
- the type of the cuepoint [ie. 'event' or 'navigation']
- the time the cuepoint sits at in the video
[note: this code assumes your FLV video file has an instance name of 'vid'. if you change the instance name change the "this.vid.playheadTime" part of the code accordingly]
||||||||||||||||||||||||||||||||||||||||||| */
// begin traces
trace("listener detected: \t"+eventObject.type);
trace("cuepoint is called: \t"+eventObject.info.name);
trace("cuepoint is of type: \t"+eventObject.info.type);
trace("vid playhead time: \t"+vid.playheadTime);
trace("------\n");
// end traces
ActionScript actionscript - 闪存中FLV视频的cuepoint监听器
// add an event listener to the video to listen for cuepoints
// in this case the videoclip instance name is 'vid'
// if you change the instance name change the code accordingly
var listenerObject:Object = new Object();
listenerObject.cuePoint = function(eventObject:Object):Void
{
// stuff the function does goes below here
// stuff the function does goes above here
}
vid.addEventListener("cuePoint", listenerObject);
ActionScript ActionScript中的格式编号
function numberFormat(number, decimals, thousands_sep, decimal_sep) {
// var_number.number_format([decimals ,thousand separator,decimal separator]);
// var number: number to format
// decimals: how many decimal numbers (default value 0);
// thousand separator: char that define the thousandecimal_sep (default value ,);
// decimal separator: char the defines the decimals (default value .);
if(isNaN(number)) return undefined;
if(decimals < 0) return undefined;
if(decimals == undefined) decimals = 0;
if(thousands_sep == undefined) thousands_sep = ',';
if(decimal_sep == undefined) decimal_sep = '.';
var returned = number.toString().split('.'), str_begin, str_after, temp_str = "", i;
if(returned.length == 1) {
str_begin = returned[0]
str_after = '';
} else if(returned.length == 2) {
str_begin = returned[0];
str_after = returned[1];
str_after = str_after.substr(0, 2);
} else {
trace("uncaught number format");
}
// thousands seperator
if(str_begin.length > 3) {
for(i = 0; i < str_begin.length; i++) {
if(((str_begin.length - i) % 3) == 0 && i != str_begin.length - 1) {
temp_str = temp_str + thousands_sep + str_begin.charAt(i);
} else {
temp_str = temp_str + str_begin.charAt(i);
}
}
} else {
temp_str = str_begin;
}
// ----------------------
// decimals
// if decimals==0 return
// ----------------------
if(decimals > 0) {
str_after = str_after.substr(0, decimals);
if(str_after.length < decimals) {
while(str_after.length < decimals) {
str_after += '0';
}
}
}
if(decimals > 0) {
return temp_str + decimal_sep + str_after;
} else {
return temp_str;
}
}