somdow
somdow

Reputation: 6318

Flash AS3 if/else simple problem

I am trying to write a smooth transition piece and I don't know what is happening. I left it last night and it was working, I think, now its not.

here it is.

function btnclick(event:Event):void
{   
    if(event.target == load1 || load2)
    { 
        trace("working");
    } 
    else 
    {
        trace("not ");
    }
}

on my stage, I have 3 buttons,

load1.addEventListener(MouseEvent.CLICK, btnclick);
load2.addEventListener(MouseEvent.CLICK, btnclick);
load3.addEventListener(MouseEvent.CLICK, btnclick);

the 3 buttons instances are

load1, load2 and load3.

When i click buttons load1 or load2, I get the trace "working", but when I press button load3, i am still getting "working" when its supposed to say "not"

just to check i changed it to this:

function btnclick(event:Event):void
{
    if(event.target == load1 || load2) 
    {
            trace("working");
    } 
    else if (event.target == load3)
    {
            trace("not ");
    }
}

and the same problem, they all say "working"

Any ideas what it is that I am missing?

Upvotes: 0

Views: 4135

Answers (2)

Kaken Bok
Kaken Bok

Reputation: 3395

Simple thing: Your condition is wrong!

if(event.target == load1 || load2) {

That evaluates:

IF (event.target == load1)
OR IF (load2 is not empty)

Since load2 is your button, this will be always true.

Change it to:

if(event.target == load1 || event.target == load2) {

Upvotes: 4

Constantiner
Constantiner

Reputation: 14221

Try this:

function btnclick(event:Event):void{


    if(event.target == load1 || event.target == load2) {
           trace("working");
          } else {
               trace("not ");
           }

}

But I recommend you to use currentTarget instead of target:

function btnclick(event:Event):void{


    if(event.currentTarget == load1 || event.currentTarget == load2) {
           trace("working");
          } else {
               trace("not ");
           }

}

The problem with you code was in event.target == load1 || load2. And it is equivalent of (event.target == load1) || (load2) where Boolean(load2) is true if load2 isn't null. So event.target == load1 || load2 is always true in your case.

Upvotes: 3

Related Questions