Copy link to clipboard
Copied
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 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.)
Copy link to clipboard
Copied
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.)
Copy link to clipboard
Copied
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");
}
Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
oh! thank you since I write as3 firstly. I do not know this method also can be used a function name. I think function must be used in addEventListener before I get your reply. Thank you very much!
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now