17.5.11

Okay. Here is the code that didn't work last time.

package  {

import flash.display.MovieClip;
import flash.sensors.Accelerometer;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.ui.KeyLocation;


import flash.events.KeyboardEvent;

public class PlaneGame extends MovieClip {

public var level:Level1;
public var player:Plane;


public function PlaneGame() {
creategame();
createplayer();
addlisteners();

}
private function creategame():void
{
var level:Level1 = new Level1() ;
level.x=level.y=-0
addChild(level);

}


private function createplayer():void
{

var player:Plane = new Plane();
player.x=player.y = 100;
addChild(player);


}

private function addlisteners():void
{
stage.addEventListener(Event.ENTER_FRAME, movelevel, false, 0, false);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveplayer, false, 1, false);
}


private function movelevel(e:Event):void
{


level.x-=10
}

private function moveplayer(k:KeyboardEvent):void

{
switch (k.keyCode)
{
case Keyboard.UP:
{
player.y -= 5;
break;
}
case Keyboard.DOWN:
{
player.y += 5;
break;
}
case Keyboard.LEFT:
{
player.x -= 5;
break;
}
case Keyboard.RIGHT:
{
player.x += 5;
break;
}
}
}


}

}



I'm going to remove the function "add listeners" from the constructor, add those listeners to the creategame and createplayer functions and then lead them to the resultant functions. 

6.5.11

Game Doesn't Work!

As was written in my previous post, my Game didn't work. There were two compiler errors:


TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PlaneGame/movelevel()

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PlaneGame/movelevel()

From just looking at these two errors, I have discerned that the cause of the problem is the "null object reference", which I assume means the the Actionscript file is calling objects that were not even instantiated yet or added to the Display list- one or the other.

Well, looking back at the code, I can see that the main variables in the game- na aircraft and na stage- have already been instantiated, and therefore the problem must be that the objects were not added to the display list before they were worked on. So that's my answer.

First Game OMG!

Yes well I tried to make a game a week ago. Here is the code:


package  {

import flash.display.MovieClip;
import flash.sensors.Accelerometer;
import flash.events.Event;
import flash.ui.Keyboard;
import flash.ui.KeyLocation;


import flash.events.KeyboardEvent;

public class PlaneGame extends MovieClip {

public var level:Level1;
public var player:Plane;


public function PlaneGame() {
creategame();
createplayer();
addlisteners();

}
private function creategame():void
{
var level:Level1 = new Level1() ;
level.x=level.y=-0
addChild(level);

}


private function createplayer():void
{

var player:Plane = new Plane();
player.x=player.y = 100;
addChild(player);


}

private function addlisteners():void
{
stage.addEventListener(Event.ENTER_FRAME, movelevel, false, 0, false);
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveplayer, false, 1, false);
}


private function movelevel(e:Event):void
{


level.x-=10
}

private function moveplayer(k:KeyboardEvent):void

{
switch (k.keyCode)
{
case Keyboard.UP:
{
player.y -= 5;
break;
}
case Keyboard.DOWN:
{
player.y += 5;
break;
}
case Keyboard.LEFT:
{
player.x -= 5;
break;
}
case Keyboard.RIGHT:
{
player.x += 5;
break;
}
}
}


}

}


Not surprisingly, the code didn't work. I got two compiler errors. These are the errors:


TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PlaneGame/movelevel()

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at PlaneGame/movelevel()


What do I want to do with this, huh?


10.4.11

This one's a Larf

If you really want to do something for a larf, link this script up to your FLA file and run it.



package  {
import flash.display.MovieClip;
import flash.events.*
import flash.system.*
import flash.errors.InvalidSWFError;
import flash.filters.BlurFilter;
import flash.display.ActionScriptVersion;
import flash.net.FileReferenceList;
import flash.desktop.Clipboard;
import flash.sensors.Geolocation;
import flash.external.ExternalInterface;
import flash.errors.IOError;
import flash.text.FontType;
import flash.text.CSMSettings;
import flash.ui.Keyboard;
import flash.ui.KeyboardType;
import flash.profiler.profile;
import flash.xml.XMLNode;
import flash.printing.PrintJobOptions;
import flash.ui.KeyLocation;

public class Structure extends MovieClip {
public function Structure() {
PlusListeners()
}
public function PlusListeners() {
stage.addEventListener(Event.ENTER_FRAME, Event1);
stage.addEventListener(Event.ENTER_FRAME, Event2);
stage.addEventListener(Event.ENTER_FRAME, Event3);
stage.addEventListener(Event.ENTER_FRAME, Event4);
stage.addEventListener(Event.ENTER_FRAME, Event5);
stage.addEventListener(Event.ENTER_FRAME, Event6);
stage.addEventListener(Event.ENTER_FRAME, Event7);
stage.addEventListener(Event.ENTER_FRAME, Event8);
stage.addEventListener(Event.ENTER_FRAME, Event9);
stage.addEventListener(Event.ENTER_FRAME, Event10);
}
private function Event1(event:Event):void {
trace("flash.system.ApplicationDomain.MIN_DOMAIN_MEMORY_LENGTH: " + flash.system.ApplicationDomain.MIN_DOMAIN_MEMORY_LENGTH)
}
private function Event2(event:Event):void {
trace("flash.system.Security.sandboxType: " + flash.system.Security.sandboxType);
}
private function Event3(event:Event):void {
trace ("flash.system.Capabilities.cpuArchitecture: " + flash.system.Capabilities.cpuArchitecture);
}
private function Event4(event:Event):void {
trace ("flash.errors.IOError: " + flash.errors.IOError);
}
private function Event5(event:Event):void {
trace ("flash.system.Capabilities.serverString: " + flash.system.Capabilities.serverString);
}
private function Event6(event:Event):void {
trace ("flash.display.ActionScriptVersion.ACTIONSCRIPT3: " + flash.display.ActionScriptVersion.ACTIONSCRIPT3);
}
private function Event7(event:Event):void {
trace("flash.ui.Keyboard.CharCodeStrings: " + flash.ui.Keyboard.CharCodeStrings);
}
private function Event8(event:Event):void {
trace ("flash.desktop.Clipboard.generalClipboard.formats: " + flash.desktop.Clipboard.generalClipboard.formats)
}
private function Event9(event:Event):void {
trace ("flash.external.ExternalInterface.objectID: " + flash.external.ExternalInterface.objectID); 
}
private function Event10(event:Event):void {
trace ("flash.system.IME.conversionMode: " + flash.system.IME.conversionMode);
}
}
}

What you're seeing here is the internal information that is stored deep inside Flash; everything from the Keyboard Character codes to the server string. 

The result should look like this:

flash.system.ApplicationDomain.MIN_DOMAIN_MEMORY_LENGTH: 1024
flash.system.Security.sandboxType: localTrusted
flash.system.Capabilities.cpuArchitecture: x86
flash.errors.IOError: [class IOError]
flash.system.Capabilities.serverString: A=t&SA=t&SV=t&EV=t&MP3=t&AE=t&VE=t&ACC=t&PR=t&SP=f&SB=f&DEB=t&V=WIN%2010%2C1%2C52%2C14&M=Adobe%20Windows&R=1280x1024&COL=color&AR=1.0&OS=Windows%20Vista&ARCH=x86&L=en&IME=t&PR32=t&PR64=f&PT=External&AVD=f&LFD=f&WD=f&TLS=t&ML=5.1&DP=72
flash.display.ActionScriptVersion.ACTIONSCRIPT3: 3
flash.ui.Keyboard.CharCodeStrings: Up,Down,Left,Right,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21,F22,F23,F24,F25,F26,F27,F28,F29,F30,F31,F32,F33,F34,F35,Insert,Delete,Home,Begin,End,PgUp,PgDn,PrntScrn,ScrlLck,Pause,SysReq,Break,Reset,Stop,Menu,User,Sys,Print,ClrLn,ClrDsp,InsLn,DelLn,InsChr,DelChr,Prev,Next,Select,Exec,Undo,Redo,Find,Help,ModeSw
flash.desktop.Clipboard.generalClipboard.formats: air:text
flash.external.ExternalInterface.objectID: null
flash.system.IME.conversionMode: ALPHANUMERIC_HALF

9.4.11

How Does it Work?

Here's how the preceding code works. At the top you will see the import commands for all of the classes used within the document. Below that is the class declaration. This class is an extension of the Movieclip class because it is attached to my main FLA file, which can only instantiate a Movieclip.

Inside the Class declaration is the main constructor. This is the big branch that will spawn all of the little branches. Now within the constructor there is only one function to b run. This is PlusListeners(). All this function does is add ten event listeners to the stage.

As you can see in the body of PlusListeners(),  each of the ten listeners is  identical. Each listener waits for the FLA stage to dispatch an event. The name of that event is ENTER_FRAME. This event is dispatched every time the animation moves forward a frame, which will occur automatically unless you tell Flash to stop moving forward.

Anyway, at the end of each listener a function is called. In the first listener, that function is called Event1. After the right brace signifies that PlusListeners() is complete, each of the "Event" functions is . In the case of Event1, all the function does is call a trace function.

private function Event1(event:Event):void {
trace("This is event1")
}

