04-21-2007, 12:44 PM
|
#331
|
|
If you can read this, I'm
|

 |
San Francisco, CA (USA) |
|
 |
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.
__________________

|
|
|
04-21-2007, 12:45 PM
|
#333
|
|
If you can read this, I'm
|

 |
San Francisco, CA (USA) |
|
 |
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/>
__________________

|
|
|
04-21-2007, 12:45 PM
|
#334
|
|
If you can read this, I'm
|

 |
San Francisco, CA (USA) |
|
 |
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().
__________________

|
|
|
04-21-2007, 12:45 PM
|
#335
|
|
If you can read this, I'm
|

 |
San Francisco, CA (USA) |
|
 |
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;
__________________

|
|
|
04-21-2007, 03:08 PM
|
#339
|
|
|
Quote:
Originally Posted by senocular
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"
|
|
|
04-21-2007, 03:24 PM
|
#340
|
|
|
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
|
|
|
04-21-2007, 04:13 PM
|
#341
|
|
|
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.

|
|
|
04-21-2007, 04:36 PM
|
#342
|
|
|
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 AS 2, 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));
|
|
|
04-21-2007, 05:56 PM
|
#343
|
|
|
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? 
|
|
|
04-21-2007, 07:56 PM
|
#344
|
|
|
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
|
|
|
04-21-2007, 09:01 PM
|
#345
|
|
|
Quote:
Originally Posted by Krilnon
|
Ah, then I understand.  However, I don't make own classes.. (don't understand that either  )
|
|
|
|
Currently Active Users Viewing This Thread: 28 (0 members and 28 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -4. The time now is 03:10 PM.
|
|