Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 11-07-2007, 01:17 PM   #11
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

Sweet... Appreciate it... and keep it coming...
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline  
Old 11-07-2007, 01:31 PM   #12
matbury
What's up pussycat?
 
matbury's Avatar
 
Join Date: Dec 2006
Location: Toronto, Canada
Posts: 1,990
Default

How about some syntax for sharing variables between classes?

i.e. getter and setter methods, accessing public variables, etc.
__________________
http://matbury.com/
matbury is offline  
Old 11-07-2007, 02:01 PM   #13
FrodoBaggins
Fellowship of the Script
 
Join Date: Jan 2006
Location: the Netherlands / heading to the UK
Posts: 629
Default Accessing a loaded SWF

Thanks!
Here's one more for today.

Accessing a loaded SWF
It's not that hard actually. It's a matter of loading one swf in the other and assign it to a variable. Than use that variable to call the functions in the loaded swf. For this example we'll load b.swf in a.swf

code on frame 1 in b.swf:
ActionScript Code:
function sayHi() {    trace("b.swf says hi!"); }

code in a.swf:
ActionScript Code:
import flash.display.Loader; import flash.net.URLRequest; import flash.events.*;     var loader:Loader; var req:URLRequest; var loadedSWF:Object; loader = new Loader(); addChild(loader);             req = new URLRequest("b.swf"); loader.load(req); // check when we're done loading        loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler); function initHandler(e:Event):void {     loadedSWF= loader.content;     // call the sayHi function from the loaded b.swf     loadedSWF.sayHi(); }

NOTE: We use Event.INIT because that event is called when the first frame of the loaded file (ie: if its a SWF) is loaded, which means that all your functions are available. Event.COMPLETE is is called when the file is done loading ie: 100%. Slight difference, but can proof pretty fatal.

Thanks to bzouchir: http://actionscript.org/forums/showthread.php3?t=151392 (This thread also digs into the subject a bit deeper.)

The Loader class on liveDocs: http://livedocs.adobe.com/flash/9.0/...ay/Loader.html

Last edited by FrodoBaggins; 11-16-2007 at 11:24 AM. Reason: added url to livedocs
FrodoBaggins is offline  
Old 11-07-2007, 02:18 PM   #14
matbury
What's up pussycat?
 
matbury's Avatar
 
Join Date: Dec 2006
Location: Toronto, Canada
Posts: 1,990
Default

How about if you create an array in one class (i.e. CreateArray.as) and you want to access it in another class (i.e. UseArray.as)?
__________________
http://matbury.com/
matbury is offline  
Old 11-07-2007, 02:26 PM   #15
FrodoBaggins
Fellowship of the Script
 
Join Date: Jan 2006
Location: the Netherlands / heading to the UK
Posts: 629
Default

Come on mat, you know how that works, now show me!
FrodoBaggins is offline  
Old 11-07-2007, 06:52 PM   #16
Takster
Registered User
 
Join Date: Sep 2007
Location: Orange County
Posts: 22
Thumbs up Response to post #7

I just added some code to post number seven so people can copy and paste it into the first frame of a AS3 file and get it to work. No need to make a class file unless you want to, which would require extra work to get working . Also there is a typo in the removeEventListener. You are trying to remove a method that has not been defined, (onMouseMove). To see how well this works, publish the file at 1fps. After you run it comment out line 30: e.updateAfterEvent(); and then run it again. Great idea for a thread by the way. Kudos to FrodoBaggins.

Code:
import flash.display.MovieClip;
import flash.display.Graphics;
import flash.events.*;

var myMC:MovieClip = new MovieClip;

myMC.graphics.beginFill(0xff0000,1);
myMC.graphics.drawRect(0,0,100,100);
myMC.graphics.endFill();
addChild(myMC);

myMC.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler) 
myMC.addEventListener(MouseEvent.MOUSE_UP, mouseReleasedHandler);

// create Rectangle object
var bounds:Rectangle = new Rectangle(0,0, 450, 300);

function mouseDownHandler(e:MouseEvent):void {
    // changed for limited movement.
    myMC.startDrag(false, bounds);;
    stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}

function mouseReleasedHandler(e:MouseEvent):void {
    myMC.stopDrag();
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
}

function mouseMoveHandler(e:MouseEvent):void{
    e.updateAfterEvent();
}
Takster is offline  
Old 11-07-2007, 07:12 PM   #17
ryryguy
Scripts with Action!
 
