Wednesday, April 14, 2010

Javascript Scroll to Top

I was building a website for a client, all self contained into one Flash file. A good friend pointed out to me that with every link in the footer, while it changed the site as expected, the site stayed in one place. Meaning the content changed, but there was no jump to the top of the page like you'd expect from a normal HTML page. A lot of these sites I build are landing pages for AMOE, meaning I'm basically doing in Flash what is normally done in HTML/Javascript. The main difference is that I don't do Javascript, so building these in Flash and giving an HTML experience (sounds funny, I know) is my goal. Doing this in Flash cuts down on my development time greatly. Annnnnnnnnnd tangent.

The point of this post is, you can scroll to the top of the browser window directly through a Flash call. And that ActionScript is?

import flash.external.ExternalInterface;
ExternalInterface.call("self.scrollTo(0,0)");

That's it. No need to reference a Javascript script in your HTML page. You can also use getURL with the same value, but I found that if you need to run any other scripts at the same time, they may clash. Javascript can only run one at a time if running through the address bar. I found this out while trying to use SWFAddress with getURL. One had to give.

Tuesday, April 13, 2010

Global clearInterval

i = setInterval(function(){},100) + 1
while(i--){
clearInterval(i)
}

See that above? Run that at any time during your Flash to completely clear out any intervals you have running. If you're like me, sometimes you have 2-3 running at a time, and switching between frames and inadvertantly resetting your intervals renders some crazy results. When you call this, don't forget about your intervals that are running onMotionComplete. Make sure you stop your tweens at the same time, or they'll keep running and reset your interval. Damnit if that last piece of advice didn't waste an hour of my time!

I'll let Ted explain how it works:
http://ted.onflash.org/2004/11/fun-with-intervals.php