Go Back   kirupaForum > Flash > ActionScript 3.0

Reply
 
Thread Tools Display Modes
Old 04-21-2007, 12:44 PM   #331
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
Getting Around globally accessible _root and _global

The _root and _global objects in ActionScript 2 were globally accessible in code (meaning they could be accessed anywhere) making them easy places to store variables and functions to be used anywhere else. In ActionScript 3, there is no longer a _global object, and _root, now root, is accessible only to DisplayObject instances and only to those currently on the screen in an active display list. This makes them very difficult to use to store definitions that you want accessible everywhere. Instead, now with AS3, what you would need to do is use classes.

More specifically, static variables in classes. Most classes are used to create instances of themselves - they are a template by which objects in code are constructed. In working with those instances each instance has their own set of variables and run methods that only affect them. Static variables (and methods) are specific to the class object itself. They are not associated with instances and are instead referenced from the class directly. Since classes themselves are global, by having a static object defined in a class, you can create a global variable container that is accessible anywhere within your movie (or any class used within it).

Consider the following glo class. It has a static variable called bal that is a generic object. As a generic (and dynamic) object, you can define anything within it at runtime without having to delcare it beforehand. Also, being static, you would access it from the glo class directly, not from an instance: glo.bal; This provides a globally accessible object anywhere in code (and since the class is not defined in a package, no importing is necessary).

ActionScript Code:
package {
    public class glo {
        public static var bal:Object = new Object();
    }
}


Any where in your movie you can access glo.bal just as you would otherwise with _root or _global.
ActionScript Code:
trace(glo.bal.foo); // undefined
glo.bal.foo = "bar";
trace(glo.bal.foo); // bar
 


Note: you can use any naming for the glo class, this was just an example.

__________________
senocular is offline   Reply With Quote

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

Old 04-21-2007, 12:45 PM   #332
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
ActionScript 2 to ActionScript 3 Converter

Patrick Mineault has created an AS2 to AS3 converter in PHP. Though not 100% in its conversion (would such a conversion be possible?), it does a very good job and is a great tool for helping you make that leap in converting code. An online version is available here:
http://www.5etdemi.com/convert

You can download it (with Win binary) here:
http://www.5etdemi.com/convert/convert.zip

__________________
senocular is offline   Reply With Quote
Old 04-21-2007, 12:45 PM   #333
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
XML: Removing Nodes

For XML in ActionScript 2 (now XMLDocument in ActionScript 3), to remove an XML node, you would use an XMLNode method called removeNode().
ActionScript Code:
// ActionScript 2
var myXML:XML = new XML("<parent><child /></parent>");
myXML.firstChild.firstChild.removeNode();
trace(myXML); // <parent />
 

This is no longer a case with AS3 E4X. Now, to remove an XML node, you use the delete keyword just like you would for a dynamic property in a generic ActionScript object.
ActionScript Code:
// ActionScript 3
var myXML:XML =
    <parent>
        <child />
    </parent>;
delete myXML.child;
trace(myXML.toXMLString()); // <parent/>
 

__________________
senocular is offline   Reply With Quote
Old 04-21-2007, 12:45 PM   #334
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
getObjectsUnderPoint

ActionScript 3 now has a new method for DisplayObjectContainer instances called getObjectsUnderPoint(). This method provides a kind of point-based hitTest for all objects within the DisplayObjectContainer returning an array of all objects under the point provided. This would be equivalent to looping through all children (and chilren's children, etc) and checking for a point hitTest on those objects.
ActionScript Code:
var location:Point = new Point(stage.mouseX, stage.mouseY);
var objectsBelowMouse:Array = stage.getObjectsUnderPoint(location);

All points used in getObjectsUnderPoint are assumed to be in the global coordinate space of the stage, not the target DisplayObjectContainer instance. To use a point within the coordinate space of that object, use localToGlobal before passing the point into getObjectsUnderPoint.
ActionScript Code:
var location:Point = new Point(target.mouseX, target.mouseY);
location = target.localToGlobal(location);
var objectsBelowMouseInTarget:Array = target.getObjectsUnderPoint(location);

Note that not all objects under the point will be returned if security restrictions prevent it. To see if this is the case, you can use areInaccessibleObjectsUnderPoint().

__________________
senocular is offline   Reply With Quote
Old 04-21-2007, 12:45 PM   #335
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
Accessing FlashVars and HTML Parameters

You can pass variables to SWF files in the object/embed code used to display a SWF in HTML. You can do this two ways, one using URL variables (query string) at the end of the SWF path, or through the FlashVars property.
HTML Code:
<!-- URL Variables -->
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="640" height="500" align="middle">
	<param name="allowScriptAccess" value="sameDomain" />
	<param name="movie" value="flashMovie.swf?myVar=1" />
	<param name="quality" value="high" />
	<param name="bgcolor" value="#EFF7F6" />
	<embed src="flashMovie.swf?myVar=1" quality="high" bgcolor="#EFF7F6" width="640" height="500" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
HTML Code:
<!-- FlashVars -->
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="550" height="400" align="middle">
	<param name="allowScriptAccess" value="sameDomain" />
	<param name="movie" value="flashMovie.swf" />
	<param name="quality" value="high" />
	<param name="bgcolor" value="#FFFFFF" />
	<param name="FlashVars" value="myVar=1" />	
	<embed src="flashMovie.swf" FlashVars="myVar=1" quality="high" bgcolor="#FFFFFF" width="550" height="400" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />
</object>
In ActionScript 2, these variables were simply defined as variables in _root. This has changed for ActionScript 3. Now these variables are accessible in a parameters object located in the root loaderInfo object.

Given the HTML embed code above, you would access the myVar property using:
ActionScript Code:
root.loaderInfo.parameters.myVar;

__________________
senocular is offline   Reply With Quote
Old 04-21-2007, 12:56 PM   #336
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
ActionScript Speed Through Typing

One of the reasons ActionScript 3 is so fast is that it now supports runtime type annotations (typing) that allows values to be stored as native machine types allowing for best performance and usage of memory.

When possible, always appropriately type your variables

Similarly, sealed (non-dynamic) classes provide a fixed set of properties and improves memory usage by not requiring an internal hash table for each object instance resulting in improved performance.

When possible, avoid dynamic classes

By using sealed classes with appropriately typed variables, you will be able to maximize the performance of your flash application.

__________________
senocular is offline   Reply With Quote
Old 04-21-2007, 01:10 PM   #337
Pattt
Hm?
 
Pattt's Avatar
Omg! AS3 is huge. And the CS3 AS is so different.
Where can I get started with the CS3 AS3?

__________________
[ veclock.deviantart.com ]
Pattt is offline   Reply With Quote
Old 04-21-2007, 01:23 PM   #338
senocular
If you can read this, I'm
 
senocular's Avatar
Location San Francisco, CA (USA)

Posts 17,266
Quote:
Originally Posted by Pattt View Post
Omg! AS3 is huge. And the CS3 AS is so different.
Where can I get started with the CS3 AS3?
well... this thread has a lot of help
Otherwise there's flash help and eventually books (some might be out now).
This post has links to the livedocs.

__________________
senocular is offline   Reply With Quote
Old 04-21-2007, 03:08 PM   #339
Pattt
Hm?
 
Pattt's Avatar
Quote:
Originally Posted by senocular View Post
well... this thread has a lot of help
Otherwise there's flash help and eventually books (some might be out now).
This post has links to the livedocs.
This thread just talks about those package{}-stuff that I don't understand. :O Can't be used in fla-files I think.

I wanna learn about the new structure of AS3. Why do I get errors when I'm trying to use random(), for example?

"Calls to a possibly undefined method random"

__________________
[ veclock.deviantart.com ]
Pattt is offline   Reply With Quote
Old 04-21-2007, 03:24 PM   #340
Krilnon
≈ ≠ =
 
Krilnon's Avatar
Location Rochester, NY

Posts 7,183
Quote:
This thread just talks about those package{}-stuff that I don't understand. :O Can't be used in fla-files I think.
Classes… You can (have to) use classes in .fla files, but the classes that you define should be stored in .as files. (AS2 equivalent explained here)

This thread still covers most (if not all) of the syntax changes that you would need to code in the little editor window in Flash. Instead of declaring methods, you can declare functions, (so, there aren't any access modifiers like public or private) but if you ignore (or learn) the class/package syntax then all of the posts become useful again.

Quote:
I wanna learn about the new structure of AS3. Why do I get errors when I'm trying to use random(), for example?
random has been deprecated, you should probably use Math.random. See: http://livedocs.adobe.com/flex/201/l...migration.html
Krilnon is offline   Reply With Quote
Old 04-21-2007, 04:13 PM   #341
Pattt
Hm?
 
Pattt's Avatar
I prefere to use .fla files, not .as.

I am totaly newbie on AS3, I'm used to AS2 only.

Then I get Incorrect number of arguments. Expected no more than 0.


__________________
[ veclock.deviantart.com ]
Pattt is offline   Reply With Quote
Old 04-21-2007, 04:36 PM   #342
Krilnon
≈ ≠ =
 
Krilnon's Avatar
Location Rochester, NY

Posts 7,183
Quote:
I prefere to use .fla files, not .as.
How much experience do you have using .as files? If you aren't familiar with the package structure, then you probably haven't used .as files as they were primarily intended to be used in AS2.

Quote:
I am totaly newbie on AS3, I'm used to AS2 only.
You're probably actually used to AS1 syntax. The big difference between AS1 and AS2 was class syntax. Objects, datatypes, methods, etc. all existed in AS1. The type operator introduced with AS2 is only used for compile-time type checking, though it is one of the most commonly referenced AS2 'features'. Plenty of the code that I've seen claim to be AS2-only could be rewritten using AS1 syntax by removing all of the type declarations. (and perhaps adding some qualified names)

If you were really familiar with AS2, you would almost be guaranteed to be familiar with the class/package structure that's confusing you now.

Quote:
Then I get Incorrect number of arguments. Expected no more than 0.
It doesn't accept any arguments, it generates a floating-point number from the set [0,1). To make an equivalent of the old top-level random function, you can use:
ActionScript Code:
Math.floor(Math.random() * n));
Krilnon is offline   Reply With Quote
Old 04-21-2007, 05:56 PM   #343
Pattt
Hm?
 
Pattt's Avatar
Quote:
How much experience do you have using .as files? If you aren't familiar with the package structure, then you probably haven't used .as files as they were primarily intended to be used in AS2.
Of course, as-files isn't a problem as long it is AS2, then it would be as simple as writing the code into a frame.

I haven't used the packed-thing. I tought it only was for AS3.

Quote:
If you were really familiar with AS2, you would almost be guaranteed to be familiar with the class/package structure that's confusing you now.
I am. I can use classes quite alot. But not package. Don't even know what it does.

Quote:
It doesn't accept any arguments, it generates a floating-point number from the set [0,1). To make an equivalent of the old top-level random function, you can use
Ah, there's the problem!

Yet I had problems with onEnterFrame, now I have to "register" it?

__________________
[ veclock.deviantart.com ]
Pattt is offline   Reply With Quote
Old 04-21-2007, 07:56 PM   #344
Krilnon
≈ ≠ =
 
Krilnon's Avatar
Location Rochester, NY

Posts 7,183
Quote:
I am. I can use classes quite alot. But not package. Don't even know what it does.
It's just a way of organizing your classes into logical groups. For example, all of the filter classes are in the flash.filters package. This thread has an explanation of the new package syntax: http://www.kirupa.com/forum/showthre...56#post1890456
Krilnon is offline   Reply With Quote
Old 04-21-2007, 09:01 PM   #345
Pattt
Hm?
 
Pattt's Avatar
Quote:
Originally Posted by Krilnon View Post
It's just a way of organizing your classes into logical groups. For example, all of the filter classes are in the flash.filters package. This thread has an explanation of the new package syntax: http://www.kirupa.com/forum/showthre...56#post1890456
Ah, then I understand. However, I don't make own classes.. (don't understand that either )

__________________
[ veclock.deviantart.com ]
Pattt is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 28 (0 members and 28 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:10 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