ryryguy's Avatar
 
Join Date: Aug 2007
Location: Seattle, WA
Posts: 164
Arrow Types and typecasting

ActionScript 3.0 adds a number of features to support object-oriented ("OO") programming. Even if you are not creating lots of your own classes, much of ActionScript 3.0's functionality is built into a set of classes that have established inheritance relationships. To use these Flash classes effectively, it's essential to understand how types and typecasting work.

Look at the DisplayObject class, for an example. DisplayObject has the following direct subclasses:
  • AVM1Movie
    Bitmap
    InteractiveObject
    MorphShape
    StaticText
    Video

And that's only the subclasses that extend DisplayObject directly. If you look at subclasses of subclasses, you'll find that (among others) Loader, Sprite, and Stage are also indirect subclasses of DisplayObject ("great grandchildren" if you like: DisplayObject -> InteractiveObject -> DisplayObjectContainer -> Sprite). MovieClip is another indirect subclass of DisplayObject (through Sprite).

Point #1: Objects can be treated as if they are objects of any of its parent classes. So, for the following DisplayObjectContainer method:
ActionScript Code:
addChild(child:DisplayObject)
the argument "child" can be a Bitmap, or a Loader, or a StaticText, or any other subclass of DisplayObject. All of these things, in an important sense, are DisplayObjects.

