Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 12-03-2007, 06:08 AM   #1
lg101
Registered User
 
Join Date: Nov 2007
Posts: 16
Default _root replacement in AS3

Hi all,

here is my question
i find in the AS2 code that we everyone uses _root to use the functions and properties of the parent

like in one of the codes i have seen "_root.testOver()" to call a parent method testOver().

Now if i need the same behavior in AS3 what can i use.

Thanks in Advance
lg101 is offline   Reply With Quote
Old 12-03-2007, 09:31 AM   #2
matbury
What's up pussycat?
 
matbury's Avatar
 
Join Date: Dec 2006
Location: Toronto, Canada
Posts: 1,990
Default

It's been changed to 'root'. Using root can get you into trouble if you addChild() and removeChild(), also known as reparenting. It works differently to AS 2.0 because the objects can exist, with all their properties, in 'limbo' while they're not on the stage. This means that DisplayObjects' parents and relationship to the root can change.

In other words, decide whether your DisplayObjects are going to be static or reparented before you decide to use root.

There's also an alternative trick. You can set the SWF as a variable so that if you load your SWF into another SWF your root doesn't change.

ActionScript Code:
var myRoot:MovieClip = this;
__________________
http://matbury.com/
matbury is offline   Reply With Quote
Old 12-03-2007, 10:07 AM   #3
atishay811
Registered User
 
Join Date: Jun 2007
Posts: 2
Default

Quote:
Originally Posted by lg101 View Post
Hi all,

here is my question
i find in the AS2 code that we everyone uses _root to use the functions and properties of the parent

like in one of the codes i have seen "_root.testOver()" to call a parent method testOver().

Now if i need the same behavior in AS3 what can i use.

Thanks in Advance
One option could be the use of a global variable via another class in a .as file. keep it as an array of objects, and add your root clip to that. now you can access it via the class you created outside, from anywhere. its global. Read topics about making global variables in as3 for more details...
atishay811 is offline   Reply With Quote
Old 12-03-2007, 10:58 AM   #4
lg101
Registered User
 
Join Date: Nov 2007
Posts: 16
Default

Hey guys just wanted to add to it, here i am accessing props of a AS2 code
so i want my AS3 code to access props of AS2 code without _root
lg101 is offline   Reply With Quote
Old 12-03-2007, 05:59 PM   #5
dr_zeus
Feathers
 
dr_zeus's Avatar
 
Join Date: Mar 2005
Location: Silicon Valley
Posts: 1,999
Default

AS3 SWFs cannot directly speak with AS2 SWFs using regular ActionScript. You must use the LocalConnection class if you need communicate between them.
__________________
Josh Tynjala | Feathers
dr_zeus is offline   Reply With Quote
Old 12-03-2007, 08:37 PM   #6
matbury
What's up pussycat?
 
matbury's Avatar
 
Join Date: Dec 2006
Location: Toronto, Canada
Posts: 1,990
Default

Quote:
Originally Posted by matbury View Post
It's been changed to 'root'.
Sorry, I was misinformed on this one! What you're looking for is the stage property, or at least that's the nearest ting to it in AS 3.0.

This is from the Flex docs:

Quote:
stage property

stage:Stage [read-only]
The Stage of the display object. A Flash application has only one Stage object. For example, you can create and load multiple display objects into the display list, and the stage property of each display object refers to the same Stage object (even if the display object belongs to a loaded SWF file).

If a display object is not added to the display list, its stage property is set to null.

Implementation
public function get stage():Stage

Example
The following code creates two TextField objects and uses the width property of the Stage object to position the text fields:

ActionScript Code:
import flash.text.TextField; var tf1:TextField = new TextField(); tf1.text = "Text Field 1"; tf1.border = true; tf1.x = 10; addChild(tf1); tf1.width = tf1.stage.stageWidth / 2 - 10; var tf2:TextField = new TextField(); tf2.text = "Text Field 2"; tf2.border = true; tf2.x = tf1.x + tf1.width + 5; addChild(tf2); tf2.width = tf2.stage.stageWidth / 2 - 10; trace(stage.stageWidth);
__________________
http://matbury.com/
matbury is offline   Reply With Quote
Old 12-03-2007, 08:47 PM   #7
matbury
What's up pussycat?
 
matbury's Avatar
 
Join Date: Dec 2006
Location: Toronto, Canada
Posts: 1,990
Default

This is from the Flash docs:

Quote:
Package flash.display
Class public class Stage
Inheritance Stage DisplayObjectContainer InteractiveObject DisplayObject EventDispatcher Object


Language Version : ActionScript 3.0
Player Version : Flash Player 9



The Stage class represents the main drawing area. The Stage represents the entire area where Flash® content is shown.
The Stage object is not globally accessible. You need to access it through the stage property of a DisplayObject instance.

