Skip to main content
MarkSmit
Inspiring
November 1, 2022
Answered

if statement ignored with onPressUp function

  • November 1, 2022
  • 1 reply
  • 316 views

I don't get it. At the 'onPressUp' function I want the 'button2' movieclip to disappear and 'buttonAn' movieclip to play. But only if it is over the 650 y coordinate. I added the if statement but it is ignored.

 

Project file:

https://www.dropbox.com/s/mvpa1r73urwsvie/hide%20ball%20on%20drag%2008.fla?dl=0

 

var root = this;
this.button2.visible = false
this.buttonAn.visible = false

function main()
{
	createjs.Touch.enable(stage);
	root.mouseDownListener = root.button.on("mousedown", onMouseDown);
	root.pressMoveListener = root.button.on("pressmove", onPressMove);
	root.pressUpListener = root.button.on("pressup", onPressUp);
}

function onMouseDown(e)
{
	e.currentTarget.offset = { y: (e.stageY / stage.scaleY) - e.currentTarget.y };
}

function onPressMove(e)
{
	e.currentTarget.y = (e.stageY / stage.scaleY) - e.currentTarget.offset.y;
	{
	if (e.currentTarget.y > 650)
	root.button.visible = false
	root.buttonAn.visible = true
	}
}

function onPressUp(e)
{
	if (e.currentTarget.y > 650)
	root.button.y = 275
	root.fallingShape.play();
	root.button2.visible = true
	root.buttonAn.play();
}

main();

 

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

Hi.

 

You're missing curly braces in the if statement. It should be like this:

 

 

 

function onPressUp(e)
{
	if (e.currentTarget.y > 650)
	{
		root.button.y = 275
		root.fallingShape.play();
		root.button2.visible = true
		root.buttonAn.play();
	}
}

 

 

 

If you don't use curly braces in a if statement, only the first line below it will be considered.

Regards,

JC

1 reply

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
November 1, 2022

Hi.

 

You're missing curly braces in the if statement. It should be like this:

 

 

 

function onPressUp(e)
{
	if (e.currentTarget.y > 650)
	{
		root.button.y = 275
		root.fallingShape.play();
		root.button2.visible = true
		root.buttonAn.play();
	}
}

 

 

 

If you don't use curly braces in a if statement, only the first line below it will be considered.

Regards,

JC

MarkSmit
MarkSmitAuthor
Inspiring
November 1, 2022

Such a rookie mistake of me. Now I can sleep tonight, haha thanks!

JoãoCésar17023019
Community Expert
Community Expert
November 1, 2022

Don't worry! It's a common mistake! 😄

 

You're welcome!