0

i am new in flash..please help me in this code..

why is it that when i run this program it will proceed to else statement???
how coould i fix this???

thanx..

var dragArray:Array = [orangeduck, monkey, yoyo, car];

	 
	for(var i:int = 0; i < dragArray.length; i++){
		dragArray[i].buttonMode = true;
	    dragArray[i].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);
	    dragArray[i].addEventListener(MouseEvent.MOUSE_UP, item_onMouseUp);
	}
	   	 
	function item_onMouseDown(event:MouseEvent):void {
	    var clip:MovieClip = MovieClip(event.currentTarget);
	    clip.startDrag();
	}
	function item_onMouseUp(event:MouseEvent):void {
	    var clip:MovieClip = MovieClip(event.currentTarget);
	    clip.stopDrag();
		if (i==1)
		{
			gotoAndStop(3);
			
			}
		if(i==2)
		{
			gotoAndStop(currentFrame+2);
			
		}
		if(i==3){
			gotoAndStop(currentFrame+3);
			
		}
		else{
			
			gotoAndStop(currentFrame+4);
			
		}
	}

Edited by dada2011: n/a

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by JasonHippy
0

From what I can see, the variable 'i' shouldn't be in scope in your item_onmouseup function because it is declared as a local variable in the for loop in the main bit of AS code in your keyframe. So you should really be getting some kind of error about an undeclared variable in your event handler function, because 'i' should really only be visible/in scope inside the for loop!

However, if the variable is visible/in scope in your event listener (which it really shouldn't be); the reason it's always going to the else statement should be obvious. Take a look how you're using 'i' in your loop:

In the main bit of actionscript on the keyframe, 'i' is declared in your for loop and is used to index the Movieclip objects in your array, where you add the event listeners and set the button-mode for each.

So 'i' starts at 0 (first object in the array). Because 0 is less than the break condition (i < array.length, where array.length would be 4). The body of the loop is entered, the 1st Movieclip's button state is set and has event listeners added.
Then 'i' is incremented (therefore i==1) and the loop reiterates.

Because 'i'==1 the condition (i < 4) is true. So the body of the loop is entered, the button state is set, event listeners are added for the 2nd item in the array. 'i' is then set to 2 and the loop reiterates.

Because (i < 4), the body of the loop is entered, button state set, event listeners added, 'i' is incremented to 3.

Because (i < 4), the body of the loop is entered, button state set, event listeners added and 'i' is incremented to 4 and the loop reiterates.

On this final iteration the value of 'i' is 4, so the break condition (i < 4) is not true and the for loop breaks out. So after your for loop has ended, the value of 'i' will always be 4.

So if 'i' is still visible/in scope in your handler function (again, it really shouldn't be) that would explain why your else statement is always being entered.

Also another thing to look at is your nested series of if statements. You should really be using 'else if' statements for the middle two 'if's. e.g.

if(i==1)
    ...
else if(i==2)
    ...
else if(i==3)
    ...
else
    ...

Because if i was 1 or 2, it would execute the code under those conditions. But when then it would check i against 3. And because i is not 3 it would also execute the code under the final else statement too. Which would be a bit of a bug!
Whereas using 'else if's this would no longer be a problem. Something for you to bear in mind in future!

Either way, it's all academic as the value of 'i' is not going to help you to determine which clip the event handler is acting upon. Partly because as we've already seen, the value will always be 4 (event though it technically shouldn't be in scope at all). Also because arrays in AS2 and AS3 are indexed from position 0, NOT position 1, if 'i' was even useful (which it isn't), you'd need to be checking the range 0..3 in your if statements, NOT 1..4!

So forget about using the value of 'i' to determine the clip referenced in the event handler. You should use some other way to determine which clip is being used.

For example, if the Movieclips all have instance names, you may be able to use the instance name to determine the clip. e.g.

if(clip.name == "orangeduck_mc")
    ....
else if(clip.name == "monkey_mc")
    ....
else if(clip.name == "yoyo_mc")
    ....
else
    ....

Which may work, depending on how you've got your .fla set up.

Alternatively, you could create a class for each of the different Movieclips, which should all derive from MovieClip. Then you could give each class a common property which has a unique value for each movieclip type. This would make it easy to determine the type of the clip in the array and therefore which keyframe/label to jump to in the parent/container clip.

However, without knowing exactly how you've got things set up in your .fla, I can't really offer much practical advice as there are probably several different ways around your problem. To compound things, I can't look at any .fla you might have because I don't use the Flash IDE at all for Flash development ATM. I use the Flex SDK and do everything in AS3, using classes. So I just use a text editor and the command line for much of my Flash development!

Anyway, I hope this helps in some way!

Edited by JasonHippy: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.