Flash - beyond animation 3

by Joe Gillespie — Apr 1, 2001

ActionScript for designers

I have always said that this site is not about programming but a little bit of scripting comes in useful, especially when it is used creatively.
You will appreciate that much of the Web is driven by server-side applications written in a wide variety of programming languages - C++, Java, Perl etc. and there are many other solutions like ASP, JSP, PHP. Developing Web applications using these means that you have to hire skilled programmers but designers sometimes need simpler solutions, ones that they can produce and control themselves.

JavaScript can handle quite a lot of simple Web scripting jobs and it is relatively easy to implement, but it has two major drawbacks. Firstly, not all surfers can make use of JavaScript. In fact, statistics that I've seen recently show that some 18% of general surfers either don't have JavaScript support or deliberately turn it off.

The other problem is that it is not secure. Other people can see your JavaScript source code by opening the HTML page in a text editor. Even if you use an external .JS file, the path to it will be visible in your HTML source and people can access it without too much difficulty.

Flash 5's ActionScript is as near to being a 'designer language' as you will get. It is an huge improvement over its clumsy Flash 4 predecessor and very similar to JavaScript in syntax. If you can write a little JavaScript, you are 90% of the way there with ActionScript, but ActionScript is easier than JavaScript, so if you learn ActionScript first, you are well on your way to becoming a JavaScripter too.

Flash gets round some of the JavaScript limitations I mentioned. If you believe the statistics, users are more likely to be able to handle Flash (4) than JavaScript. I'm not totally convinced about that, but anyway... Flash code is also more difficult for prying eyes to see. You can't see the Flash source code in a HTML page and a .SWF file can be protected so that it can't be opened and inspected - although there are ways round that too, so it's not entirely secure, just a little more trouble for would-be hackers.

Examples

The examples I'm giving here are not intended to replace CGI scripts on a server, they are just a kind of half-way-house between the complete insecurity of JavaScript and the heavy-weight security offered by a proper server-side solution. Call them 'quick and dirty' if you like, but they are still very useful to help you understand the basic principles involved in ActionsScript.

ActionScripts can be attached to objects or frames. So, a button, for instance, can have a script of its own that says:-

on (release) { gotoAndPlay ("start"); }

Which is ActionScript's way of saying "When the mouse button is released, go to the frame labeled "start" and play from there". There, that wasn’t too difficult, was it?

'on (release)' is a structure called a 'handler'. It 'handles' or manages mouse events - or 'mouse button events' to be more precise. Other possible mouse button events are things like Press, Release Outside, Roll Over, Roll Out, Drag Over, Drag Out.

The visual appearance of buttons during all this is handled by Flash separately, so you don't need all the image swapping code so necessary with JavaScript.

Frame scripts are attached to frames on the timeline. It is best to reserve a layer just for frame scripts and another one for frame labels. Always use labels instead of frame numbers, they are easier to read and debug and if you add or remove frames from your movie, you won't have to go in and change all the hard-coded frame numbers - which will, of course, then be invalid.

Here is an example of a frame script that introduces a timed pause in a timeline. A timed pause means that whatever text or images on the screen at the time will be static for a while giving the surfer time to read some text, for instance.

[Frame "setTimer"] delay = getTimer() + 5 * 1000;

GetTimer() is a built-in function that returns the number of milliseconds that have elapsed since the movie started playing. If we add 5 * 1000 to the current time, we have set the variable 'delay' to the current time plus 5 seconds (5 x 1000 milliseconds).

Then, two frames further on we put:-

if (getTimer() <= delay) { gotoAndPlay (currentFrame - 1); }

This sets up a loop that repeatedly checks the current time with getTimer() and sees if it is less than or equal to the value of 'delay' and if it hasn't yet reached that value, it goes back to the previous frame and plays until it has. When the preset delay has been reached, the movie continues on to the next frame to do what ever happens next.

A simple password protection script / An online survey using Flash

It uses ActionScript and therefore requires that you have the Flash 5 plug-in installed. If you don't have Flash 5 plug-in, the page will go into a continual loop. You can download the Flash 5 plug-in here.

 

The examples that you will find on the other page are a simple password protection script and an online survey. You can also download the Flash source files and adapt them for you own projects.

Well, that is just a little 'taster' to give you an idea what ActionScript is all about and only just scratches the surface.

If you stick to Flash's 'normal' scripting mode instead of 'expert', a lot of the hard work is already done for you. To find out more about ActionScript a book such as Foundation ActionScript (reviewed below) will get you up to speed in no time.

More Flash related Web resources.

Next month we go back to graphics and I will ask the question, "Is it finally time to ditch the Web-safe palette?".

Del.icio.us Digg Technorati Blinklist Furl reddit Design Float