Posts with the tag 'flash'

Our Christmas E-card game

Super Sledge Challenge game

To celebrate the festive season this year we’ve created a fun e-card with a flash game. The only impact on trees is virtual (if your steering isn’t very good). Our Christmas game is a 3d sledging challenge where you need to get to the bottom of the slope as quickly as possible, by avoiding the trees and picking up presents for time bonuses on route.

To play the game go here: http://festive.eastcoastinteractive.co.uk/

You can also create your own free custom version too for your friends which will display your own Christmas greeting.

1 comment December 9th, 2008

The Library, Classes and MovieClips in AS3

This post describes some of the differences I have stumbled over whilst working with actionscript 3.0 and Flash CS3 compared to earlier versions.

It starts with Export for Actionscript

In AS2 if you wanted to access the properties of an instance on the stage from code you had to ensure that the “export for actionscript” checkbox was selected in the linkage properties for that MovieClip in the library. If it wasnt your code wouldn’t see the instance and would fail silently. This was pretty confusing at first and even once you got used to it there would be times that it would still catch you out.

When you dont need to Export for Actionscript

In AS3 you do not need to select the “export for actionscript” checkbox to access the properties of an instance on the stage. Any MovieClip instance you drag onto the stage at design time becomes a property of the document class and can be accessed from code as long as it has an instance name.

If you do select “export for actionscript” for a clip in the library, like this.

class Square

When you click OK you will be greeted with the following slightly alarming pop-up message.

ActionScript Class Warning
A definition for this class could not be found in the classpath, so one will be automatically generated in the SWF file upon export.

This is actually nothing to worry about but may induce mild panic, you just click OK to continue and your swf still works fine. What the message is saying is that you are creating a new type, in other words a new class.

The MovieClip you have created in the library extends the definition of the flash MovieClip and adds extra features to it, at this point just the graphics you have drawn in it. The message is complaining that this extension of MovieClip doesn’t have a definition in code, there is not an .as file for it. However it lets you off lightly and generates the code inside the swf ( no .as files ) at compile time for your new type.

When you do need to Export for Actionscript

In AS3 “export for actionscript” should be selected when you want to add a MovieClip to the stage at runtime from actionscript. The syntax for doing this in AS3 with a MovieClip called Square in the library ( with export for actionscript selected ) looks like this.

1
2
3
4
5
var mySquare_mc:Square = new Square();
mySquare_mc.x= 100;
mySquare_mc.y= 100;

addChild (mySquare_mc );

In the first line an instance of the new type Square is created and named mySquare. At this point mySquare doesnt exist on the display list ( so cant be seen on the stage ) it only exists as a variable in actionscript. The next two lines set some of the properties of the mySquare variable, in this case position. Then finally with the addChild command I hook the mySquare MovieClip variable onto the display list. mySquare now exists as a variable in actionscript and is referenced and displayed on the stage.

In AS2 it was easy to think of MovieClips as things on the stage that your code could reference but with AS3 it becomes clear that a MovieClip is actually a type just as String or Number are.

In the case above Square extends MovieClip. In purely Object Oriented terms this means that if I create an instance of Square it will have all the methods and properties of a MovieClip such as mySquare.x or mySquare.visible plus any extra I add to it. However so far I havent provided code for any extra methods hence the ActionScript Class Warning when the clip is created in the library. If I wanted to add extra methods to Square I could do so by adding a new .as file called Square.as if I wrote the .as before creating the clip in the library I wouldnt get the warning.

Square.as

1
2
3
4
5
6
7
8
9
10
11
12
package
{
import flash.display.MovieClip;

public class Square extends MovieClip
{
public function sayHello()
{
trace( "Hello" );
}
}
}

Timeline code in my fla file

1
2
3
4
5
6
7
var mySquare_mc:MovieClip = new Square();
mySquare_mc.x= 100;
mySquare_mc.y= 100;

mySquare_mc.sayHello();

addChild (mySquare_mc );

The output now traces “hello”. As well as using the standard MovieClip methods and properties mySquare can also call the new method sayHello because it is of type Square and Square extends MovieClip.

One last point, notice that this time I have declared mySquare as type MovieClip and before I declared it as type Square. Either could be used, this duality of type is what is known as polymorphism in Object Oriented speak.

Hope that is of use to somebody!

5 comments July 7th, 2008

AS3 Basic Setup

Writing your first Actionscript 3.0 application with Flash CS3? Here is a quick guide to get the basics set up.

The basic set-up includes one .fla file and one .as file.

First create a fla. You can name this whatever you like. Maybe myFirstApp.fla. At this point you could start writing code directly into a frame on the timeline however best coding practice is to define a document class.

The document class can be specified by entering the name of a class into the property panel for the document. This is the same place where you would specify the size of your swf. Think of a name for your document class, maybe: myFirstApp. The document class doesnt have to have the same name as your main fla there is no link between them.

By typing myFirstApp into the property panel you are telling flash to create an instance of the myFirstApp class when the fla runs.

When compiling your fla flash will go and look for the code for the myfirstApp class. It will look for a file called myFirstApp.as by searching the directory the fla is in or in the directories specified under classpath from preferences.

The next step is to create the myFirstApp.as file. This just a text file with the extension changed to .as. Place the file in the same directory as your fla.

Contents of myFirstApp.as

1
2
3
4
5
6
7
8
9
10
11
package
{
import flash.display.Sprite;
public class myFirstAppextends Sprite
{
public function myFirstApp()
{
trace("hi");
}
}
}

The package tag places the myFirstApp class into the root package. Which means this class wont be stored in a subfolder just in the same directory as the fla.

The myFirstApp class extends Sprite. This means that the document class as well as having any further methods and propertys that you define will also have all the methods and properties of a Sprite. The import flash.display.Sprite statement lets the flash compiler know where to look for the definition of Sprite at compile time.

Note that the myFirstApp.as file should only contain a class called myFirstApp or you will get errors.

The document class unlike other classes must extend Sprite because the document class automatically represents the swf stage. Any movieclips instances you name and place on the stage at author time become properties of the document class and can be accessed by name from actionscript inside the class.

This part defines the constructor for the myFirstApp class.

1
2
3
4
public function myFirstApp()
{
trace("hi");
}

The constructor function for a class is a function that has the same name as the class and is called whenever an instance of the class is created. For the document class an instance is created when the swf runs. This means that the constructor is the entry point for our application and the trace statement is the first line of code to be executed at runtime. The creators of flash have borrowed these concepts from the java world.

And thats the basic set up!

Add comment July 2nd, 2008

Calendar

September 2017
M T W T F S S
« Feb    
 123
45678910
11121314151617
18192021222324
252627282930  

Categories

Most Recent Posts

Feeds