Archive for the ‘Actionscript 3’ Category

HTML and CSS in Flash

Saturday, May 2nd, 2009

I have just uploaded my slides on HTML and CSS in Flash from the last A-SFUG meeting

I hope you find it useful.

Use SharedObject to save data on users computer

Tuesday, March 24th, 2009

By using SharedObject you are able to store limited amounts of data on a user's computer. It's works in the same kind of way that your browser stores cookies. However there are some limitations with using this. Sometimes swf files may not be able to write the data, and the data can sometimes be deleted without your knowledge. Users can set their own limits on how much space flash player can use on their computer. When they lower the amount of disk space available, some local shared objects might get removed.

I'll show you a basic example of using SharedObject. We will have an input text field on stage and a button that will save the text in the text field.

Create a new AS3 Document and put a text field on stage and give it an instance name. I will call mine theText_txt.
Next make a button on stage and give it an instance name. I called mine save_btn.

Create a new layer in the main timeline and call it actions and lock it. Open up the actions panel and enter the following:

  1. var mySO:SharedObject;
  2. mySO = SharedObject.getLocal("myfirstSO");

The first line creates the shared object. The second line gets a local shared object called myfirstSO. Nothing is stored in it at the moment but we will assign data to it shortly.

Now when we click the button we want to assign whatever text is in the text field to the shared object. In the actions panel enter:

  1. save_btn.addEventListener(MouseEvent.CLICK, saveText);
  2.  
  3. function saveText(e:MouseEvent):void
  4. {
  5. mySO.data.myText = theText_txt.text;
  6. }

This has now assigned the text in the text field to a variable called myText in the SharedObject.

OK now that the text is saved we will want to populate the text field with that saved data when you open the swf again. In the actions panel enter the following.

  1. if (mySO.data.myText)
  2. {
  3. theText_txt.text = mySO.data.myText;
  4. }

This checks to see if there is any data in the myText variable in the SharedObject. If there is then it assigns that value to the text field.

Now test the movie. Enter some text in the text field and click your save button. Close the swf and test the movie again. You will see that the text you entered before is in the text field.

You can download the example here: SharedObject.zip

Hope this was helpful,
Ed

Instance names and code hinting – Tips & Tricks

Monday, March 2nd, 2009

code_hintingWhen coding in actionscript sometimes it can be a pain that you don't get code hinting with objects that are on stage. A good way of overcoming this is to change the instance names of your objects (Movie Clips, Buttons, etc.) to have an underscore "_" then the abbreviated name of that object at the end of the name.

For example, you may have a movie clip on stage called myMovieClip. Change the instance name to have an _mc at the end (myMovieClip_mc), and now in your actions panel type myMovieClip_mc and press period. You will now see all the code hinting for a movie clip popup (see image). This can save a whole heap of time when coding.

Here is a list of the different shortcuts that you can use to bring up the code hinting. Some of these are for AS2 but the rest will work with AS3.

Object type Variable suffix
Array _array
Button _btn
Camera _cam
Color _color
ContextMenu _cm
ContextMenuItem _cmi
Date _date
Error _err
LoadVars _lv
LocalConnection _lc
Microphone _mic
MovieClip _mc
MovieClipLoader _mcl
PrintJob _pj
NetConnection _nc
NetStream _ns
SharedObject _so
Sound _sound
String _str
TextField _txt
TextFormat _fmt
Video _video
XML _xml
XMLNode _xmlnode
XMLSocket _xmlsocket

I find that using this technique saves me a whole heap of time when programming.

Make a button from a MovieClip – Tips & Tricks

Tuesday, February 24th, 2009

movieclipbtnIn the first meeting of A-SFUG I showed a tip about creating the up, over and down states of a button using a movie clip. I thought that I should put it up here as a reference.

An easy way of creating a button from a movie clip is to give the names of the frames with the different states of the button "_up", "_over" and "_down".

Add a stop(); frame on frame 1 of the movie clip. Then out on the main timeline, open the actions panel and write:

myButton_btn.buttonMode = true;

Just change myButton_btn to the instance name of your movieclip.

Now test the movie and roll over the button and It will now go to the over state. Press down on it and it will go to the down state.

So you do not need to write the code for on roll over, on mouse down and on roll out. Flash will automatically detect what you are wanting to do and do it for you.

Now you may be asking "Why should I do it this way? Why can't I just make it as a regular button?" Well you can still do it that way if you like, however the benefit of doing it this way is that a movie clip is more light weight then a button. This means that flash can run a bit faster.

Cheers,
Ed

Forget 25 lines. Try 140 Characters

Thursday, February 19th, 2009

A while ago I talked about the 25 lines competition which is a contest to see who can make the most spectacular thing with only 25 lines of code. Today Grant Skinner has announced on his blog a competition called #tweetcoding. Basically the contest is to create the coolest thing you can in AS3, with  only using 140 characters of code. If you enter you stand to win a copy of Flash CS4!

Details can be found here: http://www.gskinner.com/blog/archives/2009/02/tweetcoding_con.html

Using addFrameScript

Monday, December 22nd, 2008

addFrameScript is a function that allows you to add actionscript to a specific frame (never would have guessed...). You need to pass in 2 parameters, the first is which frame you want to add the script on and the second is the function that you want to trigger on that frame. Now here is the pain with it, the frames start at zero. So if you wanted to put a script on frame 10 you would reference it with :

addFrameScript(9, myFunction);

So it can be a little tricky at first, but once you remember how to reference the frames it can be a very handy little function.

25lines finalists

Wednesday, December 17th, 2008

Here are the finalists of the recent 25 lines competition.
http://www.25lines.com/?page_id=139

They are really impressive. You are able to view the code for each entry as well.

Searching through arrays

Monday, July 7th, 2008

This works in both actionscript 2 and actionscript 3.

To search an array starting from the first position in the array we use:

  1. for (var i:int = 0; i < myArray.length; i++)
  2. {
  3. ...
  4. }

Where you would change myArray with the variable name of your array.

But what if you wanted to search through the array from the last item? Well the easiest way of doing this is by using:

  1. for (var i in myArray)
  2. {
  3. ...
  4. }

Now you know. And knowing is half the battle. - G.I. Joe

The Holy Bible of Actionscript 3.0

Friday, June 27th, 2008

Everyone should rush out and buy Essential Actionscript 3.0 by Colin Mook. It is a really great resource for actionscript programming with the new AS3 code. I actually managed to get this book free from Colin's Actionscript 3.0 From the Ground Up Tour when he came to Singapore. All I had to do was answer the question " can anyone name a text adventure game?" I called out "ZORK" and from that got a free book. Sometimes being a retro gamer pays off. I have had a good look through it and its really helpful for learning actionscript 3.

Scales in AS3

Thursday, May 29th, 2008

In actionscript 3 the scales for alpha, x scale and y scale have changed. In actionscript 2 they were a percentage rage between 0 and 100. In actionscript 3 this has changed to a scale between 0 and 1. So if you are converting from AS2 just divide your scale by 100.

Also the names of these have changed. In AS2 they were

  1. _xscale
  2. _yscale
  3. _alpha

In AS3 this has changed to

  1. scaleX
  2. scaleY
  3. alpha