Go Back   kirupaForum > Flash > ActionScript 3

Reply
 
Thread Tools Display Modes
Old 07-26-2006, 01:21 AM   #106
NiñoScript
mesiah's lil bro
 
NiñoScript's Avatar
Location Earth, SouthAmerica, Chile, Quinta Region, Viña del Mar, 4 norte 1329, third floor, my house, my room, in front of my computer.

Posts 664
Quote:
Originally Posted by Senocular
trace(value1, value2, value3);
is there anyway so i can make a function (or class method) that accepts any number of arguments?

__________________
My real name is: Cristián Arenas Ulloa.

Member #1 Of The NiñoScript's Club
Member #4 Of The I Want A Mac Club
Member #7 Of The Kirupa Anime Club
Member #1 Of The Courier Font Club
NiñoScript is offline   Reply With Quote
Old 07-26-2006, 04:14 AM   #107
Krilnon
≈ ≠ =
 
Krilnon's Avatar
Location Rochester, NY

Posts 7,709
Quote:
Originally Posted by NiñoScript
is there anyway so i can make a function (or class method) that accepts any number of arguments?
Yep, use ... (rest). Senocular mentioned it in this post, and the documentation on it is here.
Krilnon is online now   Reply With Quote
Old 07-26-2006, 10:44 AM   #108
NiñoScript
mesiah's lil bro
 
NiñoScript's Avatar
Location Earth, SouthAmerica, Chile, Quinta Region, Viña del Mar, 4 norte 1329, third floor, my house, my room, in front of my computer.

Posts 664
wow, thats cool!

now, one more question... is there anyway to pass that array as single arguments for another function?

something like:
Code:
function _trace(... rest):Array {
    trace(rest);
/* well... this is not a good example
    couse trace can accept an array as argument...
    but think about a function that can accept any number of Numbers
    but does nothing if you pass an Array to it */
    return rest;
}

__________________
My real name is: Cristián Arenas Ulloa.

Member #1 Of The NiñoScript's Club
Member #4 Of The I Want A Mac Club
Member #7 Of The Kirupa Anime Club
Member #1 Of The Courier Font Club
NiñoScript is offline   Reply With Quote
Old 07-26-2006, 10:45 AM   #109
senocular
On Vacation
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,426
use Function.apply() just like in AS1 and AS2

__________________
senocular is offline   Reply With Quote
Old 07-26-2006, 12:57 PM   #110
NiñoScript
mesiah's lil bro
 
NiñoScript's Avatar
Location Earth, SouthAmerica, Chile, Quinta Region, Viña del Mar, 4 norte 1329, third floor, my house, my room, in front of my computer.

Posts 664
ohh, i never used that
thanks

__________________
My real name is: Cristián Arenas Ulloa.

Member #1 Of The NiñoScript's Club
Member #4 Of The I Want A Mac Club
Member #7 Of The Kirupa Anime Club
Member #1 Of The Courier Font Club
NiñoScript is offline   Reply With Quote
Old 07-29-2006, 10:51 AM   #111
senocular
On Vacation
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,426
is Operator (vs instanceof)

The is operator (is operator) is a new keyword that lets you check to see if an instance is of a certain object type. This works for ActionScript 3 class instances checked against class types as well as interfaces. Ex:
Code:
var mySprite:Sprite = new Sprite();
trace(mySprite is Sprite);           // true
trace(mySprite is DisplayObject);    // true
trace(mySprite is IEventDispatcher); // true
The is operator is a replacement for instanceof (instanceof operator) from ActionScript 1 and 2. The is operator, however, is specific to AS3 classes and is used to check the inheritance specific to those classes. This will not work on dynamic classes created with AS1-style constructor functions. In that situation, you would use instanceof since instanceof checks the prototype chain of an instance as opposed to class inheritance (which is does). Because an AS3 class's class inheritance is mirrored in its prototype chain, this means that instanceof will also work for AS3 classes, though is is prefered then (and less typing!).

Code:
var mySprite:Sprite = new Sprite();
trace(mySprite instanceof Sprite);           // true
trace(mySprite instanceof DisplayObject);    // true
trace(mySprite instanceof IEventDispatcher); // false - not in prototype chain
Code:
var AS1StyleClass:Function = function(){}
AS1StyleClass.prototype = new MovieClip();

