简体   繁体   中英

AS3 - how to make 4 buttons and go to another frame when all 4 buttons clicked in random order

Hello I hawe 4 buttons on one frame and I can't figure out how to go to another frame when I clicked all 4 buttons in random order. Condition is one - all 4 buttons must be clicked at least once.

my buttons: Button1 Button2 Button3 Button4 in frame 1 - when al clicked - goto frame 2

Here is a pretty common way of getting this done.

private var _buttonsRemaining:int;
private function setup():void{
  button1.addEventListener(MouseEvent.CLICK,onButtonClicked);
  button2.addEventListener(MouseEvent.CLICK,onButtonClicked);
  button3.addEventListener(MouseEvent.CLICK,onButtonClicked);
  button4.addEventListener(MouseEvent.CLICK,onButtonClicked);
  _buttonsRemaining = 4;
}
private function onButtonClicked(e:MouseEvent):void{
  (e.target as EventDispatcher).removeEventListener(MouseEvent.CLICK,onButtonClicked);
  _buttonsRemaining --;
  if(_buttonsRemaining <= 0){
    allClicked();
  }
}
private function allClicked():void{
  trace("all buttons clicked.");
}

Unlike above, I like to declare 'allClicked' as an Event-friendly method. This way you can dispatch an event -- Event.COMPLETE maybe -- to trigger the method.

private function allClicked(e:Event=null):void{
  //...
}

I am not sure if I understand your problem right. I assume you mean be "frame" the view of a view controller, and you want to show another view of another view controller as soon as all of your 4 buttons in the 1st view have been pushed at least once.
I assume you have already your first view controller and its view, together with the 4 buttons, defined in the storyboard. You could then drag a new view controller from the library to your storyboard. You had to define a new subclass of an UIViewController in your app, and set the class of the new view controller in the storyboard to this new subclass.
Next you had to check if all of your buttons have been pressed. You could thus define one @property bool buttonXpressed for each button X, and set this property to YES when the IBAction method of this button is pressed. In each of these IBActions , you could check if all 4 properties are set to YES , and if so, you could present the view of the 2nd view controller by calling the presentViewController:animated:completion: method with the 2nd view controller as argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM