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...




Using Classes

After putting the main body in place, I proceeded to add the class files for each of the directional buttons, to give them functionality.


package body {
import flash.utils.Timer;

public class RedLeft {


private static constant DIRECTIONAL:Number = -1
private var _StepAmount:Number = 0
private var resetTimer:Timer = new Timer (250, 1);
resetTimer.addEventListener(TimerEvent.TIMER_COMPLETE, reset);

The DIRECTIONAL number is the directional coefficient for each direction, to let the program know what direction to go in on each axis. The purpose of the resetTimer will soon be revealed.


private function Jump():void {
if (BluePressed) {BlueRight.stop(); && BluePressed = false}
RedPressed = true
var lug:Number = DIRECTIONAL * StepAmount
Green.x += lug
}



I added the BlueRight.stop() to the private Jump function but I have no idea if it will work. This is a stand-in line of code for when I upgrade the Jump EX to work with the soon-to-be-made Push Ex and Accelerate/Decellerate EX. Lug is the final number of pixels that the object will move on the specified axis.


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


Either the internal part means that the function is accessible within the package or I messed up. I'm doing my first error check here as well. I want to make sure that I'm dealing with a number only.


internal function GetStepAmount(); 
{
return _StepAmount
}
public function RedLeft(event:MouseEvent):void
{
Jump(Axis, DIRECTIONAL);
}
internal function reset();
{
RedPressed = false
}


I will probably end up having to change GetStepAmount to GetRedStepAmount to disambiguate for the other class files that need it. Right below it the publick function RedLeft(event:MouseEvent):void, the bread and butter of the program. Finally, the reset function sets the boolean back to false.

Using Classes

I created the external class files, which are all in the 'body' package, and hooked everything up. Let's look at the main class file, shall we?


package body  {

public class Jump {
   protected static constant STEP_AMOUNT:Number = 60
public var RedPressed:Boolean =  false
public var BluePressed:Boolean = false
public var YellowPressed:Boolean = false
public var OrangePressed:Boolean = false

At the bottom I added four booleans. These should help me with verification and error checking in the future. The step amount is standard.


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


internal function startRed():void {
Red.addEventListener(MouseEvent.CLICK, RedLeft);
Red.SetStepAmount(STEP_AMOUNT)
}


internal function startBlue():void {
Blue.addEventListener(MouseEvent.CLICK, BlueRight);
Blue.SetStepAmount(STEP_AMOUNT);
}


internal function startYellow():void {
Yellow.addEventListener(MouseEvent.CLICK, YellowDown);
Yellow.SetStepAmount(STEP_AMOUNT);
}


internal function startOrange():void {
Orange.addEventListener(MouseEvent.CLICK, OrangeUp);
Orange.SetStepAmount(STEP_AMOUNT);


I'm feeling kind of nervous about this part. It seems like it might lead to some weird looping problems. The functions are internal because they don't need to be accessed by any other class or instance. Their purposes are two-fold. First to add event listeners to each of the buttons, and then to send the standard STEP_AMOUNT integer to each of the four buttons.