Be a Supporter!

oop: accesing stage.

  • 949 Views
  • 5 Replies
New Topic Respond to this Topic
lollipopers582
lollipopers582
  • Member since: Apr. 17, 2009
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
oop: accesing stage. Feb. 22nd, 2010 @ 12:48 PM Reply

AS3: Main

hey newgrounds i decided to write a tut for as3 main
this tutorial is how to acces the stage frame an oop file to do things like stage.addChild() in oop

if you dont know what oop is go to

AS3:OOP

ok now that that is out of the way time to explain :D

code:

package{

import flash.display.Stage; // you must import this

public class test {

private var lolz:Stage;

public function test(lolz:Stage){ // the constructor to acces this in the main project do var lols:test = new test(stage);

this.lolz = lolz;// ok so this means that the variable lolz = the argument of the constructor
}
}
}

ok so that is the code and like what it does is that it has a var called lolz and an argument called lolz witch is stage in the .fla project and that var turns into the argument. to use it just do this:

lolz.addChild(new child());

if you have any problems post something in this thread.


People say nothing is impossible, but I do nothing every day
Roses are red violets are blue, God made me pretty, what happened to you?

BBS Signature
BasV
BasV
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 23
Game Developer
Response to oop: accesing stage. Feb. 22nd, 2010 @ 12:59 PM Reply

At 2/22/10 12:48 PM, lollipopers582 wrote: if you dont know what oop is go to

Do you? Accessing the stage in this way is a bad practice in OOP. If you want the stage to do something, you should notify if by dispatching an Event.

milchreis
milchreis
  • Member since: Jan. 11, 2008
  • Offline.
Forum Stats
Member
Level 26
Programmer
Response to oop: accesing stage. Feb. 22nd, 2010 @ 01:01 PM Reply

You actually extend an DiplayObject as your main class will be attached to the display list.
Now you use your class' functions that it inherited from DisplayObject to addChilds to it.

lollipopers582
lollipopers582
  • Member since: Apr. 17, 2009
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to oop: accesing stage. Feb. 22nd, 2010 @ 01:18 PM Reply

At 2/22/10 12:59 PM, BasV wrote:
At 2/22/10 12:48 PM, lollipopers582 wrote: if you dont know what oop is go to
Do you? Accessing the stage in this way is a bad practice in OOP. If you want the stage to do something, you should notify if by dispatching an Event.

i prefer this way. its easier cuz like in timer events you can do like lolz.addChild(); without having to do like

public function lolz(){
addEventListener(Event.ADDED_TO_STAGE, init);

public function loltime(event:TimerEvent){
init();
}

public function init(event:Event){
stage.addChild();
}

see its way easier


People say nothing is impossible, but I do nothing every day
Roses are red violets are blue, God made me pretty, what happened to you?

BBS Signature
meneldil
meneldil
  • Member since: May. 27, 2004
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to oop: accesing stage. Feb. 22nd, 2010 @ 01:36 PM Reply

Uh... gah...

1. If you want to pass a reference to a container through a constructor, pass it as a DisplayObjectContainer, it makes the code more reusable.
2. Don't add anything to the stage's display list, it's a very bad habit. Learn to use the main timeline instead.


Indescriptive topic names have left me in despair!

CaptainPoncho
CaptainPoncho
  • Member since: Nov. 29, 2003
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to oop: accesing stage. Feb. 22nd, 2010 @ 02:00 PM Reply

If you do need global access to the stage (which you don't), it's easier just to make a static variable on your main as file that you can access by main.stage.


BBS Signature