The Stage class has several ancestor classes — DisplayObjectContainer, InteractiveObject, DisplayObject, and EventDispatcher — from which it inherits properties and methods. Many of these properties and methods are either inapplicable to Stage objects, or require security checks when called on a Stage object. The properties and methods that require security checks are documented as part of the Stage class.

In addition, the following inherited properties are inapplicable to Stage objects. If you try to set them, an IllegalOperationError is thrown. These properties may always be read, but since they cannot be set, they will always contain default values.

accessibilityProperties
alpha
blendMode
cacheAsBitmap
contextMenu
filters
focusRect
loaderInfo
mask
mouseEnabled
name
opaqueBackground
rotation
scale9Grid
scaleX
scaleY
scrollRect
tabEnabled
tabIndex
transform
visible
x
y
I don't think it's advisable to try to do some things in AS 3.0 the same way you do them in AS 2.0. The display list and the event architecture are fundamentally different between the 2 versions and that's why they're not compatible, hence Zeus' comment about communicating between them.

I'd recommend studying up on AS 3.0's new features and the differences to AS 2.0. Here's a rough guide:

http://livedocs.adobe.com/flex/201/l...migration.html
__________________
http://matbury.com/
matbury is offline   Reply With Quote
Old 06-16-2008, 12:29 PM   #8
bilgenc
Registered User
 
Join Date: Jun 2008
Posts: 8
Default

var sahne=root;
sahne.function_name();

pls try it...



and using of _parent
as2 ex:
trace( _parent.sayi );
as3 ex:
var disari=parent;
trace(disari.sayi);

Last edited by bilgenc; 06-16-2008 at 12:33 PM.
bilgenc is offline   Reply With Quote
Old 06-23-2008, 03:23 PM   #9
jbailey
Registered User
 
Join Date: Mar 2006
Posts: 33
Default

Quote:
Originally Posted by lg101 View Post
Now if i need the same behavior in AS3 what can i use.
matbury is very correct when he said "I don't think it's advisable to try to do some things in AS 3.0 the same way you do them in AS 2.0. The display list and the event architecture are fundamentally different..." AS3 is based on ECMAScript. As such, a new philosophy should be used when coming to AS3.

A good solution to globals and roots is the Singleton pattern (http://www.darronschall.com/weblog/archives/000274.cfm). This prevents from the headaches that result from globals and roots. Therefore, it aligns with OOP in general and is in the ECMA standard. Other that get into your code can now use/debug it with ease. Moreover, the Singleton can be reused and passed around.

hope that helps
jbailey is offline   Reply With Quote
Old 08-06-2008, 11:22 PM   #10
madmac66
Member
 
Join Date: May 2007
Location: Fullerton, CA
Posts: 58
Send a message via AIM to madmac66 Send a message via Yahoo to madmac66 Send a message via Skype™ to madmac66
Default

I too am struggling with converting AS2 to AS3. I have tried some of the tips in this thread and have not had any work for me.

I have inherited an ongoing AS3 project that I have been asked to complete. This is my first foray into AS3 and it is kinda killing me slowly

The work has been created with a number of external custom classes. I am trying to add an additional function and call to one of these classes. At first I added some code to the root timeline, in good old AS2 fashion. Almost everything worked as expected. I have since copy/pasted the code from timeline into the class, and I need to connect a function in the class to an MC on the root stage. So far I cannot do it.

Strange thing is the Array() I declared here worked perfectly when on the root timeline, but it gives me 'undefined property' errors when used in the class.

Here is the additional code I added to the class, dial_mc being an MC on the root stage.


Code:
		public var locRoot = root;
		locRoot.function_name();
		
		public var dialRote:Array = new Array();
		dialRote[0] = 100.6;
		dialRote[1] = 60.7;
		dialRote[2] = 20.25;
		dialRote[3] = -19.5;
		dialRote[4] = -60;
		dialRote[5] = -99.9;

		public var dialPos:int = 0;
		public var curRote:Number = dialRote[dialPos];

		locRoot.dial_mc.rotation = dialRote[dialPos];
		locRoot.dial_mc.alpha = 100;

Any help with the root property or the array elements would be GREATLY appreciated right now

cheers
mm66
madmac66 is offline   Reply With Quote
Reply


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
Nice introduction to AS3 SHDR ActionScript 3.0 7 08-07-2008 11:17 AM
Create applications with AS3? De1l ActionScript 3.0 4 11-11-2007 03:40 AM
Converting to AS3. How would I do this in AS3 bubba ActionScript 3.0 7 10-08-2007 04:26 PM
help with as3 and mxml integration photoeq ActionScript 3.0 0 08-19-2007 07:53 PM
AS3 loads AS2 swf. AS2 swf not displayed darbar ActionScript 3.0 0 07-20-2007 09:12 PM


All times are GMT. The time now is 03:29 AM.

///
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.