Question

Lets say we have 4 instances of SimpleButton Class. SimpleButton Class has the variable b:Boolean. When you click on button instance, its variable b:Boolean becomes true, all the rest buttons take false for their b:Boolean variable. How to achieve that? Any suggestions appreciated.

Was it helpful?

Solution

Every time an instance is creator, its constructor "registers" itself into a class static array. By pushing the instance into this static array variable, the variable then keeps track of all the instances that are created. The instances will have a public setter function of variable b. if the incoming Boolean value is true, it sets its private b property to true and then loops over all the other instances that are registered in the static array, and sets their b setter functions to false. If an incoming b Boolean value is false, then it just sets its b to false and maybe changes its display(whatever you want). This will achieve what you want so that whenever you set a instances's b setter function to true, it automatically changes all the others to false. You can adapt this to do different things to. For instance: have their be a maximum of 2 instances that can have a b value true. In you'r case, just make the listener function call the b setter function, or whatever.

Below is the code I wrote to show my explanation, follow the comments to understand:

package{
public class SimpleButton extends Sprite { // your class

    private static var instances:Array = new Array(); // your static class-wide instance tracker
    private var x_b:Boolean; // your private var of b (I am using OOP techniques here

    public function SimpleButton() {
        instances.push(this); // "register" this instance into the array
    }
    // ... (rest of class)
    public function set b(newB:Boolean):void { // setter function for b
        this.x_b = newB; //set its value
        if (this.x_b) { // if its true
            for (var i in instances) { //loop through all the other "registerd" instances
                if(instances[i]!=this){ //if its not this instance
                    SimpleButton(instances[i]).b = false; // then set its b value to false
                }
            }
        }else { // if its being set to false (either manualy from outside, or from the loop of another instance
            // If you want to do something when it is false
        }
    }
    public function get b():Boolean { //just a getter to match our setter
        return this.x_b;
    }
}
}

If I have answered you'r question then Great! But if not then please comment back and let me know. Thanks.

EDIT

there would be only one array for all the instance of SimpleButton. It is static for the class. And its not the references to the instances that take up memory, its the instances themselves. What i mean is if you have created 50 instances of SimpleButtons, to the computer their are 50 different objects to keep track of. Even if you create 1000 variables all pointing to the same instances, to the computer those variables are just references to the instances, not the instances themselves and so you would still have just 50 objects in memory. The same is true here. the array is just one array with lots of references to your various SimpleButtons. If you delete all references to an instance of a class, that instance is still their in the computer - you just can't access it anymore, it is only deleted when the "Garbage Collection" of the flash player runs, and deletes all objects with no references to them. This "collection" is the only way to delete things in flash player, making a variable equal to null does not delete it, it only deletes the reference to it. This way is very elegant and easy when dealing with the SimpleButtons from outside its class. All you do is create one and it automatically records itself - ready to change its b var when another is changed.

If you still have trouble understand the difference of references to objects, and the objects in memory themselves, then respond again and i'll find a good link to send you about it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top