var as1Instance:* = new AS1StyleClass();
trace(as1Instance instanceof AS1StyleClass);    // true
trace(as1Instance instanceof MovieClip);        // true
trace(as1Instance is AS1StyleClass);    // true
trace(as1Instance is MovieClip);        // false - cant see prototype chain

__________________
senocular is offline   Reply With Quote
Old 07-29-2006, 11:24 AM   #112
senocular
On Vacation
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,426
Flash 9: Timelines as Classes

In Flash 9, you are able to associate all timelines with classes, including the root timeline. Movie clip timelines are associated with classes the same way as ActionScript 2 using the linkage dialog. The root timeline can be given a class association through the property inspector or through the publish settings (in ActionScript settings).

If you do not associate a timeline with a class, one is automatically created for that timeline by Flash. When this happens, variables become class variables, all named functions (declared using function functionName(){}) become methods of that class, and all scripts within the frames associated with methods that are automatically called when that frame is reached (minus the variable and method definitions). Because of this, you can only declare variables and named functions of any specific name for a function once. Variables you can redefine, but not redeclare. Functions (methods) are locked. Note: You cannot mix classes associated with timelines and timeline scripts.

The following timeline script in Flash 9
Code:
var num:Number = 1;
function showNum():void {
	trace(num);
}
showNum();
essentially becomes the following AS3 class that gets associated with that timeline:
Code:
package {
	class TimelineClass extends MovieClip {
		
		public var num:Number;

		public function showNum():void {
			trace(this.num);
		}
		public function frame1():void {
			num = 1;
			showNum();
		}
	}
}

__________________
senocular is offline   Reply With Quote
Old 07-29-2006, 11:49 AM   #113
senocular
On Vacation
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,426
RegExp: Email Validation

Here's an example of how one might validate email using ActionScript 3's Regular Expression class (Top level RegExp):

Code:
function isValidEmail(email:String):Boolean {
	var emailExpression:RegExp = /^[a-z][\w.-]+@\w[\w.-]+\.[\w.-]*[a-z][a-z]$/i;
	return emailExpression.test(email);
}
//...
trace(isValidEmail("senocular@example.com")); // true
trace(isValidEmail("@example.com")); // false
trace(isValidEmail("senocular@example")); // false
trace(isValidEmail("seno\\cular@example.com")); // false

__________________
senocular is offline   Reply With Quote
Old 07-29-2006, 12:16 PM   #114
senocular
On Vacation
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,426
Render Event

ActionScript has long since relied on the enterFrame (onEnterFrame) event for time-based actions, especially animation and actions relating to frame playback. In comparison, Director's Lingo language has, not only an enterFrame event, but two other frame events, prepareFrame and exitFrame. Though ActionScript 3 has not aquired prepareFrame or exitFrame, it has gained a new frame event, render, or Event.RENDER (flash.events.Event.RENDER).

The render event in AS3 is a frame event that occurs after enterFrame (flash.events.Event.ENTER_FRAME) allowing one more chance to do what you need to do before the screen updates its display.

Unlike enterFrame, however, the render event will not be called unless the display object using it is attached to a stage (or in any display list attached to the stage). Also, render is not automatically called every frame, even if attached to the stage. In order for render to be called for the current frame, you must make a call to stage.invalidate() (flash.display.Stage.invalidate());

Ex:
Code:
var sprite:Sprite = new Sprite();
stage.addChild(sprite);

sprite.addEventListener(Event.ENTER_FRAME, enterFrame);
sprite.addEventListener(Event.RENDER, render);
stage.addEventListener(MouseEvent.CLICK, click);

function enterFrame(event:Event):void {
	trace("enter frame");
}
function render(event:Event):void {
	trace("render");
}
function click(event:MouseEvent):void {
	trace("click");
	stage.invalidate();
}
Code:
Output:
enter frame
enter frame
enter frame
click
enter frame
render
enter frame
enter frame
enter frame
enter frame
...

__________________
senocular is offline   Reply With Quote
Old 07-29-2006, 12:37 PM   #115
senocular
On Vacation
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,426
XML: @ Operator for Attributes

E4X (XML used in ActionScript 3) has new operators to access values in XML. One operator is the @ operator which accesses attributes. It can be used in place of the attribute() (Top level XML.attribute()) method for obtaining attribute values. Ex:

Code:
var myXML:XML = <user name="senocular" id="2867" />;
trace(myXML.attribute("name")); // senocular
trace(myXML.attribute("id")); // 2867
trace(myXML.@name); // senocular
trace(myXML.@id); // 2867
You can also use an asterisk (*) with the @ operator to get a list of all attributes associated with an XML node in the form of an XMLList object. This is equivalent to the attributes() (Top level XML.attributes()) method. Ex:

Code:
var myXML:XML = <user name="senocular" id="2867" />;
var atts:XMLList;

atts = myXML.attributes();
trace(atts.toXMLString());
/* Output:
senocular
2867
*/
atts = myXML.@*;
trace(atts.toXMLString());
/* Output:
senocular
2867
*/

__________________
senocular is offline   Reply With Quote
Old 07-29-2006, 12:40 PM   #116
senocular
On Vacation
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,426
sorry about the delay this week; I got slammed at work

__________________
senocular is offline   Reply With Quote
Old 07-30-2006, 09:27 AM   #117
Deviant1853
Actionscripter
 
Deviant1853's Avatar
Quote:
Originally Posted by senocular
ActionScript 3 lets you easily obtain any instances class name using a new function called getQualifiedClassName (flash.utils.getQualifiedClassName).
ActionScript Code:
var sprite:Sprite = new Sprite();
trace(getQualifiedClassName(sprite)); // "flash.display::Sprite"




You can also use the getQualifiedSuperclassName (flash.utils.getQualifiedSuperclassName) function to find its superclass
ActionScript Code:
trace(getQualifiedSuperclassName(sprite)); // "flash.display:isplayObjectContainer"




If you want to go backwards, and convert a string into an actual class reference, you can use getDefinitionByName (flash.utils.getDefinitionByName).
ActionScript Code:
trace(getDefinitionByName("flash.display::Sprite")); // [class Sprite]


With getDefinitionByName you should in theory be able to do some basic Object Reflection ? I guess you would have to do the method information manually though...
Deviant1853 is offline   Reply With Quote
Old 07-30-2006, 11:44 AM   #118
senocular
On Vacation
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,426
Quote:
Originally Posted by Deviant1853
With getDefinitionByName you should in theory be able to do some basic Object Reflection ? I guess you would have to do the method information manually though...
Did you see devonair's mention of describeType in the following post?

__________________
senocular is offline   Reply With Quote
Old 07-30-2006, 01:13 PM   #119
senocular
On Vacation
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,426
Event Propagation Support

ActionScript 3 now supports event propagation - the transference of a single event applying to multiple objects to each of those objects instead of one - in Display objects. In ActionScript 1 and 2, "Button" events (such as onPress, onRelease, etc.) handled by movie clips were not propagated to that movie clip's children. This means that though visually you were clicking on a child of a movie clip handling an onPress event, that onPress would never make it to the child because the parent movie clip handling the event would intercept it and prevent the propagation of the onPress event to that child.

Example; movie clip parent_mc contains child_mc:
Code:
// AS1 and AS2
parent_mc.onPress = function(){
	trace("parent pressed");
}
parent_mc.child_mc.onPress = function(){
	trace("child pressed");
}

/* click child output:
parent pressed
*/
Code:
// AS3
parent_mc.addEventListener(MouseEvent.CLICK, parentClick);
parent_mc.child_mc.addEventListener(MouseEvent.CLICK, childClick);

function parentClick(event:MouseEvent):void {
	trace("parent pressed");
}
function childClick(event:MouseEvent):void {
	trace("child pressed");
}

/* click child output:
child pressed
parent pressed
*/
In ActionScript 1 and 2, the child is never able to receive the event. In ActionScript 3, both movie clips are able to receieve it as the event is propagated to each movie clip to which it applies.

__________________
senocular is offline   Reply With Quote
Old 07-31-2006, 11:06 AM   #120
senocular
On Vacation
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,426
Get Sound Spectrum Information

Using ActionScript 3 you can now obtain sound spectrum information from audio played through Flash. This lets you create visualizations like those seen in popular in media player applications. The class that provides this information is the SoundMixer class (flash.media.SoundMixer). It's computeSpectrum method (static) places sound spectrum information in a ByteArray instance which can then be used to generate a visualization.
Code:
// play sound...
var spectrumInfo:ByteArray = new ByteArray();
SoundMixer.computeSpectrum(spectrumInfo);
// spectrumInfo is now a byte array with sound spectrum info

__________________
senocular is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 15 (0 members and 15 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:31 AM.

SHARE:

SUPPORTERS:

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com