Search code examples
actionscript-3flashpositiontilelist

AS3 Change position of object in TileList


I am using a tileList in ActionScript 3 to display movieclips. However I have the problem that not all reference points of the movieclips are in the correct place. This leads to that these movieclips are shown partly outside of their cell in the tileList.

I have tried to adjust the x and y position of the movieClip before adding it to the tileList, but this did not change anything. Now I have tried to find if it is possible to change the x and y position of an object already in the tileList, but without finding any answers.

I hope that I have made my problem clear. Thanks in advance!

EDIT:

This is the code I tried:

private function initTileList():void {
    for(var i:int = 0; i < _movieClips.length; i++) {
        changePos(_movieClips[i]);
        tileList.addItem({label: _movieClips[i].name, source: _movieClips[i]});
    }
}

private function changePos(mc:MovieClip):void {
    if(MovieClip(mc).getRect(mc).x != 0) {
        mc.x -= MovieClip(mc).getRect(stateMachineRef).x;
}
    if(MovieClip(mc).getRect(mc).y != 0) {
        mc.y -= MovieClip(mc).getRect(stateMachineRef).y;
    }
}

I do not have any errors, it just doesn't affect the position of the object in the tileList.

Example of how the problem looks.


Solution

  • Hard to say where's the problem without knowing these things:
    1. What tileList.AddItem() does exactly;
    2. What is stateMachineRef
    3. How MovieClips are loaded. If they are loaded from a network, that'll be a whole different story.

    By the way, you don't have to cast MovieClip(mc) as mc is already a MovieClip. Also, there is no difference as to when you will correct the coordinates: before or after adding to the tileList. Should work either way.

    So, given that information on your problem is not complete, I would just suggest you insure the following steps:

    -We assume all tiles are displayed inside a tile container. It can be Stage or a MovieClip or any suitable DisplayObjectContainer, so let's call it just tileContainer from now on.

    -We assume all tiles are of the same width and height. If you are not sure, you should check it again.

    -We assume that each tile in the tileContainer is displayed at some regular grid coordinates. I.e. it conforms the following code:

    for (var pos_y:int = 0; pos_y < GRID_SIZE_Y; pos_y++) {
        for (var pos_x:int = 0; pos_x < GRID_SIZE_X; pos_x++) {
            var tile:Tile = getNextTile(); // just get a tile from somewhere
            tile.source.x = pos_x * TILE_WIDTH; // using your tile structure
            tile.source.y = pos_y * TILE_HEIGHT;
            tileContainer.addChild(tile.source);
        }
    }
    

    Now I see your problem that some tiles are created in a way that they have their source movieclip coordinates shifted from (0,0). So they will not align with the grid.

    What are you doing seems to be a proper way of aligning them but I don't know exactly what happens in your code so I'll just rewrite it:

    function changePos(mc:MovieClip) {
       var r:Rectangle = mc.getRect(mc);
       mc.x -= r.x; // note you don't need any if's
       mc.y -= r.y;
    }
    

    And in the above loop just add the changePos() AFTER setting the grid coordinates:

    tile.source.x = pos_x * TILE_WIDTH;
    tile.source.y = pos_y * TILE_HEIGHT;
    changePos(tile.source);
    tileContainer.addChild(tile.source);
    

    If you're following all these steps, that's basically all you need and it will work for sure.