The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Page 10 of 45 FirstFirst ... 8910111220 ... LastLast
Results 136 to 150 of 669

Thread: ActionScript 3 Tip of the Day

  1. #136
    god i'm so scared of as3 ok?

  2. #137

    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:

    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:
    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:
    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.

  3. #138

    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.
    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
    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.

  4. #139

    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.
    Code:
    function usingRest(required:Number, ... optionalArgs):void {
    	trace(required); // 1
    	trace(optionalArgs); // [2, 3, 4]
    }
    usingRest(1, 2, 3, 4);

  5. #140

    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.
    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.

  6. #141
    Increu's Avatar
    30
    posts
    Ancient Evil Spirit
    Hey Sen, when you run out of ideas, maybe you can talk about ByteArray, it would be really cool

  7. #142

    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 Attached Files
    Last edited by gburks; August 10th, 2006 at 11:06 AM.

  8. #143
    solution -
    mouseEnabled = false; on the externally loaded movie.

  9. #144

    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:
    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:
    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() {
    		}
    	}
    }

  10. #145

    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:
    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.

  11. #146
    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?

  12. #147
    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).
    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.

  13. #148

    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.

  14. #149
    this was addressed earlier in the thread

  15. #150
    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: )

Page 10 of 45 FirstFirst ... 8910111220 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012