Here is some code to get you off the ground in Actionscript only programming, in combination with the link I previously provided.
- Run Flex Builder, create a new ActionScript Project. Name it whatever you like.
- Change the name of the .as file to Main.as
- Right click in the navigator window and create a new Actionscript file and name it ObjSpawn.as
- Paste the Code for Main and ObjSpawn.as into their respective files.
- Run the program.
- Read through the comment in the code that explains the structure and syntax.
This project is very simple, so there isn’t a lot going on to confuse you if you are picking this up for the first time.
Main.as
{
import flash.display.Sprite;
//The class name MUST be the SAME as the filename that contains it.
public class Main extends Sprite
{
//The function with the same name as the class is the known as the constructor.
//since this is the constructor for Main, it’s the first thing that runs and will control or fire off everything else.
public function Main()
{
//You must declare a variable to use it.
//If your variable is an instance of an object, then you need to instatiate that object with "new"
var _Wall:Sprite = new Sprite;
_Wall.graphics.lineStyle(5,0,100);
_Wall.graphics.drawRoundRect(0,300,500,10,5,5);
//the name attribute can be used to quickly identify an object later. You probably don’t want a lot of objects with the same name, so you may add a serialized number to a string for the name.
_Wall.name = "_Wall";
//everything that will be displayed MUST be added to a display container. if you do not add the following line or an equivalent to it, you will not see the object you created.
//This can be used to your advantage because you can build and manipulate an object independatly of when you display it.
//The order of these child objects determines the depth of the display, where the last child is at the front.
this.addChild(_Wall);
//the is calling the custom class I have written, which will call its constructor etc.
var _Rect:ObjSpawn = new ObjSpawn(100,100);
this.addChild(_Rect);
}
}
}
ObjSpawn.as
package {
//These are the libraries required for the script. Without including the correct libraries, the compiler will simply tell you that it can’t find the data types or methods you are trying to use.
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.geom.*;
//everything in Actionscript 3 must be in a class. When you create a class you can extend an existing class so that it inherits all of the properties and methods of that class.
public class ObjSpawn extends Sprite
{
//a private var or function can only be access by this class and its subclasses.
//variables are defined as: accessLevel var variableName:dataType = value;
private var _XSpeed:int = 5;
private var _YSpeed:int = 5;
//a public var or function is availabel to all other classes, even if they aren’t related to this class.
//function declaration is: accessLevel function functionName(requiredParameters)
public function ObjSpawn(_width:int, _height:int)
{
this.graphics.lineStyle(1,0,100);
this.graphics.beginFill(0xFF8800);
this.graphics.drawRoundRect(_width,_height,10,10,0,0);
//event listeners assign a function to an event. since the event listener is defined inside the class it only applies to this class.
this.addEventListener(MouseEvent.CLICK, onMouseClick);
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
//function used for the enterframe event for this class.
//this fires as many times a seconds as is running on the client, so you may ant to standardize the framerate programmatically.
private function onEnterFrame(event:Event):void
{
//action that looks at X and Y hits separately.
/*
if (this.x >= stage.width || this.x < 0)
{
_XSpeed = -_XSpeed;
}
if (this.y >= stage.height || this.y < 0)
{
_YSpeed = -_YSpeed;
}
*/
//This looks for a basic collision of 2 objects and takes action.
if(this.hitTestObject(parent.getChildByName("_Wall")))
{
_XSpeed = -_XSpeed;
_YSpeed = -_YSpeed;
}
//call to the movit function.
this.moveIt();
}
private function onMouseClick(evt:MouseEvent):void
{
var _Rect:ObjSpawn = new ObjSpawn(0,0);
this.addChild(_Rect);
}
//accepts direction and speed for moving the object. The effect gained from moving a # of pixels on the enter frame event is simple animation.
private function moveIt():void
{
this.x += _XSpeed;
this.y += _YSpeed;
//this.rotation += _XSpeed;
}
}
}