Skip to main content
Participating Frequently
October 29, 2019
Question

Syntax error: else is unexpected.in my code drag and drop

  • October 29, 2019
  • 1 reply
  • 439 views

this is my code

var hits = 0;
tom.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
tom.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
function mouseDownHandler(evt:MouseEvent):void {
var object = evt.target;
// we should limit dragging to the area inside the canvas
object.startDrag();
}
function mouseUpHandler(evt:MouseEvent):void {
var obj = evt.target;

var target = obj.dropTarget;

if (target == food && obj == tom){
}

obj.visible=false;
{

}
}



else if (target != food && obj == tom)

{

obj.x=1051.05
obj.y=135.05

}

 

----------

!= imean by it:that if target doesn't equals food

if is that right,why this error appears?

This topic has been closed for replies.

1 reply

JoãoCésar17023019
Community Expert
Community Expert
October 29, 2019

Hi.

 

Your code has several formatting mistakes. The main one is that your else if statement is outside the function body.

 

I believe your code should be something like this :

var hits = 0;

tom.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
tom.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

function mouseDownHandler(evt:MouseEvent): void
{
	var object = evt.target;
	object.startDrag();
}

function mouseUpHandler(evt:MouseEvent): void
{
	var obj = evt.target;
	var target = obj.dropTarget;

	if (target == food && obj == tom)
	{
		obj.visible = false;
	}
	else if (target != food && obj == tom)
	{
		obj.x = 1051.05;
		obj.y = 135.05;
	}
}

 

Please notice that I'm only looking at formatting and not functionality here.

 

 

Regards,

JC

Participating Frequently
October 31, 2019

Thank you for your effort

but (

obj.visible = false;

) doesn't work 

Participating Frequently
October 31, 2019

hello,i had edited the code,now there is not a proplem with ( else ) but another proplem appeared.

( if ) condetion itself ,here is the code

 

var hits = 0;
tom.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
tom.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
function mouseDownHandler(evt:MouseEvent):void {
var object = evt.target;
// we should limit dragging to the area inside the canvas
object.startDrag();
}
function mouseUpHandler(evt:MouseEvent):void {
var obj = evt.target;

var target = obj.dropTarget;

if (target == food && obj == tom)
{

obj.visible=false;

}
else {
obj.x=1051.05;
obj.y=135.05;
obj.stopDrag();
}

}

 

the proplem is in this part 

obj.visible=false;

it doesn't work

please i need a help