PDA

View Full Version : return class help



knickers95
April 14th, 2008, 01:59 PM
im converting a load of my old AS2 classes into AS3 and im having problems with this one class i have. It is a class full of custom math functions, and all they do is they use the numbers given to them, and return a new value. simple. And it worked fine in AS2. but for some reason AS3 doesnt like it.

This is basically what ive done:

package
{
public class Math2
{
public function randRange(l:Number, h:Number):Number
{
return Math.round(l + (Math.random() * (h - l)));
}
}
}

this function returns a random number between the two given values.

and in the AS3 .fla, i put this into the key frame:


import Math2;

trace(Math2.randRange(1, 10));

But i keep getting an error message that says:

"1195: Attempted access of inaccessible method randRange through a reference with static type Class."

And i dont know why it doesnt work.

Many thanks in advanced :D

ecptavares
April 14th, 2008, 02:15 PM
Hi!
here is what I´ve done and it worked fine!

My .as file Math2.as



package
{
public class Math2
{

public function randRange(l:Number, h:Number):Number
{
return Math.round(l + (Math.random() * (h - l)));
}
}
}



my test.fla file



import Math2

var s : Math2 = new Math2();
trace(s.randRange(10,20));



take a look and see if it is wht you want!

hobbbz
April 14th, 2008, 02:37 PM
you need to make the randRange function static, just like the original Math classes functions are all static. Otherwise you have to do it like the prev. poster said.

knickers95
April 16th, 2008, 06:47 AM
thanks guys!

it works fine now.

Just wondering, what does static mean/do?

Felixz
April 16th, 2008, 10:30 AM
static means that variable, constant, function is attached to that class. When u do not set static keyword var, const, func is separete for each instance

knickers95
April 16th, 2008, 01:02 PM
thanks for clearing that up felixz