21.12.10

I need classes

... and here's why. I'm going to take one of the older scripts and make some changes to it.


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

var tDura:Number =.01*1000
var Reps:Number = 500
var tStep:Number = .5
var tDone:Number = Reps * tStep

The Push_Timer class actually has variables in it, so it will be more useful in this explanation. 
Let's say that I want to change all of these from standard variables to private or protected. 

protected var tDura:Number =.01*1000
protected var Reps:Number = 500
protected var tStep:Number = .5
protected var tDone:Number = Reps * tStep

I attempt to run the program and receive these errors:

Scene 1, Layer 'Actions', Frame 1, Line 8 1150: The protected attribute can only be used on class property definitions.
Scene 1, Layer 'Actions', Frame 1, Line 7 1150: The protected attribute can only be used on class property definitions.
Scene 1, Layer 'Actions', Frame 1, Line 6 1150: The protected attribute can only be used on class property definitions.
Scene 1, Layer 'Actions', Frame 1, Line 5 1150: The protected attribute can only be used on class property definitions.

Without consulting reference, I add a little class definition at the top.

public class paera extends MovieClip

And the resulting errors:

Scene 1, Layer 'Actions', Frame 1 1084: Syntax error: expecting rightbrace before end of program.
Scene 1, Layer 'Actions', Frame 1, Line 7 1084: Syntax error: expecting leftbrace before protected.
Scene 1, Layer 'Actions', Frame 1, Line 5 1114: The public attribute can only be used inside a package.

So then I add the braces and the package notation.

The resulting error

Scene 1, Layer 'Actions', Frame 1, Line 1 1037: Packages cannot be nested.


'Packages cannot be nested' means that I cannot place my package inside of the Actions panel of Flash CS5, and therefore I won't be able to use the advanced features unless I create an external file.

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.


31.10.10

Movement Interface

This is what the program looked like at first. I was moving so fast that I never bothered to label anything until later on. 


I here I added the labels. The "UP" and "DOWN" buttons are still reversed at this point.

Movement program

It was at this point that my lack of programming experience began to catch up with me... 



Button_1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);

function fl_MouseClickHandler_3(event:MouseEvent):void
{
Green.x += 30;
}


Button_2.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);

function fl_MouseClickHandler_4(event:MouseEvent):void
{
Green.x -= 30;
}

Yellow.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);

function fl_MouseClickHandler_3(event:MouseEvent):void
{
Green.y += 30;
}


Yellow.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);

function fl_MouseClickHandler_4(event:MouseEvent):void
{
Green.y -= 30;
}

After running the program I got this error:

Scene 1, Layer 'Actions', Frame 1, Line 24 1021: Duplicate function definition.

When I copied the event listeners I forgot to change the function name for the event handlers, so they were duplicated, which caused this error. Also, the red and blue buttons are labelled "Button_1" and "Button_2". I soon changed the names so that I wouldn't have to remember what color goes with which function.

Also, the names of the event listener targets for the yellow and orange buttons were duplicated, which resulted in both buttons being put out of commission.There was no resulting error message; both the yellow and orange buttons simply failed to function.

This is the code that worked:


button_1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_3);

function fl_MouseClickHandler_3(event:MouseEvent):void
{
Green.x += 30;
}


button_2.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_4);

function fl_MouseClickHandler_4(event:MouseEvent):void
{
Green.x -= 30;
}

Yellow.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_1);

function fl_MouseClickHandler_1(event:MouseEvent):void
{
Green.y += 30;
}


Orange.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_2);

function fl_MouseClickHandler_2(event:MouseEvent):void
{
Green.y -= 30;
}


18.10.10

Movement program


On September  29th I started  working on an  Actionscript  3.0 "Movement"  program. The link to the finished project is here. Adobe  Flash 5 comes  with certain  stock buttons  that I think  look awesome,  so I wanted to  combine that  with the code  snippets that  are also found  shipped with  the program.  The buttons  that I like so  much are found  in Window>  Common  Libraries>  Buttons>  Classic  Buttons>  Arcade Buttons  (At the top).

The code  snippet that used came  from Code  Snippets/  Event  Handlers/  Mouse Click  Event (On the little side panel). 

This is the  code entire  code snippet:

/* Mouse Click  Event
Clicking on  the specified  symbol  instance  executes a  function in  which you can  add your own  custom code.

Instructions:
1. Add your custom code on a new line after the line that says "// Start your custom code" below.
The code will execute when the symbol instance is clicked.
*/

button_1.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);

function fl_MouseClickHandler(event:MouseEvent):void
{
// Start your custom code
// This example code displays the words "Mouse clicked" in the Output panel.
trace("Red");
// End your custom code
}

I put the trace method in there because I was starting with the red button first and I wanted the output menu to trace "Red" instead of the more generic "Mouse clicked". When I got confirmation of the working program I combined that code snippet with another which was to move the object across the screen. 

Here is that one: 

instance_name_here.x -= 10;

And this is the part where I started cutting out all of the extra information given with the snippet. I would eventually make two movement buttons and then four: Left, Up, Right and then Down. Up and down were on the Y axis while left and right were on the X axis. 

instance_name_here.x += 10;


The script worked for the first left/right buttons on either side, so I moved on to the up/down buttons in the middle.

--------------------------------------------------------------------------------------------------------------------



instance_name_here.y -= 10;
instance_name_here.y += 10;




The idea is that every time the button is clicked, the position (X or Y) is increased by a pre-determined number.

13.10.10

Rough Sketch


This is a rough sketch for an upcoming project in Flash: How to use Sine, Cosine and Tangent functions to make a circle using Actionscript.

28.9.10

Sine

Here is another error I had when creating a function that utilize the Sine function in Actionscript 3.0.


function FindSin(Fin:Number):Number {
    var Truest1 = Fin * Math.sin;
    return Truest1;
}




trace(FindSin(115));


When I ran this code, I got a particular error message. 

Scene 1, Layer 'Layer 1', Frame 1, Line 2 1067: Implicit coercion of a value of type Function to an unrelated type Number.

When I read this error message, I over-thought the issue and immediately began doing things like this, thinking it was a syntax or convention issue and not a logic issue. 

function FindSin(Fin:Number):Number {
    var Truest1:Number = Fin * Math.sin;
    return Truest1;
}


trace(FindSin(115));

No solution there. It wasn't until I actually started writing this post that I realized what the error message really meant: "You are trying to turn a function into a number; stop doing that". I immediately started fidgeting with the FindSin function and it's parameter, thinking that it was the only function in the function, but it wasn't. A lot of you may not know this, but Math.sin is a function. I looked back into my Actionscript 3.0 book and saw the function Math.sin(a). I did something crazy like...

function FindSin(Fin:Number):Number {
    var Truest1:Number = Fin * Math.sin(a);
    return Truest1;
}
trace(FindSin(115));

... and got a nice error. Eventually, I realized that the parameter in the parenthesis carries the number which will be multiplied by the Sine function.

function FindSin(Fin:Number):Number {
    var Truest1:Number =  Math.sin(15);
    return Truest1;
}
trace(FindSin(115));

VICTORY


Actionscript is highly dynamic and powerful!

Sine, Cosine, and Tangent

I started hammering some functions yesterday and I managed to save some of them in my e-mail.
This one was a preliminary for a future project that I will be working on. One of the things that I love about Actionscript is that it's modular. If you notice, each of the Sine and Cosine scripts are placed one on top of the other. All of the trace commands are at the bottom. When you use trace, it only prints the result in the output window in Flash CS5 Professional it won't print in the actual animation. We'll get to that part in a little bit.

function FindSin(Fin:Number):Number {
    var Truest1 = Math.sin(Fin);
    return Truest1;
}


function Cosin(FinC:Number):Number {
    var Truest2 = Math.sin(FinC);
    return Truest2;
}



function Cosin2(FinC:Number):Number {
    var Truest1 = Math.sin(FinC);
    return Truest1;
}

function Tangent(FinT:Number):Number {
    var Truest1 = Math.sin(FinT);
    return Truest1;
}

trace(FindSin(115));
trace (FindSin(99));
trace (FindSin(999));
trace (Cosin(55));
trace (Cosin(300));
trace (Cosin2(55));
trace (Cosin2(300));
trace (Tangent(211));
trace (Tangent(37));
trace (Tangent(90));
trace (Tangent(123));
trace (Tangent(5));

If you notice, there are two Cosine functions. The reason why I put that there was to test the properties. The first Cosine function has a property named Truest2 and the second, Truest1. I had to test and see if the naming of the property in the second Cosine function was conflicting with the naming of the property in the Sine function at the top, which is also called Truest1. To my great relief, they were kept internal.

Now I'm going to show you some of the errors that I made.


function FindSin(Fin:Number):Number {
    var Truest1 = Fin * Math.sin(Fin);
    return Truest1;
}


trace(FindSin(115));

When I traced the result, it came out to this number. 

108.72506341284858

When I saw this I was like HOORAY! But something seemed wrong because when I went to an online calculator and entered the equation into a scientific calculator it came out to this number: .9454353340247703. I was like "Totally WTF"! But then, after a while of bumbling and fidgeting with the numbers, I divided. 108.75 by the a number slightly higher than  0.95 and came up with a number that was very close to the number 115. The area where I messed up was here:

var Truest1 = Fin * Math.sin(Fin); 

When I placed Fin in the parenthesis at the right of Math.sin, I bad absolutely no idea that Math.sin was going to multiply itself by Fin AGAIN. I thought that this was just an Aristocratic do-nothing convention, but it turned out to be FUNCTIONAL! So there you have it; I multiplied Math.sin by Fin TWICE


Regular Expressions

I got down to business again yesterday and I began making scripts in Actionscript 3.0. Here is the script that gave me the error:

var searchForCorn:RegExp = /corn/gi;
 var themTharHills:String = "hillscornhillshillsCornhills";

var result:Object = searchForCorn.exec(themTharHills);

if (result) trace ("There's corn in them thar hills!");

trace(result.index);

result = searchForCorn.exec(themtharHills);

trace(result.index)

result = searchForCorn.exec(themTharHills);

if (result) trace ("There's corn in them thar hills!")

else

trace ("nope")
------------------------------------------------------------------------
-----------------------------
When I ran this script I got an error message, which read:

Scene 1, Layer 'Layer 1', Frame 1, Line 10 1120: Access of undefined property themtharHills.
Don't be alarmed, my fellow Flash CS5 and Actionscript 3.0 users. When you see this error message, the first thing you need to do is check the name- of the property in question of course. You probably just misspelled the property name when you used it again in subsequent sections of the program. Search the code and see if you can find the inconsistency.

Here is the script which has been corrected and should work now.


var searchForCorn:RegExp = /corn/gi;
var themTharHills:String = "hillscornhillshillsCornhills";
var result:Object = searchForCorn.exec(themTharHills);
if (result) trace ("There's corn in them thar hills!");
trace(result.index);
result = searchForCorn.exec(themTharHills);
trace(result.index);
result = searchForCorn.exec(themTharHills);
if (result) trace ("There's corn in them thar hills!");
else 
trace ("nope")

John Jenkins here

Greetings to all. My name is John Jenkins and I am a proud owner of Adobe Flash Professional CS5. I remember going to design school and using Adobe Flash for the first time. "This is the greatest computer program ever made", I thought. Then, as in many of my endeavors as a designer, I became furious when I discovered that making stuff look as awesome as my favorite designers do is really, really, really, really, really difficult. When I finally took the risk and spent my entire check on the $742.69 design program I had high hopes, which were quickly weighed down. Making awesome stuff in flash is already hard enough, but I soon discovered the horrors of facing one of my greatest fears: trying to become a programmer.

Up until the day that I started programming and Actionscript 3.0, my only experiences in programming came from a stint in computer camp where I dabbled in Visual Basic; afterwards I purchased a Visual Basic book and CDrom; taking a course in Programming Logic; making a few odd script for an FPS for which I enjoyed to make maps, not mods (Operation Flashpoint: Cold War Crisis); and all of this was preceded by my amateur foray into HTML, which I had all but completely given up by the time I was a sophomore in college. "This is not going to be another HTML, or even worse, CSS", I told myself as I began hoarding books on programming and digital design from my local Borders store. Adobe Flash CS5  Professional was going to be my program of choice and I have been determined since to make it work for me.

I have been hitting big giant snags everywhere. The problem: I want everything I do to look like coolzies in five minutes. I hate circles, I hate squares, and I hate fills. I want to be able to go into the options menu of flash and grab the cool tool and just paint cool or an ultra cool all over the screen without so much as a bother. The bother about that is that I don't have the skills necessary to do that as of yet. Of course, I won't listen to you if you tell me this; just as I ignore myself as I tell myself this every day.

Oh and by the way; I hate code snippets and I hate reading the instruction manual that came with the program and I have never gone back to the web site to take advantage of the privileges of being a full owner of Adobe Flash CS5 Professional. I want every script I create to confound Asimo.