gavin sim logo
 

Practical Notes

Teaching > Flash notes

OOP

Part 1

Create a new Flash .as file and type the following:

package {
 import flash.display.MovieClip;
 
 public class Main extends MovieClip {
 
 public function Main (){
 trace("hello");
 }
 }
 }

Save the file as Main.as. Here we are creating a class called Main which is an extension of the MovieClip class and therefore will inherit all its properties and methods. When teh class is called the constructor function will be called and Hello will appear in the output window as trace is used.

Create a new falsh document and in the properties panel, select the document class and type Main. Save the file as Main.fla and ensure it is in the same folder as the .as file.

Control / Test Movie.

 

Part 2

Create a new .as file and save this as Hello.as. Then type in the following code:

package {
 import flash.display.MovieClip;
 import flash.text.*;
 
 public class Hello extends MovieClip {
 
 public function Hello (){
 var myText:TextField = new TextField();
 myText.text = "Hello";
 addChild(myText);
 }
 }
 }

As we want to us the textfield we need to import the textfield class into our new class, this is done via import. The constructor function creates a new textfield and then assignes it the text hello and adds it to the stage using teh addChild method.

As per the first example create a new fla file calling it Hello and attach this class to it. Then test the movie.

 

Excercise

Modify the x and y values of the text so that x = 100 and y = 250.

 

Part 3

This time we are going to create a class to move a ball across the screen. Create a new .as file called Ball and attach the following code.

package {
 import flash.display.MovieClip;
 import flash.events.*;
 
 public class Ball extends MovieClip {
 private var speed:int;
 
 public function Ball (xspeed:int){
 speed=xspeed;
 this.addEventListener(Event.ENTER_FRAME, moveBall);
 }
 
 public function moveBall(evt:Event):void{
 this.x+=speed;
 }
 }
 }

This time we are creating a private variable called speed. The constructor function will receive a variable which is then assigned to the speed variable, and an eventlistener is addded to the object for the enter frame, which will call the moveBall function. This then simply adds the value of speed to the current x position.

Now create a new fla naiming it Ball. Draw a circle on the stage and convert it to a movieclip and in the advanced options select export for actionscript and give it a class name of Ball. Delete the object from the stage as we want to add it dynamically.

On frame 1 open the actions panel and type:

var ball:Ball = new Ball(2);
addChild(ball);

This will create a new Ball instance with a speed of 2 and then it will be added to the stage.

 

Part 4

Using the same files as part 3, delete the code from the frame 1. Now create a new .as file called MainBall.as and type the following code:

package {
 import flash.display.MovieClip;
 import Ball;
 
 public class MainBall extends MovieClip {
 private var ball:Ball;
 
 public function MainBall (){
 ball=new Ball(5);
 addChild(ball);
 }
 }
 }

Here we are importing the Ball class we just created creating a new instance of the ball and adding it to the stage.

In the fla file open the document class and link it to MainBall and test the movie. You will see that the ball is attached to the stage.

 

Part 5

Based on the lecture material we will modify the code to use getters and setters.