Page 15 of 45 FirstFirst ... 5131415161725 ... LastLast
Results 211 to 225 of 669

Thread: ActionScript 3 Tip of the Day

  1. #211
    Are there any basic tutorials or samples to start with action script 3?
    I have developed many simple games in flash with AS 2.
    What I tried to do with AS 3.0 is, simple on(press). which didn't work.
    Can you tell me some basic tutorials, sort of "hello world" tutorials..

    Thanks...

  2. #212
    105
    posts
    Registered User
    In the duplicateDisplayObject example i get an error which i dont quite understand:

    Error: Access of possibly undefined property constructor through a reference with static type flash.displayisplayObject.

    var targetClass:Class = target.constructor;


    Ive tried many diffrent changes and read around in the manual, but i cant understand what im supposed to do.

  3. #213
    Quote Originally Posted by v_gyku
    Are there any basic tutorials or samples to start with action script 3?
    I have developed many simple games in flash with AS 2.
    What I tried to do with AS 3.0 is, simple on(press). which didn't work.
    Can you tell me some basic tutorials, sort of "hello world" tutorials..

    Thanks...
    Like AS2, AS3 classes (those defined with package) must be placed in external files. Code on the timeline is not explicitly defined in classes (though becomes part of a class as explained in tip 38).

    The first post in this thread has links to some documentation and tutorials.

  4. #214
    Quote Originally Posted by ven
    In the duplicateDisplayObject example i get an error which i dont quite understand:

    Error: Access of possibly undefined property constructor through a reference with static type flash.displayisplayObject.

    var targetClass:Class = target.constructor;


    Ive tried many diffrent changes and read around in the manual, but i cant understand what im supposed to do.
    That is the result of having strict mode turned on in your ActionScript 3 settings. Strict mode uses a more strict compiler check to determine the validity of your code and it doesn't recognize the constructor property because it is added to Object's prototype dynamically (as mentioned in tip 21). To get rid of that error you can turn off strict mode or use something like
    Code:
    var targetClass:Class = target["constructor"];
    which should be able to bypass it.

    Also, in using that function with Flash 9, make sure that you with the symbol(s) you are duplicating you export for ActionScript and give them explicit class names (even if the class is auto-generated). Otherwise, duplicateDisplayObject will not be able to duplicate the class properly.

  5. #215

    goto one movieclip from another

    There are two movie clips (background_mc, char_mc) on the main timeline.

    This is the code on the keyframe inside char_mc.

    Code:
    _root.background_mc.gotoAndPlay(2);
    I have searched macromedia live docs. I found that the closetst substitute for the _root is stage. I tried somthings which didn't work out.

    any suggestion?

    Thanks...
    Last edited by v_gyku; September 7th, 2006 at 12:00 PM.

  6. #216
    Quote Originally Posted by v_gyku
    ...
    Code:
    _root.background_mc.gotoAndPlay(2);
    I have searched macromedia live docs. I found that the closetst substitute for the _root is stage. I tried somthings which didn't work out...
    There has been a fair amount of discussion throughout this thread about the "stage" that should more than thoroughly answer your question.

    ~JC

  7. #217
    I will try to make the next post a comprehensive stage access post.

  8. #218
    I tried this and it worked.

    Code:
    root.background_mc.gotoAndPlay(2);
    Quote Originally Posted by v_gyku
    There are two movie clips (background_mc, char_mc) on the main timeline.

    This is the code on the keyframe inside char_mc.

    Code:
    _root.background_mc.gotoAndPlay(2);
    I have searched macromedia live docs. I found that the closetst substitute for the _root is stage. I tried somthings which didn't work out.

    any suggestion?

    Thanks...

  9. #219

    Access to stage and root

    In Flash it is often useful to reference the main timeline, or the root, of a movie. In ActionScript 3, the root instance of the SWF loaded into a player represents the first instance of the stage (flash.display.Stage). The stage object in Flash is the primary container in which all other display objects, including root, exist.

    A Flash application only has one stage. However, there can be multiple root instances in an application if external content is loaded into the player using the Loader class (flash.display.Loader).

    All DisplayObject (flash.display.DisplayObject) instances have stage and root properties. These properties, however, are null unless the display object is attached to the stage or a display list that is attached to the stage. The stage property, when accessible, will always reference the stage object. The root property references different objects depending on the display object:
    • For the stage, root always references the stage
    • For the main timeline of the SWF and all display objects within it, root references the main timeline
    • If an object is added directly to the stage from any timeline, the root property for it and its children references the stage
    • For display objects in loaded SWF files, root references the main timeline of that SWF file
    • For loaded bitmap images, root references the Bitmap instance of the image loaded
    • Loader objects used to load external SWFs and images follow the rules of all other display objects within the SWF it is being used


    Keep in mind that only display objects have access to stage and root and only when attached to the stage or a display list on the stage. This is contrary to ActionScript 1 and 2 in two respects:
    1. All "display objects" (movie clips) in AS1 and AS2 were attached to the "stage" or an active timeline upon creation. This is no longer true with AS3; in fact, the only display object which is inherently attached to the stage upon instantiation is the document class (or, in Flash 9, objects existing on the timeline in the IDE before publish).
    2. All ActionScript in AS1 and AS2 are defined in timelines, even classes. AS a result, essentially all scopes gained access to _root through the timelines (MovieClip instances) in which they were defined. Since classes in AS3 are not defined in timelines, they no longer have access to root (or stage) unless they themselves are DisplayObject instances already attached to the stage


    This greatly limits your access to stage and root within your scripts. For example, non-display object classes in AS3 can only access stage or root if explicitly given a reference. Ex:
    Code:
    package {
    	
    	import flash.display.Stage;
    
    	public class CustomObject {
    	
    		private var stage:Stage;
    	
    		public function CustomObject(stageRef:Stage) {
    		
    			// stage access through
    			// constructor argument
    			stage = stageRef;
    		}
    	}
    }
    Also, your display object classes will not inherently have stage or root access in their constructor unless they are used for the document/application class of your movie.
    Code:
    package {
    	
    	import flash.display.Sprite;
    	import flash.events.Event;
    
    	public class CustomDisplayObject extends Sprite {
    	
    		public function CustomObject() {
    		
    			// stage not acessible until
    			// added to stage or display
    			// list on the stage
    			trace(stage); // null
    		}
    	}
    }
    This can make it difficult if you need to access the stage for things like adding event listeners. You would need to either pass a reference of the stage (or root) to your custom display objects as with the CustomObject class or not attempt to access stage or root until the object has been added to a display list allowing it to be accessible. And for this you can manually call a setup or init function after the instance has stage accessible or detect it automatically using the StageDetection class (com.senocular.events.StageDetection). Note: As of Flash Player 9.0.28.0 - which was released with Flash CS3 - you have 2 new, native ADDED_TO_STAGE and REMOVED_FROM_STAGE events for detection access to the stage.

    Another approach to stage or root access would be to create a static property of a known class that would be accessible from any class. This class can be instantiated in the document class or it can be a DisplayObject subclass and you can have your document classes extend it as opposed to Sprite or MovieClip. For example, if you have your document classes always extend this TopLevel class, any class would be able to access stage or root (assuming they want root to be the document class extending this) using TopLevel.stage and TopLevel.root.
    Code:
    package {
    	
    	import flash.display.DisplayObject;
    	import flash.display.MovieClip;
    	import flash.display.Stage;
    	
    	public class TopLevel extends MovieClip {
    		
    		public static var stage:Stage;
    		public static var root:DisplayObject;
    			
    		public function TopLevel() {
    			TopLevel.stage = this.stage;
    			TopLevel.root = this;
    		}
    	}
    }
    Example document class using TopLevel
    Code:
    package {
    		
    	public class MyDocumentClass extends TopLevel {
    			
    		public function MyDocumentClass() {
    			// code
    		}
    	}
    }
    Example non-DisplayObject class used in MyDocumentClass application accessing stage:
    Code:
    package {
    		
    	public class RandomClass {
    			
    		public function RandomClass() {
    			trace(TopLevel.stage); // [object Stage]
    		}
    	}
    }
    Of course, ideally, you wouldn't need reliance on a class like TopLevel, especially if you are developing classes that might be shared with other developers. If someone is not in a position to have their document class extend TopLevel or simply don't have access to the class, any other class referencing it would fail. Passing around a reference of the stage (or root) or using the added (or added to stage) event(s) would be a safer way to go.

  10. #220

    Namespaces: use namespace Directive

    Like the name qualifier operator (name qualifier operator), the use namespace directive (use namespace directive) is a way for specifying namespaces in ActionScript 3. Unlike the name qualifier operator, use namespace is more generalize and applies to entire blocks of code rather than specfic references.

    When used it is placed in a code block (package, class, method) and applies to all references within that code block. Though you cannot nest use namespace commands, you can use the name qualifier operator to reference other namespaces.

    Example:
    Code:
    package {
       
    	public namespace company = "http://www.example.com/company";
    	public namespace individual = "http://www.example.com/individual";
    		
    	public class UsingNameSpaces {
           
    		use namespace individual;
    		
    		company var value:int = 10;
    		individual var value:int = 2;
           
            public function UsingNameSpaces(){
    			showValue(); // traces individual::2
    			company::showValue(); // traces company::2;
            }
           
            company function showValue() {
                trace("company::" + value);
            }
           
            individual function showValue() {
                trace("individual::" + value);
            }
        }
    }
    Notice the use namespace individual; line within the class block. This inherently implies that the namespace for all references in that block is for individual. This makes the first call to showValue call individual::showValue. company::showValue can still be referenced using the name qualifier, but because it wasnt used for the value variable within it, its value defaults to individual::showValue which is why it shows as 2 instead of 10.

  11. #221
    Are the namespace declarations supposed to be outside of the class definition? I run into errors when I have them declared outside of the class.
    “Who were you, Krilnon, and how did you know so much about AS4?”
    The historian sighed as she gazed up at the sky and saw… not stars. A story.

  12. #222
    Quote Originally Posted by Krilnon
    Are the namespace declarations supposed to be outside of the class definition? I run into errors when I have them declared outside of the class.
    You should be able to have them both out of and in the class. Having them in the class means that they are only accessible from within the class while outside in the package block, they are accessible anywhere the package is accessible. the mx_internal namespace, for example, would be defined in the package block outside of the class block (allowing you to import it).

    I compiled this example using Flash 9 alpha, not mxmlc, so there might be some differences there. What kind of errors are you getting? The only thing I can think of off the top of my head is possibility that, like with classes and packaged functions, namespaces defined outside of class blocks might need their own .as file for mxmlc.

  13. #223
    Originally I was testing this in another class, but I just switched to your example and the same error comes up:

    Quote Originally Posted by UsingNameSpaces.as
    A file found in a source-path can not have more than one externally visible definition. company;individual;UsingNameSpaces
    Quote Originally Posted by senocular
    The only thing I can think of off the top of my head is possibility that, like with classes and packaged functions, namespaces defined outside of class blocks might need their own .as file for mxmlc.
    That sounds reasonable.

    Edit: These errors aren't at compile-time, they are just given by Flex before I try to debug, however, trace isn't outputting anthing.
    Last edited by Krilnon; September 11th, 2006 at 08:16 PM.
    “Who were you, Krilnon, and how did you know so much about AS4?”
    The historian sighed as she gazed up at the sky and saw… not stars. A story.

  14. #224
    105
    posts
    Registered User
    About loading external xml

    I have problems finding some kind of errorhandling, like Flash 8 XML.status

    [Edit]

    Never mind, solved with:

    PHP Code:
    try {
        
    myxml.parseXML(loader.data);
        
    trace(myxml);
    } catch (
    error:Error) {
        
    trace("Error: "+error);

    And:Run-time Errors


    [Edit2]

    Actually, do mind...
    Cause using XMLDocument as i did ine the example (XMLDocument.parseXML) is the old XMLobject, while XML is the new E4X-one.


    When trying to get data from:

    myXML = loader.data;

    i get "Type Coercion failed" (error 1034)

    and when i try

    myXML = loader.data as XML;

    myXML still contains null (nothing)

    [Edit3]

    and thats solved so easy i have to take 100 pushups

    try {
    myXML = new XML(loader.data);
    (...)
    Last edited by ven; September 13th, 2006 at 03:42 AM.

  15. #225

    No More Color Class; Use ColorTransform

    ActionScript 3 does not have a Color class. Instead, you would use the ColorTransform class (flash.geom.ColorTransform). This is essentially the same ColorTransform class that was introduced in Flash Player 8. Though the Color class will continue to exist in Flash 9 using ActionScript 1 and 2, it will not be available when using ActionScript 3.

    Note, in ActionScript 3, the rgb property of ColorTransform has been renamed to color.
    Code:
    // creates a red square
    var square:Shape = new Shape();
    square.graphics.beginFill(0x000000);
    square.graphics.drawRect(0, 0, 100, 100);
    
    var colorTransform:ColorTransform = square.transform.colorTransform;
    colorTransform.color = 0xFF0000;
    square.transform.colorTransform = colorTransform;
    
    addChild(square);

Page 15 of 45 FirstFirst ... 5131415161725 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 11 users browsing this thread. (0 members and 11 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