Go Back   kirupaForum > Flash > ActionScript 3.0

Reply
 
Thread Tools Display Modes
Old 09-07-2006, 04:45 AM   #211
v_gyku
Registered User
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...
v_gyku is offline   Reply With Quote

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

Old 09-07-2006, 06:08 AM   #212
ven
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.
ven is offline   Reply With Quote
Old 09-07-2006, 08:59 AM   #213
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
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.

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

Posts 17,266
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
ActionScript 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.

__________________
senocular is offline   Reply With Quote
Old 09-07-2006, 12:58 PM   #215
v_gyku
Registered User
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; 09-07-2006 at 01:00 PM..
v_gyku is offline   Reply With Quote
Old 09-07-2006, 01:22 PM   #216
ignitrix
AS3 denizen
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
ignitrix is offline   Reply With Quote
Old 09-07-2006, 08:11 PM   #217
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
I will try to make the next post a comprehensive stage access post.

__________________
senocular is offline   Reply With Quote
Old 09-08-2006, 04:05 AM   #218
v_gyku
Registered User
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...
v_gyku is offline   Reply With Quote
Old 09-08-2006, 07:52 PM   #219
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
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:
ActionScript 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.
ActionScript 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.
ActionScript 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
ActionScript Code:
package {
       
    public class MyDocumentClass extends TopLevel {
           
        public function MyDocumentClass() {
            // code
        }
    }
}

Example non-DisplayObject class used in MyDocumentClass application accessing stage:
ActionScript 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.

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

Posts 17,266
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:
ActionScript 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.

__________________
senocular is offline   Reply With Quote
Old 09-11-2006, 08:53 PM   #221
Krilnon
≈ ≠ =
 
Krilnon's Avatar
Location Rochester, NY

Posts 7,183
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.
Krilnon is offline   Reply With Quote
Old 09-11-2006, 08:59 PM   #222
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
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.

__________________
senocular is offline   Reply With Quote
Old 09-11-2006, 09:11 PM   #223
Krilnon
≈ ≠ =
 
Krilnon's Avatar
Location Rochester, NY

Posts 7,183
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; 09-11-2006 at 09:16 PM..
Krilnon is offline   Reply With Quote
Old 09-12-2006, 07:28 AM   #224
ven
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; 09-13-2006 at 04:42 AM..
ven is offline   Reply With Quote
Old 09-12-2006, 03:34 PM   #225
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
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.
ActionScript 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);

__________________
senocular is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 25 (0 members and 25 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 11:20 AM.

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