Skip to main content
Inspiring
April 17, 2013
Answered

Why wont this work? Calling functions

  • April 17, 2013
  • 1 reply
  • 979 views

I have 3 .as classes.

Main.as (which is my doccument Class)

SceneSetup.as

menu.as

Main.as

package

{

          import flash.display.Sprite;

          import flash.display.Stage;

          public class Main extends Sprite

          {

                    private var SceneSet:SceneSetup;

                    public function Main()

                    {

                              SceneSet = addChild(new SceneSetup()) as SceneSetup;

                    }

          }

}

SceneSetup.as

package

{

          import flash.display.Sprite;

          public class SceneSetup extends Sprite

          {

                    var Frame1:menu = new menu();

                    var Frame2:Shop = new Shop();

                    public function SceneSetup()

                    {

                              ChangeScene(1);

                    }

                    public function ChangeScene(Frame:Number){

                              if(Frame == 1){

                                        addChild(Frame1);

                              }else if(Frame == 2){

                                        addChild(Frame2);

                              }

                    }

          }

}

menu.as

package

{

          import flash.events.MouseEvent;

          import flash.display.Sprite;

          public class menu extends Sprite

          {

                    var obj:SceneSetup = new SceneSetup();

                    public function menu()

                    {

                              Start.addEventListener(MouseEvent.CLICK,startgame);

                              Game.addEventListener(MouseEvent.CLICK,gameSettings);

                              Ins.addEventListener(MouseEvent.CLICK,instructions);

                              Arena.addEventListener(MouseEvent.CLICK,arena);

                    }

                    private function startgame(e:MouseEvent)

                    {

                              trace("GameStarted");

                              obj.ChangeScene(2);

                    }

                    private function gameSettings(e:MouseEvent)

                    {

                              trace("gameSettings");

                    }

                    private function instructions(e:MouseEvent)

                    {

                              trace("instructions");

                    }

                    private function arena(e:MouseEvent)

                    {

                              trace("arena");

                    }

                    //RollOvers

                    //----------------------------------------------------------------------------------------

          }

}

All I want to do is call the function in SceneSetup.as when the startgame function is triggered in menu.as

I tried to do it with the lines I have highlighted red.

But it dosen't work.

Help please!?



This topic has been closed for replies.
Correct answer kglad

This OOP Is really confusing :S

I do not have anything on my stage.

I have main.as which is my Document Class

I have SceneSetup Which is just a .as class

I have Menu.as which is a class linked to the main menu in the libary

Basicly I dont know if I have wrote my code right but what I want to happen is that when I start the .swf the main.as should import the SceneSetup.as to manage my scene and add the graphics to the stage, Then say if we add the Menu(MovieClip) to the stage that comes with the menu.as which lets us click the buttons

Thanks for your help so far. And sorry I am just quite new to Classes in as3


you should probably remove scenesetup.

add menu from your document class.

1 reply

kglad
Community Expert
Community Expert
April 17, 2013

1.  there's no SceneSetup instance created in Main.  ie, it's null and you should see a compiler error.

2.  if you do create a SceneSetup instance in Main, it won't be the same one created in menu.  that may or, may not, be a problem.

Inspiring
April 17, 2013

As it is now I get this:

Error: Error #1023: Stack overflow occurred.

          at SceneSetup()

          at menu()

And I dont get a compiler error.

when I remove these 2 lines I dont get Error #1023

var obj:SceneSetup = new SceneSetup();

obj.ChangeScene(2);



Amy Blankenship
Legend
April 17, 2013

You don't say anything about how your code is organized, or what instances you have on stage (although obviously you do have some stage instances).I'm going to assume that Main is the Main document and that it has an instance of Menu.as named theMenu on stage.

What you need to do is this:

In Main.as

public function Main() {

    super();

    sceneSet = addChild(new SceneSetup()) as SceneSetup;

   if (theMenu) {

      theMenu.obj = sceneSet;

   }

}

In Menu.as

public var obj:SceneSet;

...

protected function startGame(e:MouseEvent):void {

   if (obj) obj.changeScene(2);

}

I'd also suggest you add some removeChild code to your SceneSet Class.