07-04-2006, 12:29 PM
|
#63
|

 |
San Francisco, CA (USA) |
|
 |
17,426 |
|
|
Access Attributes
ActionScript 3 introduces 2 new access attributes for classes and members and refines the private attribute of ActionScript 2. In ActionScript 3 you have the following attributes for controlling access: - public
- protected
- private
- internal (default)
public: The public attribute is the same as it was in ActionScript 2. Anything defined as public can be accessed anywhere by anything.
Constructors are always public.
Application classes need to be public.
protected: The protected attribute is a new attribute for ActionScript. It sets variables hidden from all access except from subclasses accessing the variables (inherited) from their own instance. Attempting to access a protected variable from another class instance in any circumstance will result in an error.
Classes and constructors cannot be defined as protected.
private: AS2 had the private attribute but it worked more like protected as subclasses had full access to private members. Now, with AS3, private is completely private and is only accessible from within the class in which it is defined. To all other classes, even subclasses, a private member doesn't even exist. This means that subclasses can even define new members of the same name and not have a conflict since the private member of the superclass is completely hidden. Private members are accessible directly off of class instances only when done so within the class definition.
Classes and constructors cannot be defined as private.
internal: Internal access is similar to public access but is restricted to package definitions. Classes in the same package have access to all internal members of any other class in that package. Classes in other packages do not have access.
Internal is the default for every class and class member except constructors which are always public.
Access control in helper classes are a little different. Since helper classes don't technically belong in packages, internal access for them is restricted to the classes in the current file. Helper classes themselves are inherently internal in this respect (and do not need to be explicitly provided any form of access attribute). Other access attributes within helper classes behave as expected. Note: your primary class cannot extend a helper class. Only helper classes can extend other helper classes and they need to be in the same file.
Something to keep in mind that access control for AS3 is also enforced not only during compile time, but also runtime. Hacks used in AS2 to get by accessing hidden methods will no longer work in AS3.
Example 1
Code:
package {
import flash.display.Sprite;
// Application class needs to be public (internal by default)
public class AccessControl extends Sprite {
// constructors are always public
function AccessControl() {
// only classes in this file
// can access helper classes
var helper:Helper = new Helper();
trace(helper.pubNum); // OK
// trace(helper.protNum); // Error - cannot access protected
// trace(helper.privNum); // Error - cannot access private
trace(helper.interNum); // OK
}
}
}
// Helper class is implicitly internal
class Helper {
// public access granted anywhere
// variables are usually protected or
// private with get/set used for public access
public var pubNum:Number = 1;
// protected access granted only for
// subclasses in that class
protected var protNum:Number = 2;
// private access granted only in this class
private var privNum:Number = 3;
// internal access granted only in the same
// package, but for helper classes, it means
// only in the same file
internal var interNum:Number = 4;
// constructors are always public
function Helper() {
}
}
// SubHelper class is implicitly internal
// can extend other helper classes
class SubHelper extends Helper {
// constructors are always public
function SubHelper() {
trace(pubNum); // OK
trace(protNum); // OK - inherited
// trace(privNum); // Error - cannot access private
trace(interNum); // OK
}
}
Example 2
Code:
package {
import flash.display.Sprite;
import containers.*;
// Application class needs to be public (internal by default)
public class AccessControl extends Sprite {
// constructors are always public
function AccessControl() {
// can access classes in other packages
// only if public
var bowl:Bowl = new Bowl(); // OK
// var basket:Basket = new Basket(); // Error - cannot access internal
trace(bowl.pubNum); // OK
// trace(bowl.protNum); // Error - cannot access protected
// trace(bowl.privNum); // Error - cannot access private
// trace(bowl.interNum); // Error - cannot access internal
}
}
}
Code:
package containers {
// public class accessible anywhere
public class Bowl {
// public access granted anywhere
public var pubNum:Number = 1;
// protected access granted only for
// subclasses in that class
protected var protNum:Number = 2;
// private access granted only in this class
private var privNum:Number = 3;
// internal access granted only in the same package
internal var interNum:Number = 4;
// constructors are always public
function Bowl() {
// can access inteneral classes if in same package
var basket:Basket = new Basket();
trace(basket.pubNum); // OK
// trace(basket.protNum); // Error - cannot access protected
// trace(basket.privNum); // Error - cannot access private
trace(basket.interNum); // OK - same package
// clone using public method
var basketCopy:Basket = basket.clone();
}
}
}
Code:
package containers {
// interal only accessible
// from other classes in package
internal class Basket {
// public access granted anywhere
public var pubNum:Number = 1;
// protected access granted only for
// subclasses in that class
protected var protNum:Number = 2;
// private access granted only in this class
private var privNum:Number = 3;
// internal access granted only in the same package
internal var interNum:Number = 4;
// constructors are always public
function Basket() {
}
// accessible anywhere as long as
// referencing a Basket instance
public function clone():Basket {
var basket:Basket = new Basket();
basket.pubNum = pubNum; // OK
basket.protNum = protNum; // OK - same class
basket.privNum = privNum; // OK - same class
basket.interNum = interNum; // OK
return basket;
}
}
}
__________________

