?
Solved

AS2.0 strict typing and one heck of a strange data type conversion

Posted on 2007-10-07
7
Medium Priority
?
166 Views
Last Modified: 2010-04-03
Hi there, more AS2 questions (man, I just wish I could use AS3!)

In this class I'm working with I define the following instance variable:

var scaleParams:Array;

later on, I assign some values to it (well, actually, I only assign one value for now, but I want an array because I suspect I'll be adding additional parameters. Bear with me here).

this.scaleParams = new Array (myNumber);

and then a little later on I call a method using scaleParams as the arguments:
this.scaleRandom(this.scaleParams);

and here's the scaleRandom() method:

function scaleRandom(theNumber:Number){
  trace (typeof theNumber);
}

and guess what, the trace returns "object", not "number"!!

This is actually significant, because I'm trying to use theNumber in a mx.transitions.Tween call and the call fails! If I replace the variable theNumber with a number literal, the Tween works. When I use the variable, the Tween doesn't work at all.

If I do a trace(theNumber) I actually get the value I'm looking for (which is bizarre). But when I trace (typeof theNumber) I get "object", not "number" (sorry, I'm repeating myself).

My questions for you:
- why is this theNumber local variable being converted from a Number to an Object?
- why is the strict typing that I'm using in the scaleRandom(theNumber:Number){ } function definition not catching this and generating an error? I thought that was the whole point of strict typing

Thanks for your thoughtful reply.

Tom
0
Comment
Question by:tomaugerdotcom
  • 4
  • 3
7 Comments
 
LVL 14

Expert Comment

by:trigger-happy
ID: 20031377
1.) typeof really returns an object, flash uses it to differentiate the data types within actionscript.
2.) Despite the clear ability to define strict datatypes, flash actionscript 2 is still very lenient with the data types. If you want something better, you could either use AS 3 or use MTASC.

--trigger-happy
0
 
LVL 14

Author Comment

by:tomaugerdotcom
ID: 20033160
Fair enough. But typeof returns the primitive data type. So if you've declared a variable of type number, normally typeof returns Number. If you declare a variable type String, typeof returns String.

After further experimentation it is really looking like it's the way I'm passing the arguments through the function.

Here's a further example of what I'm trying to accomplish:

var myParams:Array = new Array (param1, param2, param3);

doMyFunction(myParams);

function doMyFunction(p1:Number, p2:Number, p3:Number){
   trace (typeof p1);
}

//
if you call doMyFunction normally using number literals eg: doMyFunction(5,6,7), typeof p1 returns Number as it should

if you call doMyFunction, passing it the array containing three numbers, you get Object.
0
 
LVL 14

Expert Comment

by:trigger-happy
ID: 20033197
Anything stored into an array is turned into an object irregardless of type. This is why AS2 is bad with data types.

--trigger-happy
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Author Comment

by:tomaugerdotcom
ID: 20033312
Sorry, but I disagree:

var a:Array = new Array ("a", "b", "c");
trace (typeof a[0]); // returns "String", not "Object"

Or did I misunderstand your last post?
0
 
LVL 14

Accepted Solution

by:
trigger-happy earned 2000 total points
ID: 20034113
My mistake, here's what's really happening. When you passed the array into the function, the function evaluated the array itself but when you started referring to the array elements, that's when the actual data types began to show. Flash doesn't really check how many items you pass into a function so instead of passing 3 arguments through the array, you passed 1 argument, which is the array itself.

--trigger-happy
0
 
LVL 14

Author Comment

by:tomaugerdotcom
ID: 20034527
Thanks trigger. That's a good explanation. Looks like I should be passing an object, not an array, eh?

Tom
0
 
LVL 14

Expert Comment

by:trigger-happy
ID: 20037340
yep, unless you really intend to pass an array.

--trigger-happy
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question