Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Why wont this work? Calling functions

Participant ,
Apr 17, 2013 Apr 17, 2013

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!?



TOPICS
ActionScript
874
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 18, 2013 Apr 18, 2013

you should probably remove scenesetup.

add menu from your document class.

Translate
Community Expert ,
Apr 17, 2013 Apr 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 17, 2013 Apr 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);



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 17, 2013 Apr 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 17, 2013 Apr 17, 2013

yes, that code is going to create a nasty loop:

in SceneSetup, you create a new menu (var Frame1:menu = new menu())

and in menu, you create a SceneSetup (var obj:SceneSetup = new SceneSetup();)

you need to rethink what you're doing and reorganize your project.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Apr 18, 2013 Apr 18, 2013

Well spotted! I didn't even see that.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 18, 2013 Apr 18, 2013

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

Capture.JPG

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2013 Apr 18, 2013

you should probably remove scenesetup.

add menu from your document class.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 19, 2013 Apr 19, 2013
LATEST

Ok yea I will do that

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines