1

I am using actionscript 3 to make a point and click game. On frame 1 there are two buttons, button 1 and 2. On frame 3 there are two buttons, button A and B.

I want it so that after I click button 1 on frame 1, button A on frame 3 will be hidden or when I click button 2 on frame 1, button B on frame 3 will be hidden. The buttons that are hidden do not do anything when you click them.

thanks in advance

user701510
  • 5,563
  • 17
  • 61
  • 86

2 Answers2

3

If you try to remove something that is not in the display list yet Flash will thrown an error. I guess the best solution here is setting up a timeline variable to keep track of which button you have pressed. Something like this:

on frame 1

var b1:Boolean = false;
var b2:Boolean = false;

button1.addEventListener(MouseEvent.MOUSE_DOWN, checkButton);
button2.addEventListener(MouseEvent.MOUSE_DOWN, checkButton);

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

   gotoAndPlay(3);
}

on frame 3

myButtomA.visible = false;
myButtomB.visible = false;   

if (b1) myButtomA.visible = true;
if (b2) myButtomB.visible = true;
nelsond8
  • 509
  • 3
  • 6
1

There are many ways to remove an object from the display list, or otherwise hide it from view. For example:

button1.addEventListener(MouseEvent.MOUSE_DOWN, removeButton);

function removeButton(e:MouseEvent):void
{
 buttonContainer.removeChild(otherButton);
}

You could also set the target buttons "visibility" property to false. Very very simple, and you should be able to modify this snippet as needed.

On another topic.

I always urge people away from developing interactions on the timeline. It just confuses things, especially when you have such potent object oriented tools available in AS3...

Either way - Cheers and good luck.

  • update - In response to the OPs last comment:

Sure thing - although its a fairly deep question, depending on your experience. The main thing is getting involved with AS3's Object Oriented features, and driving the majority of your application via code alone.

Projects look like this: the Fla functions mostly as a container for assets (and if you utilize embeds, not even that), with a single, empty timeline frame. The main document class is then responsible for initiating and constructing all aspects of the project - everything from the load and control of data to the creation and addition of display list objects to establishing and controlling user interactions. Classic Package / Class design is used to create, potentially, dozens or hundreds of individual .AS files. The timeline is still used to create complex animations, but rarely involve any code (save a stop(); here or there).

When I was just learning this - I got a good book, and hit the googles a lot, to figure out the essentials of AS3, and just kind of went from there. Make a few simple projects, and you can see how indepth you can get, and quickly.

The benefit from working like this can not be overstated.

Let me know if you have any further questions. Good luck!

Bosworth99
  • 4,206
  • 5
  • 37
  • 52
  • 1
    Couldn't agree more with your "stay away from the timeline" sentiment. However, given that he is on the timeline, and he's trying to hide buttons on other frames that aren't in the display list yet, this code isn't going to work as is. He needs something that will manage the state of the app. Or he needs to rearrange the buttons to be on one frame. – Alex Jillard May 18 '11 at 16:28
  • Well the buttons represent objects that are placed in their own background or scenery like a TV inside a building or a car outside the building. Is there an example you can give that doesn't involve working with the timeline? – user701510 May 18 '11 at 18:42
  • @Alex. You are totally right. I deal with the timeline so infrequently I totally spaced that little detail. A great example that proves my point entirely... lol. @Nelson's answer is pretty accurate, the op will need to create another globally visible variable that dictates whether the button is displayed or not. This should then be set, based on click of button1. – Bosworth99 May 18 '11 at 19:58
  • Hey man, how would you do it then? I would really appreciate it if you told me. – user701510 May 23 '11 at 05:51