Skip to main content
Participant
March 29, 2023
Answered

if statement in a click event doesn't work, even when the condition is true

  • March 29, 2023
  • 1 reply
  • 290 views

heres my code on frame 34

var q6music = true
var q6small = false
var q6gap = false

BigCollege.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_56);

function fl_ClickToGoToAndStopAtFrame_56(event:MouseEvent):void
{
	gotoAndStop(42);
}

GapYear.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_57);

function fl_ClickToGoToAndStopAtFrame_57(event:MouseEvent):void
{
	gotoAndStop(41);
	var q6gap = true;
	trace(q6gap);
}

 and on 41

trace(q6gap)
trace(q6music)

GoBack.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_58);


function fl_ClickToGoToAndStopAtFrame_58(event:MouseEvent):void
{
	if (q6gap == true && q6music == true) {
		trace("marked true");
		gotoAndStop(44);
	}

	else {
		gotoAndStop(34);
	}
}

when GapYear is clicked, q6gap is set to true and goes to frame 41. both q6gap and q6music trace as true at 41. however, the if statement doesn't run and it follows the else instead. there are no compiling errors and when I tried changing the else statement to else if (q6music == true) {trace("q6 music is true")} nothing ran when the button was clicked.

 

why won't the if statement run? maybe it can't access the variables?

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

 

I think the reason is that you're creating another variable called q6gap in the fl_ClickToGoToAndStopAtFrame_57 function. In this way, your q6gap variable on top continues to be false and thus your if statement doesn't evaluate to true.

 

The code in the forementioned function should be like this:

 

function fl_ClickToGoToAndStopAtFrame_57(event:MouseEvent):void
{
    gotoAndStop(41);
    q6gap = true; // without the 'var' keyword
    trace(q6gap);
}

 

 

Can you check this?

 

Regards,

JC

1 reply

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
March 29, 2023

Hi.

 

I think the reason is that you're creating another variable called q6gap in the fl_ClickToGoToAndStopAtFrame_57 function. In this way, your q6gap variable on top continues to be false and thus your if statement doesn't evaluate to true.

 

The code in the forementioned function should be like this:

 

function fl_ClickToGoToAndStopAtFrame_57(event:MouseEvent):void
{
    gotoAndStop(41);
    q6gap = true; // without the 'var' keyword
    trace(q6gap);
}

 

 

Can you check this?

 

Regards,

JC

Participant
March 29, 2023

it works! thanks 

JoãoCésar17023019
Community Expert
Community Expert
March 29, 2023

You're welcome!