PDA

View Full Version : how do i access a function in the main class from another class?



nlween
March 1st, 2010, 08:26 AM
i am new to as3. i have a game that i want to put load screen on, which i created a separate class for. first the main class is initialized and in the constructor function, it does an addChild(loadScreen) which places an instance of the loadScreen MC on the stage. within the loadScreen is a "Play Button" that when a user presses this button i want it to continue startGame in the Main class. the way i tried it was Main.startGame from the loadScreen class, but that obviously does not work. is it possible to call a function from a different class, or is there just a simpler way of doing this that i am missing?

i have three files:
Main.fla
Main.as
loadScreen.as


the Main.as file:


package
{
import flash.text.*;
import flash.display.*;
import flash.events.*;

public class Main extends Sprite
{
var startPage:loadScreen = new loadScreen();

public function Main()
{
addChild(startPage);

}

public function startGame()
{
// The meat of the code for the game is here
}

}
}

the loadScreen.as file:


package {
import flash.text.*;
import flash.display.*;
import flash.events.*;

public class loadScreen extends MovieClip {


public function loadScreen() {
bLoad.buttonMode=true;
bLoad.addEventListener(MouseEvent.CLICK,playGame);
}
public function playGame(e:MouseEvent) {

Memory.startGame(); // <----- SOMETHING THAT DOES THIS
}
}
}

ClaudinoBur
March 1st, 2010, 10:20 AM
Hello,

You could have a field in Main class like public static var instance:Main,
and in your Main constructor function to set instance = this;
After you could access from everywhere Main.instance.startGame();

dr_tchock
March 1st, 2010, 11:07 AM
Might be better to get Main to listen for an event on your startPage instance and then call the relevant function.

in Main:



addChild(startPage);
startPage.addEventListener("loadComplete", startGame);


this will pass an event to your startGame function, so you need to include it as a parameter in the functions' signature:



public function startGame(e:Event){
startPage.removeEventListener("loadComplete", startGame); // don't need this listener any more so remove it to save memory/CPU
// The meat of the code for the game is here

}


your LoadScreen class will need to dispatch the event when it's done whatever you need it to do
(notice the capital 'L' - this is convention for class names - they start with a capital letter, instances of a class start with lowercase, eg. var loadScreen:LoadScreen)



public function playGame(e:MouseEvent) {

dispatchEvent(new Event("loadComplete"));
}

nlween
March 1st, 2010, 12:44 PM
Might be better to get Main to listen for an event on your startPage instance and then call the relevant function.

in Main:



addChild(startPage);
startPage.addEventListener("loadComplete", startGame);
this will pass an event to your startGame function, so you need to include it as a parameter in the functions' signature:



public function startGame(e:Event){
startPage.removeEventListener("loadComplete", startGame); // don't need this listener any more so remove it to save memory/CPU
// The meat of the code for the game is here

}
your LoadScreen class will need to dispatch the event when it's done whatever you need it to do
(notice the capital 'L' - this is convention for class names - they start with a capital letter, instances of a class start with lowercase, eg. var loadScreen:LoadScreen)



public function playGame(e:MouseEvent) {

dispatchEvent(new Event("loadComplete"));
}



worked perfectly! thanks :)
there is still a lot of stuff i need to get used to with changing over from as2 to as3. i like the idea of how classes work, but it really takes a whole different approach and a different way of thinking i guess. i'll get it eventually...

thanks again though!