This message will be printed in the output window of Flash every time a frame is passed in the animation.

You'll notice that at the end of the Actionscript file are three braces. The first declares an end to the Event10 function, the second declares that the Structure class has been completed, and the third denotes the completion of the package.

Event Listeners

I have finally come up with a standard Actionscript 3.0 structure that I will use in my future experiments. This is it:

package  {

import flash.display.MovieClip;
import flash.events.*

public class Structure extends MovieClip {


public function Structure() {
PlusListeners()
}

public function PlusListeners() {
stage.addEventListener(Event.ENTER_FRAME, Event1);
stage.addEventListener(Event.ENTER_FRAME, Event2);
stage.addEventListener(Event.ENTER_FRAME, Event3);
stage.addEventListener(Event.ENTER_FRAME, Event4);
stage.addEventListener(Event.ENTER_FRAME, Event5);
stage.addEventListener(Event.ENTER_FRAME, Event6);
stage.addEventListener(Event.ENTER_FRAME, Event7);
stage.addEventListener(Event.ENTER_FRAME, Event8);
stage.addEventListener(Event.ENTER_FRAME, Event9);
stage.addEventListener(Event.ENTER_FRAME, Event10);
}

private function Event1(event:Event):void {
trace("This is event1")
}
private function Event2(event:Event):void {
trace("This is event2");
}
private function Event3(event:Event):void {
trace ("This is event3");
}
private function Event4(event:Event):void {
trace ("This is event4");
}
private function Event5(event:Event):void {
trace ("This is event5");
}
private function Event6(event:Event):void {
trace ("This is event6");
}
private function Event7(event:Event):void {
trace("This is event7");
}
private function Event8(event:Event):void {
trace ("This is event8");
}
private function Event9(event:Event):void {
trace ("This is event9");
}
private function Event10(event:Event):void {
trace ("This is event10");

}

}

}
Now, I had a few unique errors when creating this. This is the first error:

C:\Users\Owner\Documents\FlashCS5\Structure\Structure.as, Line 26 1046: Type was not found or was not a compile-time constant: Event.

After some trial and error I figured this one out. When flash compiled my SWF, it was looking for the Event constant and couldn't find it because I hadn't imported it. Now often times flash will import classes automatically as needed, but there is no guarantee. Eventually you're going to have to learn the classes so that you can import them when you need them.

import flash.events.* 

The second error was a nail biter:

C:\Users\Owner\Documents\FlashCS5\Structure\Structure.as, Line 26 1126: Function does not have a body.

This is the function, (and it does have a body):

private function Event1(event:Event);{
trace("This is event1")
}

So why does Flash keep lying to me and saying that my function doesn't have a body when it does? Well, there's a semicolon at the end of the function when the compiler sees that semicolon, the function body becomes invisible. 

this version works:

private function Event1(event:Event):void {
                trace("This is event1")
                }



27.1.11

Using Classes

Well a new group of errors has cropped up. All three are based on the BlueRight.as curiously. I don't see any differences between this one and the rest, but this is the first error:


C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\BlueRight.as, Line 31 1149: The return statement cannot be used in static initialization code.

This error was on the GetStepAmount function in the BlueRight.as file.

C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\BlueRight.as, Line 31 1149: The return statement cannot be used in static initialization code.


This is the code for the Getter function:

internal function GetStepAmount();
{
return _StepAmount
}

And this is the code for the Setter function:

internal function SetStepAmount(value:number):Number
{
if (isNaN(value)){throw new Error ("Pivotal Variable is of the wrong type.") 
}
else
{
_StepAmount = value
}
}

If you look closely, you can see that GetStepAmount- which is designed to return a Number value- does not return anything, while SetStepAmount- which is only designed to set a value and returns nothing- is returning a  non-existent number. 

Here's how it should look:

internal function SetStepAmount(value:number);
{
if (isNaN(value)){throw new Error ("Pivotal Variable is of the wrong type.") 
}
else
{
_StepAmount = value
}
}

internal function GetStepAmount():Number
{
return _StepAmount
}

Using Classes

To break things down, after I corrected all apparent errors on the main class file a host of new errors popped up. There were three errors per button.

The first error told me that I shouldn't have mad the DIRECTION object a constant. I guess constants are only legal in the main class file. SO I changed it to a protected variable and that solved the problem.

protected var DIRECTIONAL:Number = -1

The next error shouldn't have occurred in the first place because it was simply stand-in code for the next evolution of the Acceleration/Deceleration franchise which seems light-years away from where I am now. But the issue was that I was using && to switch the RedPressed boolean to false when && is only to be used for checking, not for changing, variables. I simply deleted the symbols and the error went away.

from:
if (BluePressed) {BlueRight.stop(); && BluePressed = false}

to:
if (BluePressed) {BlueRight.stop(); BluePressed = false}

I am finally left with one error on each actionscript file. It reads as follows:


C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\OrangeUp.as, Line 21 1084: Syntax error: expecting leftparen before isNaN.


The solution was much simpler than I had originally anticipated.

if (isNaN(value)){throw new Error ("Pivotal Variable is of the wrong type.") 

I'm happy that two out of these three errors-and many of the others before- actually occurred on parts of code which were originally intended for the more advanced program iterations which will be based on this framework.

Using Classes

To make a long story short, I went through a whole lot of mess to get the errors off of the main Jump.as document and nothing I tried worked until now. This is the final error-free version of the Jump.as file.


package body  {

import flash.display.MovieClip

public class Jump extends MovieClip {
   public var STEP_AMOUNT:Number = 60
private var RedPressed:Boolean =  false
private var BluePressed:Boolean = false
private var YellowPressed:Boolean = false
private var OrangePressed:Boolean = false
}


Red.addEventListener(MouseEvent.CLICK, RedLeft);
Red.SetStepAmount(STEP_AMOUNT)


Blue.addEventListener(MouseEvent.CLICK, BlueRight);
Blue.SetStepAmount(STEP_AMOUNT);


Yellow.addEventListener(MouseEvent.CLICK, YellowDown);
Yellow.SetStepAmount(STEP_AMOUNT);


Orange.addEventListener(MouseEvent.CLICK, OrangeUp);
Orange.SetStepAmount(STEP_AMOUNT);
}

And now that those errors are taken care of I can worry about THESE:

C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\YellowDown.as, Line 6 1071: Syntax error: expected a definition keyword (such as function) after attribute private, not static.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\YellowDown.as, Line 8 1084: Syntax error: expecting rightbrace before semicolon.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\YellowDown.as, Line 21 1084: Syntax error: expecting leftparen before isNaN.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\OrangeUp.as, Line 6 1071: Syntax error: expected a definition keyword (such as function) after attribute private, not static.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\OrangeUp.as, Line 8 1084: Syntax error: expecting rightbrace before semicolon.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\OrangeUp.as, Line 21 1084: Syntax error: expecting leftparen before isNaN.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\BlueRight.as, Line 6 1071: Syntax error: expected a definition keyword (such as function) after attribute private, not static.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\BlueRight.as, Line 8 1084: Syntax error: expecting rightbrace before semicolon.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\BlueRight.as, Line 21 1084: Syntax error: expecting leftparen before isNaN.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\RedLeft.as, Line 6 1071: Syntax error: expected a definition keyword (such as function) after attribute private, not static.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\RedLeft.as, Line 8 1084: Syntax error: expecting rightbrace before semicolon.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\RedLeft.as, Line 21 1084: Syntax error: expecting leftparen before isNaN.



26.1.11

Using Classes

I ran the program and got these two errors.


C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\Jump.as, Line 4 1071: Syntax error: expected a definition keyword (such as function) after attribute protected, not static.


C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\Jump.as, Line 15 1084: Syntax error: expecting rightbrace before leftbrace.

I deleted the word static and added a right brace on line fourteen and this is what I got.

C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\Jump.as, Line 4 1071: Syntax error: expected a definition keyword (such as function) after attribute protected, not constant.

For this, I switched to a public constant and got the same error. I've decided to add a function, maybe that will solve the problem. 

internal function Jump(); {
 
startRed():void
startBlue():void
startYellow():void
startOrange():void
}

That didn't solve the problem, so I am goung back. Now the constant is a variable.

Ok, I've gotten rid of that error, and now I have these further down.


C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\Jump.as, Line 12 1078: Label must be a simple identifier.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\Jump.as, Line 13 1078: Label must be a simple identifier.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\Jump.as, Line 14 1078: Label must be a simple identifier.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\Jump.as, Line 15 1078: Label must be a simple identifier.
C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\Jump.as, Line 18 1084: Syntax error: expecting identifier before rightbrace.


I've declared the functions:

public function startRed():void
public function startBlue():void
public function startYellow():void
public function startOrange():void

And...


Awesomely, I ended with just one error at frame 39.


C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\Jump.as, Line 39 1087: Syntax error: extra characters found after end of program.

I delete the extra hanging rightbrace, and...

C:\Users\Owner\Documents\Flash Projects\FlashBlog\Jump\SwitchToExternal\body\Jump.as, Line 1 5006: An ActionScript file can not have more than one externally visible definition: body.Jump, body.startRed


I'll switch the four starts to internal, and...