PDA

View Full Version : Accessing document class property from another class



eightballmark
February 28th, 2010, 07:30 PM
I'm making a murder mystery game.

The Main.as file will have all of the game logic, such as the mysteryNumber.

The other actionScript files represent rooms that the user may click and depending on the 'mysteryNumber', it will execute the 1 of 6 scenarios.

So for example, if a user clicks the Library page, the LibPage.as will call the 'mysteryNumber' and execute some code to display on the library page.

It will be the same for the other 4 rooms.

The problem I have is that the LibPage.as won't call the 'theNumber' property from Main.as Please help.


Here's the Main.as

package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;

public class Main extends MovieClip
{
var mapPage:MapPage;
var essenPage:EssenPage;
var libPage:LibPage;
var mungosPage:MungosPage;
public var theNumber:Number;

public function Main()
{
mapPage = new MapPage;
essenPage = new EssenPage;
libPage = new LibPage();
libPage.mysteryNumber = this;
mungosPage = new MungosPage;
addChild(libPage);
mysteryNumber();

mapPage.essenButton.addEventListener(MouseEvent.CL ICK,
onEssenButtonClick);
mapPage.libButton.addEventListener(MouseEvent.CLIC K,
onLibButtonClick);
mapPage.mungosButton.addEventListener(MouseEvent.C LICK,
onMungosButtonClick);

libPage.mapButton.addEventListener(MouseEvent.CLIC K,
onMapButtonClick);
}

private function mysteryNumber():void
{
theNumber = Math.ceil(Math.random() * 1);
}

function onEssenButtonClick(event:MouseEvent):void
{
addChild(essenPage);
removeChild(mapPage);
}
function onLibButtonClick(event:MouseEvent):void
{
addChild(libPage);
removeChild(mapPage);
}
function onMungosButtonClick(event:MouseEvent):void
{
addChild(mungosPage);
removeChild(mapPage);
}
function onMapButtonClick(event:MouseEvent):void
{
addChild(mapPage);
removeChild(libPage);
}
}
}and here's the libPage.as

package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;

public class LibPage extends MovieClip
{
public var mysteryNumber:Main;

private function useMysteryNumber():void
{

trace(mysteryNumber.theNumber);
}
}
}

Krilnon
February 28th, 2010, 07:46 PM
You've never even assigned a value to mysteryNumber. Contextually, you could probably just use root['theNumber'] inside of useMysteryNumber, but the code that you posted doesn't actually include anything that calls that method, so I guess I'm just supposed to guess what's going on.

Idiomatically, you're supposed be sure that root will be a usable value whenever you access one of its properties, so you'll have to do that on your own. One common way is to listen for the event that is dispatched when your LibPage instance is added to the stage. However, that might be overkill if you know how your LibPage instances are going to be used.

komapeb
March 1st, 2010, 04:30 AM
"theNumber" could be a static member of your "Main.as" class i.e. "public static var theNumber:Number" and then you can call it with "Main.theNumber"

eightballmark
March 2nd, 2010, 07:52 AM
Thanks for the response guys but I'm on tight schedule so I've abandoned OOP for the moment. I've just put everything inside the Main class and it's working fine so far.

Here's what I've done to make each room load up

function onLibButtonClick(event:MouseEvent):void
{
if (1 == mysteryNumber){
addChild(libPage1);
removeChild(mapPage);
}
if (2 == mysteryNumber){
addChild(libPage2);
removeChild(mapPage);
}
}

And here's what I'm using to back out of the room.


function onMapButtonClick(event:MouseEvent):void
{
addChild(mapPage);
removeChild(this);
}

Although it does work I'm getting this error:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at Main/onMapButtonClick()

Any suggestions?