30.11.10

Package and Import

First, a little history. When I started out programming in Actionscript, I would copy the code straight out of the book and put it into the actions panel. At the top of the code that I would copy were the words "Package" and "Import". Every time I would copy the code as-is I would receive an error (I will reproduce the error in a future post). I searched for the solution and it led me to download the Flex SDK. What I didn't know at the time is that the Flex SDK and Flex builder are already built into Flash CS5. The only way to use the package and import functions is to create an external Actionscript 3.0 class (.as) file and link it to the program. I had no idea how to do this, but learned eventually.

Package. The purpose of this tool is to group all of the local Actionscript 3.0 class files together so that they can share information with one another.

Import. The purpose of this tool is to import class files or folders full of class files for use in the program.

29.11.10

Switching to Classes

I just tested these name changes in the original Jump program and they worked, so that part is done. I remembered to change then name both in the event listener and at the function.

Red.addEventListener(MouseEvent.CLICK, RedLeft);

function RedLeft(event:MouseEvent):void
{
Green.x -= 60;
}

Blue.addEventListener(MouseEvent.CLICK, BlueRight);

function BlueRight(event:MouseEvent):void
{
Green.x +=60;
}


Yellow.addEventListener(MouseEvent.CLICK, YellowDown);

function YellowDown(event:MouseEvent):void
{
Green.y +=60;
}

Orange.addEventListener(MouseEvent.CLICK, OrangeUp);

function OrangeUp(event:MouseEvent):void
{
Green.y -= 60;
}

Now there won't be any confusion at all when I go to connect the two with the functions that will be contained in the external class files. Which reminds me, I will have to re-create the class files for this project so that they match the new class names. 

Switching to Classes

Actually, there is another issue that I have to sort out before I switch to using separate class files. And that is the naming conventions for each function.

Take a look at this:


Red.addEventListener(MouseEvent.CLICK, RedJump);

function RedJump(event:MouseEvent):void
{
Green.x -= 60;
}

Blue.addEventListener(MouseEvent.CLICK, BlueJump);

function BlueJump(event:MouseEvent):void
{
Green.x +=60;
}

Yellow.addEventListener(MouseEvent.CLICK, YellowJump);

function YellowJump(event:MouseEvent):void
{
Green.y +=60;
}

Orange.addEventListener(MouseEvent.CLICK, OrangeJump);

function OrangeJump(event:MouseEvent):void
{
Green.y -= 60;
}

The issue that I want to fix now is that each class file is named MoveLeft, MoveRight, MoveUp and MoveDown. When I started out, it seemed ok to simply match up the Left, Right, Up and Down with its respective color, but now I want to fix this because I don't want this problem haunting me forever (I want to get in the habit of fixing these issues early on in the process). 

28.11.10

Switching to Classes

I have no idea how to do this right, so I'm just going to go at it. I will start by taking the code for each of the buttons in the original Jump program and placing them into the individual AS3.0 files.

This is the code from the original Jump program:



Red.addEventListener(MouseEvent.CLICK, RedJump);


function RedJump(event:MouseEvent):void
{
Green.x -= 60;
}


Blue.addEventListener(MouseEvent.CLICK, BlueJump);


function BlueJump(event:MouseEvent):void
{
Green.x +=60;
}


Yellow.addEventListener(MouseEvent.CLICK, YellowJump);


function YellowJump(event:MouseEvent):void
{
Green.y +=60;
}


Orange.addEventListener(MouseEvent.CLICK, OrangeJump);


function OrangeJump(event:MouseEvent):void
{
Green.y -= 60;
}

In my new folder for the project, I have created five .as files which will house the code: MoveLeft, MoveRight, MoveUp and MoveDown for each of the buttons, and Jump for the main body of the program.

25.11.10

Switching to classes

I am getting ready to start a huge project in Actionscript 3.0 and it's very risky. Up until now I have been adding all of my Actionscript 3.0 code exclusively in the Actions panel, and now I am going to switch to using external class files for all of my coding. The reason for this change is that using external class files will keep my code a lot better organized, and it will allow me to take advantage of the constants and the protected and private variables, which are forbidden in the Actions panel. I think that I will start with the Jump program and move forward. For right now, I will simply create the class files.

18.11.10

Just for Fun

On Novemeber 4 th I posted my attempt to switch from event-driven listeners in the Jump program that I created to timer-driven programs. When I first started adding in code, I recieved errors for undefined property, timer. The issue was that I forgot to add the code at the top to instantiate the variables and import the necessary objects. This is what was missing at the top of my last program.

import flash.utils.Timer;
import flash.events.TimerEvent;
var timer:Timer;

So, I added this code in at the top of the Actionscript section and the progam ran but when I clicked the button, nothing happend- at times a welcome reprieve from yet another jarring error. The problem was that I had forgotten to add the timer.start() function at the end of the Timer Event body. This is what the entire code looked like for the red button.

Red.addEventListener(MouseEvent.CLICK, RedJump);
function RedJump(event:MouseEvent):void
{

 timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, redTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, redTimerDone);
timer.start()
}
function redTimerStep(event:TimerEvent):void {
Green.x -= 8;
}
function redTimerDone(event:TimerEvent):void {
Green.x += 240;
}

And it worked, so I immediately copied this style for the other buttons in the program, thus and thus.

Blue.addEventListener(MouseEvent.CLICK, BlueJump);
function BlueJump(event:MouseEvent):void
{
  timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, blueTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, blueTimerDone);
timer.start()
}
function blueTimerStep(event:TimerEvent):void {
Green.x += 8;
}
function blueTimerDone(event:TimerEvent):void {
Green.x -= 240;
}


Yellow.addEventListener(MouseEvent.CLICK, YellowJump);
function YellowJump(event:MouseEvent):void
{
 timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, yellowTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, yellowTimerDone);
timer.start()
}
function yellowTimerStep(event:TimerEvent):void {
Green.y += 8;
}
function yellowTimerDone(event:TimerEvent):void {
Green.y -= 240;
}


Orange.addEventListener(MouseEvent.CLICK, OrangeJump);
function OrangeJump(event:MouseEvent):void
{
 timer= new Timer(.01*1000, 30);
timer.addEventListener(TimerEvent.TIMER, orangeTimerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, orangeTimerDone);
timer.start()
}
function orangeTimerStep(event:TimerEvent):void {
Green.y -= 8;
}
function orangeTimerDone(event:TimerEvent):void {
Green.y += 240;
}
Victory was mine that day. I would have reported it if I had incurred any of those pesky misspelling errors, but I'm not making those mistakes as often anymore.

7.11.10

Problems with the Circle

I didn't just want to copy the circle- I wanted to understand the circle. So when I finally happened upon the formula for a circle (x sq + y sq = 1), to say the least, I was absolutely confused. "So what you're saying to me is that the square of x plus the square of y is always equal to one?" "I guess the one must stand for ' one circle'." Luckily, I was wrong. The true equation for a circle is x sq + y sq = radius sq. The other equation was more of a template. The assumption was that the radius would always be at least one, which is not always true.

4.11.10

Just for fun

Just for fun I decided to change up the jump program to work with event listeners, starting on the first of November. The final product is at this link. This version is different because instead of the green square being moved one interval and then stopping- based on the MouseEvent framework, the green square will be moved at multiple intervals based on Timers and TimerEvents.


Again here is the code that I am going to convert. It came from my Jump experiment. 
Red.addEventListener(MouseEvent.CLICK, RedJump);
function RedJump(event:MouseEvent):void


{
Green.x -= 60;
}
Blue.addEventListener(MouseEvent.CLICK, BlueJump);
function BlueJump(event:MouseEvent):void
{
Green.x +=60;
}
Yellow.addEventListener(MouseEvent.CLICK, YellowJump);
function YellowJump(event:MouseEvent):void
{
Green.y +=60;
}
Orange.addEventListener(MouseEvent.CLICK, OrangeJump);
function OrangeJump(event:MouseEvent):void
{
        Green.y -= 60;
}



I found the code for timers and timer events in the book and attempted to quickly retro-fit it for this operation. 


{
timer= new Timer(.4*1000, 3);
timer.addEventListener(TimerEvent.TIMER, timerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
}
function timerStep(event:TimerEvent):void {
Green.x -= 20;
}
function timerDone(event:TimerEvent):void {
Green.x += 60;
}


I used the Timer and TimerEvent functions, starting with the red button only. The idea was to create a timer object that would count a certain number of milliseconds- in this case, four hundred- and then move the object a set distance- in this case, 20 pixels. I couldn't figure out a good use for the TIMER_COMPLETE event listener so I made it so that when the TIMER_COMPLETE event listener was clicked, the object would be sent back exactly the amount of pixels that it was moved.  


This is the code that I tried to implement:



{
timer= new Timer(.4*1000, 3);
timer.addEventListener(TimerEvent.TIMER, timerStep);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerDone);
}
function timerStep(event:TimerEvent):void {
Green.x -= 20;
}
function timerDone(event:TimerEvent):void {
Green.x += 60;
}



After implementing the code, I immediately received this error:



Scene 1, Layer 'Actions', Frame 1, Line 6 1120: Access of undefined property timer.
Scene 1, Layer 'Actions', Frame 1, Line 7 1120: Access of undefined property timer.
Scene 1, Layer 'Actions', Frame 1, Line 8 1120: Access of undefined property timer.