Question

The main goal of my code is to create a 3x3 grid and when you click a cell from that grid you cant click it again even if you close the fla and load it again. Something like a shop where the 1st row is level1 of the upgrade and the columns are the other levels.

enter image description here

There are also 2-3 other things that it does -> every cell of the grid has 4 mouseStates.

Also at the 1st load of the FLA you create the 3x3 grid and you can click only on the elements in the 1st row.(you cant get Speed 2 if you didnt have Speed1 before that.)

So you can click the 2nd element of a column only if the 1st element of the same column has been clicked before. The same goes for the 3rd element of the column -> it can be clicked only if the 2nd was clicked before.

But im having trouble with the logic after loading the fla for the 2nd time. To be more specific :

It is changing the mouseOver/out states on the elements that were clicked before(which is good (cause i want to see that)), but it is leting me click only the 1st row.And since Im loading the clickedBefore buttons and removing the mouseEvent.CLICK from them, I cant click some of them if i haven`t clicked them at the 1st load of the fla.

I have 2 classes: Main

import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.getDefinitionByName;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Graphics;
import flash.display.Bitmap;
import flash.display.SimpleButton;
import flash.net.SharedObject;

public class Main extends Sprite
{
    private var elementRow:int = 0;
    private var elementCol:int = 0;
    private var myClassImage_Arr:Array = new Array();//this contains the different mouseState Images in Class data.
    private var myBitmapNames_Arr:Array = ["speed1_", "speed2_", "speed3_", 
                                        "time1_", "time2_", "time3_",
                                        "turbo1_", "turbo2_", "turbo3_",];
    //------------------------------------------
    private var index:int = 0;
    private var col:int = 3;
    private var row:int = 3;
    //------------------------------------------
    private var savedData:SharedObject = SharedObject.getLocal("ZZZ_newWAY_nextButton+imageChange_7");
    private var buttonThatHaveBeenClicked_Arr:Array = [];
    private var myButtons_Arr:Array = [];


    public function Main():void
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }


    private function init(e:Event = null):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);

        for (var i:int = 0; i < col; i++)
        {
            var lastRowElement:BitmapButton = null;

            for (var j:int = 0; j < row; j++)
            {
                for (var k:int = 0; k < 4; k++)//4states of mouse
                {
                    var cls:Class = Class(getDefinitionByName(myBitmapNames_Arr[index] + k));
                    myClassImage_Arr.push(cls);
                }

                var myImage_mc = new BitmapButton(myClassImage_Arr[0 + (index * 4)],
                                                 myClassImage_Arr[1 + (index * 4)],
                                                 myClassImage_Arr[2 + (index * 4)],
                                                 myClassImage_Arr[3 + (index * 4)], i, j);

                myImage_mc.x = 100 + i * (myImage_mc.width + 10);
                myImage_mc.y = 100 + j * (myImage_mc.height + 10);

                myImage_mc.name = "myImage_mc" + index;
                this.addChild(myImage_mc);
                myButtons_Arr.push(myImage_mc)

                myImage_mc.mouseEnabled = false;
                myImage_mc.mouseChildren = false;
                myImage_mc.buttonMode = false;

                myImage_mc.addEventListener("send_SOS", onCustomClick);

                if ( lastRowElement == null )
                {
                    myImage_mc.mouseEnabled = true;
                    myImage_mc.mouseChildren = true;
                    myImage_mc.buttonMode = true;
                }
                else
                {
                    lastRowElement.next_1 = myImage_mc;
                }

                lastRowElement = myImage_mc;

                index++;
            }
        }

        if(savedData.data.myArray == undefined) trace("                               1st time loading this game\n")

        else if(savedData.data.myArray != undefined)
        {
            trace("                               Game was played before\n")

            buttonThatHaveBeenClicked_Arr = savedData.data.myArray;
            var savedData_length:int = savedData.data.myArray.length;

            trace("Buttons that have been clicked before:  "  + buttonThatHaveBeenClicked_Arr + "\n");

            for (var m:int = 0; m < myButtons_Arr.length; m++)
            {
                var myButtons_ArrName:String = myButtons_Arr[m].name

                for (var p:int = 0; p < savedData_length; p++)
                {
                    if(myButtons_ArrName == savedData.data.myArray[p])
                    {
                        myButtons_Arr[m].alpha = 0.9
                        myButtons_Arr[m].buttonMode = false;
                        myButtons_Arr[m].removeEventListener("send_SOS", onCustomClick);
                        myButtons_Arr[m].myInsideBtn.upState = myButtons_Arr[m].image3
                        myButtons_Arr[m].myInsideBtn.overState = myButtons_Arr[m].image4
                    }
                }
            }
        }
    }

    private function onCustomClick(ev:Event):void
    {
        trace(ev.target.name);

        if (ev.target is BitmapButton)
        {
            var btn:BitmapButton = ev.currentTarget as BitmapButton;

            if (btn.next_1 != null)
            {
                btn.next_1.mouseEnabled = true;
                btn.next_1.mouseChildren = true;
                btn.next_1.buttonMode = true;
            }
            btn.mouseChildren = false;
            btn.buttonMode = false;
            btn.removeEventListener("send_SOS", onCustomClick);

            buttonThatHaveBeenClicked_Arr.push( btn.name );
            savedData.data.myArray = buttonThatHaveBeenClicked_Arr;
            savedData.flush();
            savedData.close();
        }
    }
}

}

and BitmapButton

import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.events.Event;

public class BitmapButton extends Sprite
{
    public var next_1:BitmapButton = null;
    //-----------------------------------
    public var myInsideBtn:SimpleButton = new SimpleButton();

    private var image1:Bitmap;
    private var image2:Bitmap;
    public var image3:Bitmap;
    public var image4:Bitmap;

    public var imageIsInRow:int;
    public var imageIsInCol:int;

    public function BitmapButton(active_OutState:Class, active_OverState:Class, notActive_OutState:Class, notActive_OverState:Class,col:int,row:int)
    {
        image1 = new Bitmap (new active_OutState() );
        image2 = new Bitmap (new active_OverState() );
        image3 = new Bitmap (new notActive_OutState() );
        image4 = new Bitmap (new notActive_OverState() );

        imageIsInRow = row;
        imageIsInCol = col;

        myInsideBtn.upState = image1;
        myInsideBtn.overState = image2;
        myInsideBtn.downState = myInsideBtn.upState;
        myInsideBtn.hitTestState = myInsideBtn.overState;
        addChild( myInsideBtn );

        myInsideBtn.addEventListener(MouseEvent.CLICK, onClick);
    }

    private function onClick(ev:MouseEvent):void
    {
        myInsideBtn.upState = image3;
        myInsideBtn.overState = image4;

        var myNewEvent:Event = new Event("send_SOS");
        this.dispatchEvent(myNewEvent);
        trace("CLICK from inside the button");
    }
}

}

ill also upload it to this link Grid_with_sharedObject with a zip. and upload also Grod_before_Using_sharedObject if someone decides that he would help but the code is to messed up

Was it helpful?

Solution

If I'm reading your code correctly, I'd honestly say your problem is sequential. For whatever reason, the setting of the active and inactive rows is occurring BEFORE the data is actually being interpreted into the button states. As a result, the computer sees all buttons as off when it decides whether to make other rows clickable, and THEN updates the state of the buttons.

The easiest way to fix this, I think, would be to split the Main() function into a few sub functions, such as updateButtons() for the function that changes whether a row/button is clickable, and loadData() for the function the loads from the SharedObject. In Main(), put the calls to those functions. This will make Main() easier to work with, and you can call a function multiple times if necessary.

To solve your particular issue, you'd need to get the data for the buttons using the SharedObject FIRST (which obviously is working), and THEN update whether the other buttons are clickable.

A "soft-skills" tip for programming: when you run into a problem, grab a piece of paper, a pencil, and read through your code the way your computer would. Be the computer. Write down variables and their values when they change. Mark when functions are called. You'll spot a lot of errors this way.

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