1

I'm creating a game, and most is going well. However it often has a button on a certain frame which should make it go to a certain frame and play. In my research I found the following solutions

  1. possible to hide the button on frame 1 and run code (need to know how to do this see).
  2. Only add the listener of the button on the frame in question ( I don't understand this one, did they mean: coding inside the timeline instead of inside a package)

source: a stackoverflow solved question

I also found this stackoverflow question link 2 this link shows a bit about hiding buttons, however I think it's not meant for inside a package. But I changed that see below.

a snippet from my code based on the example from the above link

    package 
    {

import flash.display.MovieClip;
import flash.events.Event;

import flash.events.MouseEvent;

    public class DocumentClass extends MovieClip
{
    public var b1:Boolean = false;
    public var b2:Boolean = false;        

    public function DocumentClass()
    {
        init();


    }
    private function init():void
    {


    button1.addEventListener(MouseEvent.MOUSE_DOWN, checkButton);

    button2.addEventListener(MouseEvent.MOUSE_DOWN, checkButton);

    private function checkButton(e:MouseEvent):void
    {
       if(e.target.name == button1) b1 = true;
       else b2 = true;

       gotoAndPlay(3);
    }

on frame 3 for example I could then perhaps do the same as in the above link:

    buttonA.visible = false;
    buttonB.visible = false;   

    if (b1) buttonA.visible = true;
    if (b2) buttonB.visible = true;

EDIT: I tried out the above code and it works, but my problem is not solved yet (see below).

So here is my question: I need advice/information about how to better use the above code. Reason is; I don't think the above code is correct for what i want (see below).

The above code doesn't work (I think) on buttons placed on frames beyond frame 1 which is a problem.

I need to have a number of frames without (visible) buttons for a intro, then on frame 5 for example a menu with a button that goes to next frame and stops [stop();] and on frame 6 a button that plays the next frames till end.

Community
  • 1
  • 1
Jacques
  • 61
  • 2
  • 14

1 Answers1

0

What you could do is add all of your buttons at the start (frame 1). Then, use a variable to keep track of the current frame (currentFrame). From here, you can create a switch statement to hide and show buttons as you need them. I would also make a "hideAllButtons" function to call each time you change frames. So something like:

if(currentFrame!=prevFrame){

    hideAllButtons();
    prevFrame=currentFrame;

    switch(currentFrame){
    case 1:
    hideAllButtons();
    break;
    case 2:
    btn1.visible=true;
    btn2.visible=true;
    break;
    }
}

Use a variable to keep track of the previous frame. If it is not the same as the current (new) frame, hide all of the buttons and then run through a switch statement, adding whatever buttons you need (by setting visible to true) depending on the frame.

The hideAllButtons function could just loop through the buttons and set all the visibility to false.

bandaro
  • 165
  • 7
  • excatly what i needed, don't know what break and case are for yet but i'll find out soon enough thanks – Jacques Jun 12 '13 at 15:54
  • Switch Case: http://www.republicofcode.com/tutorials/flash/as3switch/ I'm glad it worked. – bandaro Jun 12 '13 at 16:12