|
|
|
07-06-2006, 03:35 AM
|
#64
|
|
|
Quote:
|
Originally Posted by senocular
You should be able to use the classes from the Flex SDK. Just download them somewhere on your computer and specify the location of the folder with the mx directory in your Flash preferences class path.
|
Just gave that a go... I had to comment out the includes and delete all references to the mx_internal namespace (Senocular probably knows a more elegant method of "fixing" these things, I'm more a slash and burn kinda guy) and came up with this:
http://www.onebyonedesign.com/flash/f9/tween/ (click on the circle)
the script:
[AS]package com.onebyonedesign.tests {
import mx.effects.*;
import flash.display.*;
import flash.events.*;
import mx.effects.easing.*;
public class TweenTest extends Sprite {
private var _circle:Sprite;
function TweenTest() {
stage.frameRate = 31;
init();
}
private function init():void {
_circle = makeCircle();
_circle.addEventListener(MouseEvent.MOUSE_DOWN, tweenMe);
}
private function makeCircle():Sprite {
var s:Sprite = new Sprite();
s.graphics.beginFill(0x660000);
s.graphics.drawCircle(20, 20, 20);
addChild(s);
return s;
}
private function updateTween(vals:Array):void {
_circle.x = vals[0];
_circle.y = vals[1];
}
private function endTween(vals:Array):void {
trace ("ending coordinates: " + vals);
}
private function tweenMe(e:Event):void {
var myTween:Tween = new Tween(_circle, [_circle.x, _circle.y], [275, 200], 1000, 31);
myTween.easingFunction = Elastic.easeOut;
myTween.setTweenHandlers(updateTween, endTween);
}
}
}[/AS]
the cool thing is that we can now add arrays of properties instead of doing one at a time.. Disco...
EDIT:
Thank you for these tips, Senocular.. Great thread and some great info...
Last edited by devonair; 07-06-2006 at 04:01 AM..
|
|
|
07-06-2006, 09:29 AM
|
#67
|

 |
San Francisco, CA (USA) |
|
 |
17,426 |
|
|
Quote:
|
Originally Posted by Increu
I dontknow whats hapenning, i am trying this code
import flash.utils.Dictionary;
var a:Object = new Object();
var b:Object = new Object();
var dict: Dictionary = new Dictionary();
dict[a] = 1; // dict[a] = 1;
dict[b] = 2; // dict[b] = 2;
for (var prop:String in dict) {
trace(prop); // traces: [object Object], [object Object]
trace(dict[prop]); // traces: 1, 2
}
and its not tracing 1,2, just undefined undefined
why why why???????
|
woops, that would be my fault - typo (actually force of habit) - but good that you pointed that out!
Why you are getting undefined in that trace is because I used String as the data type for the prop variable. Because of this, when iterating through the dict object, the values of prop in that loop are converted to their String equivalent instead of being the object themselves - exactly what you would use the dict object to avoid.
To fix this, you just have to make sure the prop variable isnt cast as a String. Because you actually don't know what prop could be, it should be cast as *. So the loop should look like:
Code:
for (var prop:* in dict) {
trace(prop); // traces: [object Object], [object Object]
trace(dict[prop]); // traces: 1, 2
}
So now, prop remains whatever type it actually exists within the dict object (Object in this case) and the second trace resolves correctly as dict[a] and dict[b] instead of dict["[object Object]"]

__________________

|
|
|
07-06-2006, 09:46 AM
|
#69
|

 |
San Francisco, CA (USA) |
|
 |
17,426 |
|
|
Quote:
|
Originally Posted by gpacioli
I continue to hear and read about Flex --- someone in a Davie, Florida user group said, "Flex is Adobe's Number one priority." Is this the place to ask,
"What is Flex --- and what good does it do? Does it compete with Flash? Do I start to learn Flex when I'm just learning Flash. Or what the ... ?
|
Well, I wouldn't be so bold as to say Flex is Adobe's Number one priority. But, the short answer is, Flex is a programmer-centric develop environment and server for the creation and deployment of Flash-based rich internet applications (RIAs). It has a strong focus on UI components and web services for the exchange and presentation of data.
Does it compete with Flash? No, not really. Though Flex Builder is an IDE for creating Flash applications, it doesn't have any of the drawing tools Flash has for content creation, nor does it have any timeline necessary for animations. Flex Builder is more of a workshop to bring existing content together to quickly and easily create moderately sized or large, screen or form based RIAs. You can kind of think of Flex as a programmers Flash where Flash is more for designers and animators (though, admittedly, Flash suffers a bit in terms of animation).
Flex presentation server is a different beast altogether and is more about server-side deployment (even further from Flash then Builder).
Also see:
http://www.gskinner.com/blog/archive..._from_a_f.html
Skinner's perspective on Flex (with a nice little analogy for Flex and Flash)
__________________

|
|
|
07-06-2006, 01:42 PM
|
#70
|

 |
San Francisco, CA (USA) |
|
 |
17,426 |
|
|
Abstract Classes
Unfortunately ActionScript 3 does not support abstract classes (classes that cannot be instantiated only extended). So you cannot create your own abstract classes in Flash. However, be aware that some of the internal classes in ActionScript are internally abstract. These classes include: As abstract classes, you cannot create instances of them directly using the new keyword.
Code:
var myObj:InteractiveObject = new InteractiveObject(); // ERROR
However, in addition to that, in ActionScript, it also means that you cannot directly subclass these classes and create instances of those subclasses
Code:
package {
import flash.display.DisplayObject;
public class MyDisplay extends DisplayObject{
public function MyDisplay (){
// ERROR
}
}
}
This has to do with the internal nature and how they are defined for the Player. If you attempt to subclass one of these classes and instantiate that subclass, you will get the same Argument Error that you would get if you tried to instantiate one of those classes directly.
Instead, what you would need to do is extend an internal class that already subclasses these classes. For example, if you wanted to extend DisplayObject, you could instead extend Shape, a light, internal class that is a subclass of DisplayObject.
__________________

|
|
|
07-06-2006, 11:22 PM
|
#71
|

 |
councious of a plane's spirit |
|
 |
30 |
|
|
Quote:
|
Originally Posted by senocular
Why you are getting undefined in that trace is because I used String as the data type for the prop variable. Because of this, when iterating through the dict object, the values of prop in that loop are converted to their String equivalent instead of being the object themselves - exactly what you would use the dict object to avoid.
To fix this, you just have to make sure the prop variable isnt cast as a String. Because you actually don't know what prop could be, it should be cast as *. So the loop should look like:
|
Ok..now i see it!!Thanks Sen!!
Just one more thing...i dont know if i understand it rigth, it is correct to say that the dictionary is for add properties to things that already exists
or
the dictionary is in fact, creating a new object inside the dictionary instance and the name of this object is a string and this string is the same name of any object i want...
Now i am confused...
|
|
|
07-07-2006, 12:01 PM
|
#73
|

 |
San Francisco, CA (USA) |
|
 |
17,426 |
|
|
The override Keyword
Overriding a method of a class means redefining a method for a class which would otherwise be inherited. The new method is then used in place of the inherited one (though the inherited method can still be invoked using super).
For ActionScript 3, when you override a method or property of a superclass, you need to use the override attribute with your new method. This specifies that the member you are creating is overriding that which would otherwise be inherited. If you do not use override with a method that already exists in a superclass, an error is thrown at compile time.
Ex:
Code:
package {
import flash.display.*;
class MySprite extends Sprite {
private var children:Array = new Array();
public function MySprite() {
}
public override function addChild(child:DisplayObject):DisplayObject {
children.push(child);
super.addChild(child);
return child;
}
}
}
Since addChild exists in the Sprite superclass, the override attribute is needed to successfully define the new addChild method which also adds the child passed to a children array.
Note that the method signature needs to match that of the overriden method
Override works with both normal class methods as well as getter/setter methods (properties), but it will not work with any of the following: - Variables
- Constants
- Static methods
- Methods that are not inherited
- Methods that implement an interface method
- Inherited methods that are marked as final in the superclass
Also be aware that override is not needed for methods inherited directly from the Object class. These include: - hasOwnProperty
- isPrototypeOf
- propertyIsEnumerable
- setPropertyIsEnumerable
- toString
- valueOf
These methods are added dynamically and are not part of the actual class definition. The override keyword is to be used only with methods which are part of a class's original definition.
However, if extending a class which uses a method above as part of its defnition, the override keyword is required. For example, if you are extending Object, you do not need to use the override keyword for the toString method. But, if you extend the Sprite class, you will need to override toString since the Sprite class has its own unique toString which is part of its class definition.
__________________

|
|
|
07-08-2006, 10:01 PM
|
#74
|

 |
San Francisco, CA (USA) |
|
 |
17,426 |
|
|
Using prototype
The prototype object in ActionScript is an object that exists among classes whose values are shared among all instances of the class to which it belongs. In ActionScript 1 and 2, it was used to control class inheritance. When a subclass instance references a variable, it first checks for that variable in the instance, followed by the class's prototype, followed by the superclass's prototype and so on through the prototype (inheritance) chain until there are no more classes.
In ActionScript 3, inheritance is primarily managed through Class inheritance and does not depend on the prototype object. However, the prototype object still exists and still provides much of the same functionality it did in AS1/AS2.
Each class and (non-method) function created in AS3 has a prototype object associated with it. For classes, prototype is read-only, meaning you cannot redefine it with a new value. However, it doesn't mean you cannot define new values within it (otherwise it would be pointless  ). Function prototypes are not read only. This allows you to create dynamic classes using the old style of class definitions through functions and set up inheritance through redefining the prototype.
Example:
Code:
package {
import flash.display.Sprite;
public dynamic class MyClass extends Sprite {
public function MyClass(){
// prototype = new Object(); // ERROR, cannot change prototype of class
prototype.newValue = 1; // OK, adding (or removing) prototyped values
trace(this.newValue); // 1
trace(prototype.toString); // function Function() {}
trace(prototype.addChild); // undefined
trace(addChild); // function Function() {}
// dynamic ("old style") class definition
var TempClass:Function = function():void {
trace("Create TempClass");
}
TempClass.prototype = prototype; // OK, can set up inheritance
var tempObject:* = new TempClass(); // "Create TempClass"
trace(tempObject.newValue); // 1
}
}
}
Note that you should always prefix dynamic variable references with the this keyword. Also note that the Object class methods are dynamic and are defined in the prototype (which is why they are not overridden with the override keyword). You may also notice that the tempObject is typed as * instead of TempClass. This is because TempClass is only recognized as being a Function in AS3, not an actual class though it can still be used as one. Though a Class type exists, dynamic classes created like TempClass will always be recognized as Functions so creating instances with them always generates instances typed as a generic object.
__________________

|
|
|
07-11-2006, 06:04 PM
|
#75
|

 |
San Francisco, CA (USA) |
|
 |
17,426 |
|
|
Regular Expression (RegExp) Support
ActionScript 3 now supports regular expressions! The implementation is much like the one used in JavaScript. You can create new regexp patterns with the RegExp class constructor (top level) and a string or using the literal text format (defined like a string only replacing quotes with a forward slash (/). Ex:
Code:
var reCon:RegExp = new RegExp("\\w+", "i");
var reLit:RegExp = /\w+/i;
RegExp methods include: - RegExp.exec()
- RegExp.test()
String methods that work with regular expressions include: - String.match()
- String.replace()
- String.search()
__________________

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