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

AS3 OOP Menu navigation not working

Explorer ,
Feb 22, 2015 Feb 22, 2015

Copy link to clipboard

Copied

ok so frame 1 in the timeline/layer, I have this code

stop();

var btn1:MCButton = new MCButton();

btn1.x = 1048;

btn1.y = 300;

btn1.label_txt.text = "play";

addChild(btn1);

btn1.gotoAndStop(10);

var btn2:MCButton = new MCButton();

btn2.x = 1048;

btn2.y = 380;

btn2.label_txt.text = "help";

addChild(btn2);

var btn3:MCButton = new MCButton();

btn3.x = 1048;

btn3.y = 460;

btn3.label_txt.text = "highscore";

addChild(btn3);

and now I have a MCButton class (.as)

package  {

  import flash.display.MovieClip;

  import flash.events.MouseEvent;

  public class MCButton extends MovieClip {

  public function MCButton() {

  // constructor code#

  stop();

  this.buttonMode = true; //makes movie class into btn

  this.addEventListener(MouseEvent.MOUSE_OVER, hover);//listen for a mouse hover event

  this.addEventListener(MouseEvent.MOUSE_DOWN, down);//listen for a mouse down event

  }

  private function hover(e:MouseEvent):void {

  this.removeEventListener(MouseEvent.MOUSE_OVER, hover);

  this.addEventListener(MouseEvent.MOUSE_OUT, out);

  this.gotoAndStop(2);

  }

  private function out(e:MouseEvent):void {

  this.removeEventListener(MouseEvent.MOUSE_OUT, out);

  this.addEventListener(MouseEvent.MOUSE_OVER, hover);

  this.gotoAndStop(1);

  }

  private function down(e:MouseEvent):void {

  this.removeEventListener(MouseEvent.MOUSE_OVER, hover);

  this.addEventListener(MouseEvent.MOUSE_DOWN, down);

  this.gotoAndStop(3);

  }

  }

}

ok so I want btn1 (which is in my layers) to go frame 10, where I have another menu for "easy" "medium" "hard", I have put btn1.gotoAndStop(10); but that doesnt seem to work, so I'm really confused on what im doing wrong.

Please help me,

Thanks.

TOPICS
ActionScript

Views

598

Translate

Translate

Report

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

Guru , Feb 23, 2015 Feb 23, 2015

you can`t remove sth. that has not been created:

removeChild(btn6);

...

var btn6:MCButton = new MCButton();


you try to remove btn6-9 before they have been created

I understand why you want to do it, but you have to make sure that the remove part is left out the first time.

You can do it by making sure that the button you try to remove is actually there

Change every line that removes sth. to:

if(btn6!=null){

removeChild(btn6);

}

if(btn7!=null){

removeChild(btn7);

}

etc.

Caveat:

Trying to setup a Navigation like

...

Votes

Translate

Translate
Enthusiast ,
Feb 22, 2015 Feb 22, 2015

Copy link to clipboard

Copied

>>ok so I want btn1 (which is in my layers) to go frame 10

You have btn1 already on stage or what? Not sure what you're saying here. You can't have it already on stage and then instantiate it as per the above. And you can remove every reference to this in your button class code. You're in 'this', there's no reason to reference it like that.

Votes

Translate

Translate

Report

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
Explorer ,
Feb 23, 2015 Feb 23, 2015

Copy link to clipboard

Copied

sorry for the bad english, I have made a actionscript layer (frame1) in my timeline and I put this code inside it

var btn1:MCButton = new MCButton();

btn1.x = 1048;

btn1.y = 300;

btn1.label_txt.text = "play";

addChild(btn1);

because I have  a MCButton class already, I'm trying to make a OOP Menu, but thats where im getting stuck because I dont know how to make it so when the user clicks that button it goes to frame 10, where I have a second set of buttons called "easy", "medium", "hard"

Votes

Translate

Translate

Report

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
LEGEND ,
Feb 22, 2015 Feb 22, 2015

Copy link to clipboard

Copied

You want the timeline inside of btn1 to go to frame 10?  That is what the code is trying to tell it to do.  If so, using that button will cause it to go to frames 1,2, or 3 depending on how you interact with it.

Votes

Translate

Translate

Report

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
Explorer ,
Feb 23, 2015 Feb 23, 2015

Copy link to clipboard

Copied

on the timeline, my action script layer (1), i have code to generate buttons from my button class.

I want it, so when a user clicks on this btn1 it will go to frame 10 where I have a second set of buttons called "easy", "medium", "hard"

code.png

i hope this clears up confusion, looking forward to your replies.

ThaNKS A lot

Votes

Translate

Translate

Report

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
Guru ,
Feb 23, 2015 Feb 23, 2015

Copy link to clipboard

Copied

You created the buttons so they are still there even if you move to another point of the timeline.

If you created the buttons with code you have either to get rid of them like so:

function playMenu(e:MouseEvent):void{

  gotoAndStop(10);

removeChild(btn2);

...

removeChild(btn5);

}

or better:

just change all the labels to the desired ones and don`t jump to frame 10 at all:

function playMenu(e:MouseEvent):void{

   btn1.label_txt.tetx = ""easy";

   btn2.label_txt.text = "medium";

   btn3.label_txt.text = "hard";

  btn5.label_txt.text = "main menu";

// hide the buttons not needed

   btn4.visible = false;

}

Votes

Translate

Translate

Report

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
Explorer ,
Feb 23, 2015 Feb 23, 2015

Copy link to clipboard

Copied

thank you, doing the removeChild method worked,

but I now this error and im not sure why?

TypeError: Error #2007: Parameter child must be non-null.

  at flash.display::DisplayObjectContainer/removeChild()

  at hypernova/frame1()[hypernova::frame1:40]

This is my code for AS Layer frame 1

stop();

var btn1:MCButton = new MCButton();

btn1.x = 1048;

btn1.y = 300;

btn1.label_txt.text = "play";

addChild(btn1);

var btn2:MCButton = new MCButton();

btn2.x = 1048;

btn2.y = 380;

btn2.label_txt.text = "help";

addChild(btn2);

var btn3:MCButton = new MCButton();

btn3.x = 1048;

btn3.y = 460;

btn3.label_txt.text = "highscore";

addChild(btn3);

var btn4:MCButton = new MCButton();

btn4.x = 1048;

btn4.y = 540;

btn4.label_txt.text = "awards";

addChild(btn4);

var btn5:MCButton = new MCButton();

btn5.x = 1048;

btn5.y = 620;

btn5.label_txt.text = "exit";

addChild(btn5);

btn1.addEventListener(MouseEvent.CLICK, goplayMenu);

function goplayMenu (e:MouseEvent):void{

trace("play menu!");

  gotoAndStop(10);

}

removeChild(btn6);

removeChild(btn7);

removeChild(btn8);

removeChild(btn9);

and frame 10

stop();

removeChild(btn1);

removeChild(btn2);

removeChild(btn3);

removeChild(btn4);

removeChild(btn5);

var btn6:MCButton = new MCButton();

btn6.x = 1048;

btn6.y = 300;

btn6.label_txt.text = "easy";

addChild(btn6);

var btn7:MCButton = new MCButton();

btn7.x = 1048;

btn7.y = 380;

btn7.label_txt.text = "med";

addChild(btn7);

var btn8:MCButton = new MCButton();

btn8.x = 1048;

btn8.y = 460;

btn8.label_txt.text = "hard";

addChild(btn8);

var btn9:MCButton = new MCButton();

btn9.x = 1048;

btn9.y = 540;

btn9.label_txt.text = "main menu";

addChild(btn9);

btn9.addEventListener(MouseEvent.CLICK, mainMenu);

function mainMenu (e:MouseEvent):void{

trace("back to menu");

  gotoAndStop(1);

}

when I go back to main menu, I didnt want the playmenu to conflict with the main menu which is why I removedChild for everything

Votes

Translate

Translate

Report

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
Guru ,
Feb 23, 2015 Feb 23, 2015

Copy link to clipboard

Copied

LATEST

you can`t remove sth. that has not been created:

removeChild(btn6);

...

var btn6:MCButton = new MCButton();


you try to remove btn6-9 before they have been created

I understand why you want to do it, but you have to make sure that the remove part is left out the first time.

You can do it by making sure that the button you try to remove is actually there

Change every line that removes sth. to:

if(btn6!=null){

removeChild(btn6);

}

if(btn7!=null){

removeChild(btn7);

}

etc.

Caveat:

Trying to setup a Navigation like this can not be considered good OOP Practice.

That`s why I said you should target a solution that does not rely on timeline navigation.

Votes

Translate

Translate

Report

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