Go Back   kirupaForum > Flash > ActionScript 3.0

Reply
 
Thread Tools Display Modes
Old 08-07-2006, 12:09 AM   #136
bodyvisual
♥♥♥
god i'm so scared of as3 ok?

__________________
bodyvisual.com
bodyvisual is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?
 

Old 08-07-2006, 12:00 PM   #137
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
for..in and for each..in

ActionScript 3 has a new iteration statement: for each..in (for each..in). for each..in works much like for..in (for..in) execpt it loops through the values of an object rather than its keys. Example:

ActionScript Code:
var object:Object = new Object();
object.name = "senocular";
object.id = 2867;
object.isModerator = true;
for each (var value:* in object){
    trace(value);
}
/* Output:
true
2867
senocular
*/

Respectively, a for..in would look like:
ActionScript Code:
var object:Object = new Object();
object.name = "senocular";
object.id = 2867;
object.isModerator = true;
for (var key:String in object){
    trace(key + ": " + object[key]); // object[key] is value
}
/* Output:
isModerator: true
id: 2867
name: senocular
*/

Note that the key is not available in for each..in.

For dense arrays (arrays with no gaps in element definitions), ActionScript 3 also maintains array element order (using numeric array indices) when using for..in and for each..in rather than basing order off of when the value was created as was the case in ActionScript 1 and 2. Example:
ActionScript Code:
var array:Array = new Array();
array[1] = 1;
array[0] = 2;
array[2] = 3;
for (var key:String in array){
    trace("array[" + key + "] = "+ array[key]);
}

Output AS 1 & AS 2:
Code:
array[2] = 3
array[0] = 2
array[1] = 1
Output AS 3:
Code:
array[0] = 2
array[1] = 1
array[2] = 3
This provides a more intuitive order when iterating through arrays with for..in and for each..in.

Note: To use the for each..in statement with an instance of a user-defined class, you must declare the class with the dynamic attribute.

__________________
senocular is offline   Reply With Quote
Old 08-09-2006, 10:55 AM   #138
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
Default Values for Function Parameters

ActionSript 3 now allows you to specify default values for your function's parameters. In doing so, that parameter then becomes optional and the default value assigned to the respective argument in the function call if a value is explicitly not provided.
ActionScript Code:
function method(required:String, optional:String = "default"):void {
    trace(required +" "+optional);
}
method("Hello"); // "Hello default"
 

You can only place parameters with default values after all required parameters. In other words you can't have
ActionScript Code:
// Incorrect:
function method(required:String = "default", optional:String):void { ...

since there would be no way for optional to be set without required having to be set as well.

__________________
senocular is offline   Reply With Quote
Old 08-09-2006, 11:04 AM   #139
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
Undetermined Number of Arguments With ...(rest)

Since ActionSript 3 checks argument count when functions are called you are not able to pass any number of arguments to any function like you could in ActionScript 1 or ActionScript 2. Instead, to allow for this, you need to use a new special kind of parameter called ...(rest) (Keyword: ...(rest)).

The ...(rest) parameter is a special parameter placed at the end of a parameter list in a function that specifies that there can be any number of additional arguments of any type passed into that function when its called. The form of the parameter is 3 periods followed by a keyword. When the function is called, the additional arguments are assigned to that keyword in the form of an array.
ActionScript Code:
function usingRest(required:Number, ... optionalArgs):void {
    trace(required); // 1
    trace(optionalArgs); // [2, 3, 4]
}
usingRest(1, 2, 3, 4);

__________________
senocular is offline   Reply With Quote
Old 08-09-2006, 11:14 AM   #140
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
arguments

As with ActionScript 1 and ActionScript 2, ActionScript 3 has an arguments (Top level arguments) object that is a special object created for each function call when it is executed in Flash. In ActionScript 3, however, there is no arguments.caller property. To get a reference to the caller, you must pass a reference to that function as an argument. The callee property still exists.
ActionScript Code:
function args(str:String, num:Number):void {
    trace(arguments);
}
args("a", 1); // ["a", 1]
 


Unfortunately, the arguments array does not exist when using the ...(rest) parameter meaning the arguments array can only be used when ...(rest) is not. This is something to consider if you are using ...(rest) but still want a reference to callee.

__________________
senocular is offline   Reply With Quote
Old 08-10-2006, 03:59 AM   #141
Increu
Ancient Evil Spirit
 
Increu's Avatar
Location councious of a plane's spirit

Posts 30
Hey Sen, when you run out of ideas, maybe you can talk about ByteArray, it would be really cool
Increu is offline   Reply With Quote
Old 08-10-2006, 12:03 PM   #142
gburks
Registered User
external swf as cursor 'gets in the way'

RE: your tip of the day, "Detecting When the Mouse Leaves the Movie"

I noticed behavior that didn't exist in AS2 that now happens in AS3: When you use an externally loaded SWF to replace the mouse cursor, the loaded movie prevents the hit area from triggering if the movie is directly below the cursor.

I noticed this when trying to load an swf with animation. My buttons would return to the upState as the animation moved below the cursor, and again go back to the overState as the animation moved out of the way.

Attached is a modified version of your class that replaced the cursor. It includes a button on the stage and an animated externally loaded cursor. I left the real mouse pointer visible to demonstrate the "on-off" switching.
Attached Files
File Type: zip CursorTestAnimated.zip (12.4 KB, 111 views)

Last edited by gburks; 08-10-2006 at 12:06 PM..
gburks is offline   Reply With Quote
Old 08-10-2006, 11:54 PM   #143
gburks
Registered User
solution -
mouseEnabled = false; on the externally loaded movie.
gburks is offline   Reply With Quote
Old 08-11-2006, 01:10 PM   #144
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
Support for Namespaces

ActionScript 3 now supports namespaces. Namespaces in AS3 are similar to namespaces in XML and provide a way to separate code in to separate "spaces" or collections identifiable through a name (namespace). Namespaces in this respect act much like packages. In the same manner that packages allow you to have different classes with the same name in one application (only defined in different packages), namespaces allow you to have different methods with the same name in one class (defined in different namespaces). Though you may not have known it, chances are you've already been using namespaces. Predefined namespaces include public, private, protected, and internal.

When you use a namespace, you have to declare it just like you declare any other class member. Namespaces are declared using the namespace keyword (namespace Keyword). Once declared you can use it to separate members into different namespaces. Ex:
ActionScript Code:
package {
   
    public class UsingNameSpaces {
       
        public namespace company;
        public namespace individual;
       
        company var value:int = 10;
        individual var value:int = 2;
       
        public function UsingNameSpaces(){
        }
       
        company function showValue() {
        }
       
        individual function showValue() {
        }
    }
}

Two namespaces were used here, company and individual. They were used to defined a value variable and a showValue method - one for each namespace. Though they have the same names, since they are in different namespaces, they are allowed.

Additionally, namespaces can otionally be defined with a namespace uri when declared in the class:
ActionScript Code:
package {
   
    public class UsingNameSpaces {
       
        public namespace company = "http://www.example.com/company";
        public namespace individual = "http://www.example.com/individual";
       
        company var value:int = 10;
        individual var value:int = 2;
       
        public function UsingNameSpaces(){
        }
       
        company function showValue() {
        }
       
        individual function showValue() {
        }
    }
}

__________________
senocular is offline   Reply With Quote
Old 08-11-2006, 01:10 PM   #145
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
Namespaces: Name Qualifier Operator (::)

When you want to access a class member in a namespace, you need to reference that member through the namespace in which it was defined. One way to do this is through the name qualifier operator (name qualifier operator).

The name qualifier operator takes the form of two colons that connects the namespace with the member of the class you are trying to access that exists within that namespace, eg. namespace::member. Ex:
ActionScript Code:
package {
   
    public class UsingNameSpaces {
       
        public namespace company = "http://www.example.com/company";
        public namespace individual = "http://www.example.com/individual";
       
        company var value:int = 10;
        individual var value:int = 2;
       
        public function UsingNameSpaces(){
            company::showValue(); // traces 10
            individual::showValue(); // traces 2
        }
       
        company function showValue() {
            trace(company::value);
        }
       
        individual function showValue() {
            trace(individual::value);
        }
    }
}

Note that even though showValue is being called within the namespace, the namespace is still needed to reference variables (or other members) in namespaces used within the function body.

__________________
senocular is offline   Reply With Quote
Old 08-11-2006, 02:29 PM   #146
disobey design
Registered User
Very interesting stuff on namespaces, Senocular, but I have a question:

How would you reference class members from outside the class?

I assume it would be like this:

Code:
var myObj = new UsingNameSpaces();
myObj.company::showValue();

Is that correct? Are the namespace declarations treated as any other class property?
disobey design is offline   Reply With Quote
Old 08-11-2006, 05:04 PM   #147
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
Quote:
Originally Posted by disobey design
Very interesting stuff on namespaces, Senocular, but I have a question:

How would you reference class members from outside the class?

I assume it would be like this:

Code:
var myObj = new UsingNameSpaces();
myObj.company::showValue();

Is that correct? Are the namespace declarations treated as any other class property?
namespaces, as I have written them, aren't necessarily like properties - they're more like classes.

The code you have is correct, but will not work with the UseNameSpaces class as I have written it since the namespaces were defined in the class body. For your code to work, you would need to define the namespaces in the package body (in the same block the class is defined).
ActionScript Code:
package {
   
    public namespace company = "http://www.example.com/company";
    public namespace individual = "http://www.example.com/individual";
    public class UsingNameSpaces { ... }
}


There is also a way to define namespaces as properties but I will cover that later.

__________________
senocular is offline   Reply With Quote
Old 08-11-2006, 05:06 PM   #148
a.neuhaus
Registered User
 
a.neuhaus's Avatar
Tween in AS3

It might sound like a amenitie to developers, but I'm a designer so it is a bless to me.
What happened to it? All my attempts to import it return an error:

Code:
package{

    import mx.effects.Tween;
    import flash.display.MovieClip;
    
    public class Main extends MovieClip{


        public function Main(){
            
            var t:Tween = new Tween();
            
        }
    }
}
Error #1046: Type was not found or was not a compile-time constant: Tween.

Is there a way to overcome this missing class?
Or, is there a class in the new API with its functionality?

Thanks in advance.
a.neuhaus is offline   Reply With Quote
Old 08-11-2006, 05:16 PM   #149
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
this was addressed earlier in the thread

__________________
senocular is offline   Reply With Quote
Old 08-11-2006, 05:36 PM   #150
matzo
Registered User
I don't know if this really is the place, but many people already asked their as3 questions here, so here I go. I tried the alpha version from flash 9 with this code
Code:
 
import flash.display.*;
var Filter:GradientGlowFilter = new GradientGlowFilter(4.0, 90,[0x00FF00], [50], [3], 4.0, 4.0, 1, 1, "outer");
var Movie:MovieClip = new MovieClip();
Movie.graphics.lineStyle(1);
Movie.graphics.drawRect(10,10,100,100);
addChild(Movie);
Movie.filters=new Array(Filter);
Now, I've tested it and it worked.(Jiehaa!)
But, then I notices that I forgot to import flash.filters.*;
Though, it worked without any problem?
Is this a bug, or is this really the intend of Adobe.

(yeah you may have noticed that English is not at all my native language so.., sorrymountie: )
matzo is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 26 (0 members and 26 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 05:36 PM.

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components
Flash effects. Art without coding. Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com
Streamsolutions Content Delivery Networks Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Learn how to advertise on kirupa.com
 

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