PDA

View Full Version : AS 2.0 Class absurdity.


Ripple_in_Eternity
04-08-2009, 06:12 PM
So, I'm coding a fairly complicated and elaborate project, and along comes Mr. Coding Problem.

The short of this is, what I believe to be local variables in a class are acting like they're a class value, in that there's only one value per class. Essentially: what I like to call "sticky vars".

Well I've got a bad case of the sticky vars and it's getting itchy.


function HueGene (Set0 : Array, Set1 : Array)
{
for (var u = 0; u < 8; u ++)
{
this["C" + (u + 1)][0] = Set0 [u]
this["C" + (u + 1)][1] = Set1 [u]
}
}


This is part of my class definition function and what I believe to be the source of the error. Perhaps I'm defining it improperly, perhaps the flash gods dislike me. The arrays "C1" through "C8" are the cause of my frustration.

The problem is however, that the values for these arrays are generated randomly, (granted I am using the random() function, but changing it to Math.random() didn't change so much as a byte.) but several of the variables end up being the same value.

More troubling, is the fact that this stickiness is inconsistent, and I'm having quite the time trying to figure out where I made this rather annoying boo-boo.

Anyone with an idea as to how to fix this would be appreciated. The last time I coded myself into this corner, I abandoned the project, and would be sad to lose this game to the nether-regions of my hard drive.

magcius
04-09-2009, 03:55 PM
You didn't explain your problem...

However, I can find several:


HueGene? Try to name your methods so that they make sense. (Maybe I'm wrong and this is actually meaningful in the context of the code.)
No scope access modifier. Try following the warnings and using "public" or "private"
No return type. If you have no return like in this function, use "Void" as the return type.
There is a difference between a class and an instance. "static" class variables meaning that all instances share the exact same variable. If this is what you mean by sticky, you may want to make the variables static.
You shouldn't be using this and bracket syntax together, especially with static variable. Use a multidimensional Array.



class MyClass {
public static var status:Array = [];
public function setStatus (set0:Array, set1:Array):Void {
MyClass.status[0] = set0;
MyClass.status[1] = set1;
}
}


This code should work, but instead of using this.C4[0], use this.status[0][4]. Notice the reversal. If you need it the other way, use an array to put it the other way.

Ripple_in_Eternity
04-10-2009, 01:56 PM
Well, the class is a basic representation of one of the characters in my game, and I couldn't think of a better name, seeing as the game relies on colors and breeding.

Unfortunately, these cannot be static variables, as they will be changed in-game. It should also be noted that I want to AVOID having any static variables, or pseudo-static variables.

I suppose the idea of reversal is a good idea. Really, there's not too much preventing me from switching everything. I'll give it a shot. Thanks for the tips.

magcius
04-11-2009, 06:15 PM
static doesn't mean they can't change. It just means that there's one variable per type, as opposed to per instance.

Pazil
04-22-2009, 06:22 PM
I think what Ripple_in_Eternity means is that he wants each HueGene class to have it's own set of Array's, in which case it's as simple as doing:

class MyClass {
public var status:Array = [];
public function setStatus (set0:Array, set1:Array):Void {
this.status[0] = set0;
this.status[1] = set1;
}
}


And depending on the needs, you might want to cycle through the set arrays like you were doing originally, since right now you're only storing a pointer to the sets.

Ripple_in_Eternity
04-23-2009, 12:23 AM
Right, pointers. Forgive me if I have yet to learn about those. I know what they are, just not the implementation. I'd rather just refer to the instance variable if at all possible.

Pazil
04-23-2009, 12:52 PM
Well, refering to the instance variable is what a pointer does if I understand what you're asking...Flash automatically always works with pointers, unlike in C++, where you need to tell the program to make a pointer or copy the actual value. If you know what the sets will contain, I suggest writing a manual copy function that copy's the actual values from the objects/arrays/anything...