Point #2: Objects cannot be treated as if they were objects of a particular subclass without typecasting. The situation described in point 1 doesn't work when you move in the other direction. While you can treat a MovieClip as a DisplayObject, you can't take a DisplayObject and use it as a MovieClip. For example, the following code would fail (would not compile):
ActionScript Code:
function playClip(clip:DisplayObject):void {     clip.gotoAndPlay(0)// doesn't work - "clip" is a DisplayObject, not a MovieClip! }
Note that in the "playClip" example, the object pointed to by the argument "clip" might actually be a MovieClip, as it would be if you called it like this:
ActionScript Code:
playClip(new MovieClip());
However, even if that is the case, the variable "clip" still has the type "DisplayObject", and that type does not have a "gotoAndPlay" function.

Point 3: Typecasting can convert a variable of a superclass type into a variable of a subclass type. Typecasting is a way of translating or recharacterizing a variable into a related type. There are a couple of ways to do this, but I prefer the "is" and "as" operators. Here's the "playClip" example modified to use the typecasting operators:

ActionScript Code:
function playClip(clip:DisplayObject):void {     if( clip is MovieClip )     {         var mc:MovieClip = clip as MovieClip;         mc.gotoAndPlay(0)// "mc" is type MovieClip, so we can call gotoAndPlay     }     else     {         trace("Error, " + clip + " is not a MovieClip.");     } }

The "as" operator will just return null if the thing you're trying to cast is not actually an object of the requested type, so you don't even really have to use the "is" operator in this example:

ActionScript Code:
function playClip(clip:DisplayObject):void {     var mc:MovieClip = clip as MovieClip;     if( mc != null )     {             mc.gotoAndPlay(0);      }     else     {         trace("Error, " + clip + " is not a MovieClip.");     } }

Finally, a common real-world scenario. Suppose that you have made a custom class, "MyDocument", that you are using as the document class for a Flash file:

ActionScript Code:
package pkg {     public class MyDocument extends MovieClip     {         public var filenames:Array;         public function reloadFile(var filename:String):void         {             //...         }     } }

That means that for objects put on the stage in the flash file, the "root" object will be a MyDocument. However, the "root" property is still of type DisplayObjectContainer, so it cannot be used to access MyDocument functions or properties without typecasting:

ActionScript Code:
// this code is on the timeline of a MovieClip symbol we are placing on the stage. import pkg.MyDocument; function updateStatus():void {     var theDocument:MyDocument = root as MyDocument;     if( theDocument == null )     {         trace("error, root isn't a MyDocument!");         return;     }         for each( var filename in theDocument.filenames )     {         if( isFileOutOfDate(filename) )         {             theDocument.reloadFile(filename);         }     } }

Live docs page: http://livedocs.adobe.com/flash/9.0/main/00000045.html
ryryguy is offline  
Old 11-08-2007, 05:50 AM   #18
gggmork
ignore this idiot
 
Join Date: Nov 2007
Posts: 10
Default

Quote:
Originally Posted by Takster View Post
I just added some code to post number seven...
Thanks, something simple like this will be useful for me to study; saved it. Also, something related to making it usable in a fla-only file... isn't there an easy way to dedent (de-tabify) selected text in as3? For example if I first make something the 'class' way with a seperate as file, but later want to make it usable in a fla-only file, I would probably want to delete the 'package' line and remove that indentation. I didn't see any way to remove indentation though.
gggmork is offline  
Old 11-08-2007, 07:18 AM   #19
FrodoBaggins
Fellowship of the Script
 
Join Date: Jan 2006
Location: the Netherlands / heading to the UK
Posts: 629
Default

Whoohoo, this thread got sticky!
First thing I'll do now is adding a small index in my first post to easily find subjects.

@matbury: I'll create an example later today on accessing class members.

@Takster: Thanks for pointing out those typo's, I couldn't test here, but I'll dl the cs3 trial and use that for a while.

@ryryguy: Thanks for your contribution!
FrodoBaggins is offline  
Old 11-08-2007, 11:40 AM   #20
FrodoBaggins
Fellowship of the Script
 
Join Date: Jan 2006
Location: the Netherlands / heading to the UK
Posts: 629
Default Getting and setting variables from/in a class

On special request, getter and setter methods.

Let's say we're building a race game and we want to be able to set the player's name and also retrieve it to, say, put it on the licence plate.

There are three ways of setting and retrieving a class's variables.
(This is not really as3 only, the same applies to as2.)

1. Make the variable public:
ActionScript Code:
// Player.as package awesomeRaceGame {         public class Player {                 // the player's name         public var pname:String;                 /*          * empty constructor          */         function Player(){                     }     } }

ActionScript Code:
// on timeline or other class import awesomeRaceGame.Player; var myPlayer:Player = new Player(); myPlayer.pname = "Frodo"; trace(myPlayer.pname);

2. Make the variable private, but use public functions to access the variable.
ActionScript Code:
// Player.as package awesomeRaceGame {         public class Player {                 // the player's name         private var pname:String;                 /*          * empty constructor          */         function Player(){                     }                 /*          * function that sets the racer's name          *          * @n: the name          */         public function setPlayerName(n:String):void {             pname = n;         }                 public function getPlayerName():String {             return pname;         }     } }

ActionScript Code:
// on timeline or other class import awesomeRaceGame.Player; var myPlayer:Player = new Player(); myPlayer.setPlayerName("Frodo"); trace(myPlayer.getPlayerName());

3. Using Flash's get and set methods (aka an in between 1 & 2).
ActionScript Code:
// Player.as package awesomeRaceGame {         public class Player {                 // the player's name         private var pname:String;                 /*          * empty constructor          */         function Player(){                     }                 /*          * function that sets the racer's name          *          * @n: the name          */         public function set pname(n:String):void {             pname = n;         }                 public function get pname():String {             return pname;         }     }     }

ActionScript Code:
// on timeline or other class import awesomeRaceGame.Player; var myPlayer:Player = new Player(); myPlayer.pname = "Frodo"; trace(myPlayer.pname);

Method 1 is considered bad OO practice, though some programmers argue that it is awkward to use method two. Using this method there is no control over what value can be assigned to your variable.

Method 2 is my personal favorite. The variable is private, but accessable through public methods. The methods in turn can prevent assignment of illegal values. Say we have only 15 characters available for our player name on the licence plate. We could modify the setPlayerName function to truncate the name to 15 characters.

Method 3 has the same benefits as 2, but also takes a step towards the ones in favour of method one. You can assign variables without explicitely calling a function.
FrodoBaggins is offline  
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
shared whiteboard with actionscript 3.0 flashyBird ActionScript 3.0 2 10-12-2007 05:13 AM
Animations in ActionScript 3.0 teenup ActionScript 3.0 6 09-17-2007 12:11 PM
PHP, Actionscript 3.0 and Å Ä Ö characters latrixie ActionScript 3.0 7 08-29-2007 08:41 AM
Actionscript 3.0 Programmer Insanemitch Projects and Positions 0 08-28-2007 08:21 PM
Preloader for actionscript 3.0 rysotope ActionScript 3.0 3 07-10-2007 04:58 PM


All times are GMT. The time now is 06:36 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.