Groups | Blog | Home
all groups > flash actionscript > february 2004 >

flash actionscript : Problem with Custom Class


Ticean Bennett
2/8/2004 3:19:55 PM
Hi, I'm having some problems using a simple custom class I've created, maybe
somebody can help me out.

Here is the class definition I've created:

_______
class ChipColor {

var colorName:String;
var colorValue:Color;

// Method to return property values
function getName():String {
return(colorName);
}

function getColor():Color {
return(colorValue);
}

//Constructor Function
function myColor (myName:String, myValue:Color){
colorName = myName;
colorValue = myValue;
}

}
_________

I create an instance using:

var myColor0:ChipColor = new ChipColor("Red",0xd90000);

and then try tracing a property:

thisColor = myColor0.getColor();
trace(thisColor);


I always get "undefined". This should be easy right? I'm getting no
compile errors. Can somebody please lead me in the right direction?

thanks,

Ticean Bennett

monsterdog
2/9/2004 2:58:06 PM

You have a couple of problems. First, your constructor needs to have the
same name as the class declaration. Also, if you declare a variable as
Color, the AS compiler thinks that you meant the built in Color object,
which I assume is not what you want. So you also need to change your Color
declarations to Number.

class ChipColor
{
:
var colorValue:Number;
:

function ChipColor(myName:String, myValue:Number)
{
:
}

function getColor():Number
{
return colorValue;
}
}

Hope this helps.
woody

[quoted text, click to view]

Stef
2/12/2004 11:19:36 AM
Hi,

I don't know Flash MX2004 but I do know OOP.


The problem is that in your constructor function, your 2nd argument needs an
object of the type Color:

function myColor (myName:String, myValue:Color){
....

But you are not passing an OBJECT of the type color:

var myColor0:ChipColor = new ChipColor("Red",0xd90000);

0xd90000 -> is not a Color object, its just a color value. It would be a
string if your text had been in quotes, but it is not, so I don't know what
Flash will try to do with it ...?

To solve your problem change one of the two :

1. Pass a true color object when creating the object

OR

2. Change your constructor to accept and string and then inside the
constructor convert that string to true color object and use the arguments
string value (in this case: 0xd90000) as the argument when creating the
color object:


//Constructor Function
function myColor (myName:String, myValue:String){
colorName = myName;

//create color object here ...
colorValue = myValue;
}

I never used MX before, so I don't know what the color object constructor
is,otherwise I would show you ...:)

I think I may jump into MX soon though.

Stefan

www.killersites.com
www.how-to-build-websites.com





[quoted text, click to view]

AddThis Social Bookmark Button