Skip to main content
Known Participant
November 16, 2013
Answered

two button with single function

  • November 16, 2013
  • 1 reply
  • 835 views

I want to create two buttons for single function and one button for one function. For example, I click button1, the screen will show "clicked1"

If I clicked button2, the screen will show "clicked2". If I click button1 and then click button2, it will show "two button are clicked". However, it does not work in two button with single function. What is the problem? Here is the code.

import flash.display.MovieClip;

import flash.events.MouseEvent;

var button1:MovieClip=new Button1();

button1.x=200;

button1.y=200;

addChild(button1);

var button2:MovieClip=new Button2();

button2.x=300;

button2.y=250;

addChild(button2);

button1.addEventListener(MouseEvent.CLICK, onClick);

button2.addEventListener(MouseEvent.CLICK, onClick2);

var isClicked:Boolean;

var isClicked1:Boolean;

function onClick(evt:MouseEvent):void{

          isClicked=true;

          trace("clicked1");

 

}

function onClick2(evt:MouseEvent):void{

          isClicked1=true;

          trace("clicked2");

 

}

if (isClicked&&isClicked1){

          trace("two buttons are clicked");

}

This topic has been closed for replies.
Correct answer kglad

this code

if (isClicked&&isClicked1){

          trace("two buttons are clicked");

}

has to execute AFTER your buttons are clicked.  ie, place it in a function and call that function when your buttons are clicked and after your booleans are updated.  (then you probably want to reset those booleans in that called function.)

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
November 16, 2013

this code

if (isClicked&&isClicked1){

          trace("two buttons are clicked");

}

has to execute AFTER your buttons are clicked.  ie, place it in a function and call that function when your buttons are clicked and after your booleans are updated.  (then you probably want to reset those booleans in that called function.)

Known Participant
November 16, 2013

It works but I want the screen only shows the "isClicked" only when I click button1. But now when I click button1, "isClicked" and "two buttons are clicked" show at the same time.

import flash.display.MovieClip;

import flash.events.MouseEvent;

var button1:MovieClip=new Button1();

button1.x=200;

button1.y=200;

addChild(button1);

var button2:MovieClip=new Button2();

button2.x=300;

button2.y=250;

addChild(button2);

var isClicked:Boolean;

var isClicked1:Boolean;

button1.addEventListener(MouseEvent.CLICK, onClick);

button2.addEventListener(MouseEvent.CLICK, onClick2);

button1.addEventListener(MouseEvent.CLICK, onClick3);

button2.addEventListener(MouseEvent.CLICK, onClick3);

 

function onClick(evt:MouseEvent):void{

          isClicked=true;

          trace("clicked1");

 

}

function onClick2(evt:MouseEvent):void{

          isClicked1=true;

          trace("clicked2");

}

 

function onClick3(evt:MouseEvent):void{

 

          trace("two buttons are clicked");

}

kglad
Community Expert
Community Expert
November 16, 2013

you didn't follow my suggestion.  again, use:

import flash.display.MovieClip;

import flash.events.MouseEvent;

var button1:MovieClip=new Button1();

button1.x=200;

button1.y=200;

addChild(button1);

var button2:MovieClip=new Button2();

button2.x=300;

button2.y=250;

addChild(button2);

var isClicked:Boolean;

var isClicked1:Boolean;

button1.addEventListener(MouseEvent.CLICK, onClick);

button2.addEventListener(MouseEvent.CLICK, onClick2);

 

function onClick(evt:MouseEvent):void{

          isClicked=true;

          trace("clicked1");

  bothClickedF();

}

function onClick2(evt:MouseEvent):void{

          isClicked1=true;

          trace("clicked2");

bothClickedF();

}

 

function bothClickedF():void{

 

if (isClicked&&isClicked1){

          trace("two buttons are clicked");

isClicked=false;

isClicked2=false;

}

}