1

Hey everyone So I have created a grid on stage using the code below. Everything works fine and the grid is created. Only problem is when i change the columns and rows value to a higher value the grids position changed offset to the center of the stage. I want it to be when i change the values and more columns etc are added the Grid stays dead center.

Also it seems that the grid expands to the positive x axis from the top left hand corner of the grid.

What can I do to fix this?

public function geniusMatchEngine() 
    {
        //Instantiate Variables
        nSquares = 0;
        //Movie clips
        _gridContainer = new MovieClip();

        stage.addChild(_gridContainer);

        createGrid();

        //Add Listeners
        stage.addEventListener(Event.ENTER_FRAME, logicHandler);
    }

    private function logicHandler(e:Event):void 
    {

    }

    private function createGrid():void 
    {
        for (var gy:int = 0; gy < rows; gy++)
        {
            for (var gx:int = 0; gx < columns; gx ++)
            {
                var _grid:mcGrid = new mcGrid();
                _grid.x = (stage.stageWidth / 2) + _grid.width * gx;
                _grid.y = (stage.stageHeight / 2) + _grid.height * gy;
                _gridContainer.addChild(_grid);
                _grid.addEventListener(MouseEvent.CLICK, tapGrid);
            }
        }
    }

    private function tapGrid(e:MouseEvent):void 
    {
        trace("CLICKED");
        e.currentTarget.destroy(); //Destroy current target
    }

    private function centerGrid(gCenter:MovieClip):void
    {
        gCenter.x = (stage.width / 2) - (gCenter.width / 2); // center horizontally. 
        gCenter.y = (stage.height / 2) - (gCenter.height / 2); // center vertically. 
    }
Nathan
  • 536
  • 4
  • 21

1 Answers1

2

To center something on the stage do:

private function centerThis(s:<Type>):void{
    s.x = stage.width / 2 - s.width / 2; // center horizontally. 
    s.y = stage.height / 2 - s.height / 2; // center vertically. 
}

Then with your grid you can do:

centerThis(_grid);

Note: make sure to replace <Type> with whatever makes sense. In this case it could be mcGrid but to make it more generic so you can use this function on any movie clip or Sprite use MovieClip or Sprite.

update

Since you seem a little confused about scope, I'll use your specific case as an example.

//Variables
    private var rows:int = 2;
    private var columns:int = 2;

for (var gy:int = 0; gy < rows; gy++)
        {
            for (var gx:int = 0; gx < columns; gx ++)
            {
                var _grid:mcGrid = new mcGrid();
                _grid.x = (stage.stageWidth / 2) - _grid.width / 2;
                _grid.y = (stage.stageHeight / 2) - _grid.height / 2;
                stage.addChild(_grid);
            }
        }

I thought the _mcGrid was a prefabricated grid. I'm now seeing that it is probably just one cell in the grid (maybe change the class name to _mcGridCell... less confusing). Forget trying to place the cells relative to the stage center. Add them to a parent Sprite (or MovieClip if you prefer) and then center that Sprite (movieClip) with my centerThis formula. This is the beauty of display object containers.

update

Ok. When you center the grid container, everything in the container goes with it. So no need to also try to center the cells in the grid as you have done.

grid.x = (stage.stageWidth / 2) + _grid.width * gx;

should just be

grid.x = _grid.width * gx;
Neal Davis
  • 2,010
  • 2
  • 12
  • 25
  • Okay so I have set everything up but nothing is happening. I added a mouse Event Click with the function centerThis(_grid); inside of it but when I click on the stage it doesnt center the grid. I create my grid inside my constructor if that helps;. – Nathan Mar 03 '17 at 01:36
  • Oh yeah of course you can't use _grid outside of where you declare that variable. You need to either make that _gris variable have a more global scope or put the call to centerThis() inside the function where that variable is declared. – Neal Davis Mar 03 '17 at 02:08
  • Hey Neal yeah I understand that. Since _grid is not global. Alright well thanks for all the help still a bit confused. Maybe I am missing something simple, like always haha. I tried adding the centerThis(_grid); inside the the function but just ended up scattering everything. Sorry new to this whole grid thing. – Nathan Mar 03 '17 at 17:37
  • Well I probably need to see how you are making the grid then. Or is that function what makes the grid? I assumed the mcGrid was an already made grid... is mcGrid just one cell of the grid? If so I need to adjust my answer. – Neal Davis Mar 03 '17 at 17:38
  • Oh yeah mcGrid is just one cell. I have a Movieclip in my Library that I created with a height of 50px and a width of 50px. I use the double for loop to expand the squares to make the grid. Sorry should have added that. – Nathan Mar 03 '17 at 17:42
  • See my new update. Should for fix the problem. It's all about display object containers. – Neal Davis Mar 03 '17 at 17:44
  • Oh okay I understand now. I had that initial thought but thought I could accomplish it how I had it before just missing something. I updated the my question with how I have the container. Think you can check it out and see if I am doing it correctly? Thanks man. – Nathan Mar 03 '17 at 18:10
  • 1
    You're getting close. Your only problem is that you are putting the cell.x to the right still. It should be placed at 0,0. I'll post an example in a second. – Neal Davis Mar 03 '17 at 21:42
  • okay I understand/ So would I do the same thing to the y axis as well or just the x? – Nathan Mar 03 '17 at 23:06
  • Yes do the same thing with the y axis. – Neal Davis Mar 03 '17 at 23:07