Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

two button with single function

New Here ,
Nov 16, 2013 Nov 16, 2013

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");

}

TOPICS
ActionScript
792
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 16, 2013 Nov 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.)

Translate
Community Expert ,
Nov 16, 2013 Nov 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.)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 16, 2013 Nov 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");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 16, 2013 Nov 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;

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 16, 2013 Nov 16, 2013

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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 16, 2013 Nov 16, 2013
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines