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.