Skip to main content
Participant
Mar 29, 2023 4:37 PM UTC
Answered

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

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 is closed to new replies. Start a new post to keep the conversation going.
JoãoCésar17023019
JoãoCésar17023019Correct answer
Community Expert
Mar 29, 2023 4:45 PM UTC

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

3 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
Mar 29, 2023 4:45 PM UTC

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

Translate
Participant
Mar 29, 2023 5:13 PM UTC

it works! thanks 

Translate
JoãoCésar17023019
Community Expert
Community Expert
Mar 29, 2023 5:52 PM UTC

You're welcome!

Translate