Flash Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignFlash Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 6th, 2003, 10:40 AM
kmuse kmuse is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Somerville, MA
Posts: 5 kmuse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question "while" loop chokes movie

I'm attempting to create a simple fade from one movie object with a jpg in it to another. It works (although it loops indefinitely) when I call the fading up and down functions from a within a third, without a "while" loop. The fading methods ("increMethod" and "decreMethod") use onEnterFrame to change the alpha of the jpgs. But when I try to limit the fades to within 0 and 100 alpha values, the whole thing stops working. Does anyone have an idea of what's happening?

Here's the relevant code. Sorry about all the "trace"'s:

increment=10;


MapObject.prototype.decreMethod = function() {
this.onEnterFrame = function() {
this._alpha -= increment;
trace (this + " alpha: "+ this._alpha);
updateAfterEvent();
} //end onEnterFrame function
}//end decreMethod()


MapObject.prototype.increMethod = function() {
this.onEnterFrame = function() {
this._alpha += increment;
trace (this + " alpha: "+ this._alpha);
updateAfterEvent();
} //end onEnterFrame function
}//end increMethod()


MapObject.prototype.fadeMethod = function(mc) {
trace("increment equals "+ increment); //working
trace(this + "'s alpha is starting at: " + this._alpha);
trace(mc + "'s alpha is starting at: "+mc._alpha);
//the problem is here: *****************/
while (this._alpha < 100) {
this.increMethod(); //NOT BEING CALLED IN WHILE LOOP
}

while (mc._alpha > 0) {
mc.decreMethod(); //NOT BEING CALLED IN WHILE LOOP
}
//and this is not being called: ***************************/
mapUp=this;
trace ("new mapUp: "+ mapUp);
} // end fadeMethod function

THANKS!!

Reply With Quote
  #2  
Old September 8th, 2003, 10:33 AM
bret bret is offline
flash junkie
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: CO, USA
Posts: 172 bret User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 38 sec
Reputation Power: 6
Send a message via Yahoo to bret
hey, why are you using a while loop and not an if conditional? a while loop will definitely choke, because it will loop until it's resolved all in between frame refreshes. So, even if you have an onEnterFRame event that is called in your while loop, it will always choke because your condition in your while loop is always true.

i understand that you are trying to limit your onEnterFrame call, but it would probably be better to check that in your enterFrame event

Code:
if(this._alpha<0){
     this._alpha -= increment
} else {
     delete this.onEnterFrame;
}


and so on

-bret

Reply With Quote
  #3  
Old September 8th, 2003, 11:07 AM
kmuse kmuse is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Somerville, MA
Posts: 5 kmuse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thumbs up

A million thanks, bret. Your suggestion worked beautifully.

I still can't wrap my head around your observation that the while loop will never resolve because the movie's alpha will never reach its destination value. If it does so outside the loop, why not inside? What does it mean that the while loop will iterate until it "resolves all in between frame refreshes"? Obviously this isn't an urgent question, so if you feel like answering it that would be great; otherwise, no sweat.

Thanks again.

Reply With Quote
  #4  
Old September 8th, 2003, 11:26 AM
bret bret is offline
flash junkie
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: CO, USA
Posts: 172 bret User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 38 sec
Reputation Power: 6
Send a message via Yahoo to bret
sure, for loops and while loops actually resolve before the next frame is displayed. What i mean is, when you do a for loop, it will completely loop before the next line of code is run. Which also means that it will loop all the way through before the frame is refreshed.

Here's a clear example of what i am talking about:

Code:
this.onEnterFrame = function(){
	if(counter++<5){
		trace("the current frame is " + counter + "\n");
		for(i=0;i<10;i++){
			trace("\tfor loop i value = " + i);
		}
		var j=0;
		while(j++<10){
			trace("\twhile loop j value = " + j);
		}
		trace("\n");
	}
}

copy and paste that into a blank flash doc... watch your output window and you will see the order that the events occur.

First, it traces the first frame, then all of the for loop elements, then all of the while loop elements, then on the next frame (using onEnterFrame) it goes through the loops again. i've set it to stop at 5 frames just to show the effect. But you see that all of the code resolves before the next frame is displayed or processed. does that make more sense?

-bret

Reply With Quote
  #5  
Old September 8th, 2003, 02:18 PM
kmuse kmuse is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Somerville, MA
Posts: 5 kmuse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I see. But in my movie, the "onEnterFrame" clause was inside the while-loop; while in yours, the while- and for- loops are inside the "onEnterFrame". So it would seem, in mine, that the movie would refresh before the next iteration of the while loop, and therefore the alpha would change. What am I missing?

The other thing that's strange, and maybe it's related, is that the index values i and j in your demo start at 1. Why don't they start at 0?

Reply With Quote
  #6  
Old September 8th, 2003, 03:13 PM
bret bret is offline
flash junkie
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Location: CO, USA
Posts: 172 bret User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 38 sec
Reputation Power: 6
Send a message via Yahoo to bret
here's the difference - when you call a function that has an onEnterFrame event inside of a for loop, what you are doing is reasigning that onEnterFrame event in every itteration of the loop.

Take this as an example:

Code:
foo = function(){
	trace("you called foo");
	this.onEnterFrame = function(){
		trace("i'm the onEnterFrame event");
	}
}
for(i=0;i<10;i++){
	foo();
}


my output says:

Quote:
you called foo
you called foo
you called foo
you called foo
you called foo
you called foo
you called foo
you called foo
you called foo
you called foo
i'm the onEnterFrame event ...


That tells me that it calls the function 10 times before the onEnterFrame code is executed. Does that make more sense on the order?

About the loops - you'll notice that only the for loop is starting at 0. The while loop starts at 1 and so does the counter variable. You'll also notice that i don't declare the counter variable or the i in the for loop.

my for loop creates teh variable like normal (i=0). while the while and counter variables are created in the (variable++) statement.

like my counter:

if(counter++<5)

it's saying if counter+1 <5 then... So, if counter doesn't exist, it creates it and adds one to it straight off, otherwise it just adds one. Same with my while loop, if takes the variable j, adds one to it and then does the conditional on it. The reason i had to recreate the j variable is that by default, it works on variables that are not temporary (having the life of the loop). Like in for loops, those variables will die once the loop is done. While loops don't automatically create this variable, so i have to reinitialise it if i want the loop to run again. make more sense?

-bret

Reply With Quote
  #7  
Old September 9th, 2003, 10:00 AM
kmuse kmuse is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Somerville, MA
Posts: 5 kmuse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
By saying each iteration of the while loop "reassigns" the onEnterFrame event, do you mean it overrides its previous assignment? Is the onEnterFrame waiting for all the other code to execute before it does, even if it contains instructions for a different movie clip? You've demonstrated convincingly that it doesn't behave as other functions do. I looked through a lot of web resources and books that I have on it, but couldn't find an adequate description of what it does and why it can escape a loop like that.

Your loop explanation is clear. Thank you so much, b. You've gone way beyond cool.

km

Reply With Quote
  #8  
Old September 9th, 2003, 10:04 AM
kmuse kmuse is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Location: Somerville, MA
Posts: 5 kmuse User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hold on. You explained, "..loops resolve before the next frame is displayed". And onEnterFrames obviously don't! I think that's the part I missed. I was thrown off because the while instruction was in a different movie than the one being called in the the onEnterFrame event. But I think it's clear now. THANKS FOR EVERYTHING, B!!!

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignFlash Help > "while" loop chokes movie


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway