Forums

OverviewV-Play 1 SupportDynamically changing the parent of a QML item › Re: Dynamically changing the parent of a QML item

OverviewV-Play 1 SupportDynamically changing the parent of a QML item › Re: Dynamically changing the parent of a QML item
#4090

Christian
V-Play Team

Quote from Christian on January 3, 2013, 21:21
Did you test if the row and column get modified correctly by adding this debug output for example:

onZChanged: console.debug("new z value for tile at", column, "/", row, ".z:", z) 

Yes that’s exactly what produced my output that I posted earlier.

console.debug( "tile at " + t.col + "/" + t.row + ".z: " + t.z)

I think what the problem is, that the items that get loaded from pool don’t get assigned the new parent item correctly, and now they are behind all the other items.

I remove the level items with entity manager:

entityManager.removeEntitiesByFilter(["game_item", "base_tile"])

Where “base_tile” is the entityType of the grid of tiles that should always be there, and “game_item” are items that can be placed on top of the tiles, like the player and the block you see in the screenshot.

Then i create a new level via

 function doLoadFromFile() {
        comp = Qt.createComponent(fileName);
        if (comp.status == Component.Ready)
            finishCreation();
        else
            comp.statusChanged.connect(finishCreation);
    }

    function finishCreation() {
        if (comp.status == Component.Ready) {
            item = comp.createObject(loader.parent, {});
            if (item == null) {
                console.log("Error creating object:", comp.errorString());
            } else {
                isLoaded = true
                loaded()
            }
        } else if (comp.status == Component.Error) {
            console.log("Error loading component:", comp.errorString());
        }
    }

And then in the loaded() signal handler i call a function which loads the tiles into the level:

    function load() {
        var test = ""
        for(var j = 0; j < rows; j++) {
            for(var i = 0; i < columns; i++) {
                var ind =i + j * columns
                var c = 0
                if(ind < tz.length && tz[ind] != ' ') {
                    c = tz[ind]
                }
                var  t = entityManager.getEntityById(
                                entityManager.createEntityFromComponentWithProperties(
                                    tileC, {index: ind, itemHeight: c, level: c, entityId: "tile" + ind}))
                    test += "tile at " + t.col + "/" + t.row + ".z: " + t.z + "\n"
            }
        }
}

The component to load tiles looks like this:

    Component {
        id: tileC

        Tile {
            property int base_tile: 0
            property int index: 0
            entityType: "base_tile"
            id: tile
            col: game.level ? index % game.level.columns : 0
            row: game.level ? (index - col) / game.level.columns : 0
            level: 0
        }
    }

Where tile is derived from GameItem, which is derived from EntityBase.

The QML for the levels that get loaded with the dynamic component looks something like this:

BaseLevel {
    tz: "100000000100000000100000000100000000111011111"
    Player{ col:0; row: 0; level:2; itemHeight: 1}
    Gem{ col:8; row: 4; level:1; itemHeight: 0}
    Gem{ type: 2; col:0; row: 4; level:1; itemHeight: 0}
    Block{ col:3; row: 0; level:1; itemHeight: 1}
   // Rock{col:3; row: 4; level:1; itemHeight: 1}

}

With tz being the data which levels the tiles should have, and the children are all items that are derived from GameItem, that are placed into the level on top of the tiles.
BaseLevel is the base type for the levels which has some properties like rows and columns for the size of the level, and more.

Voted #1 for:

  • Easiest to learn
  • Most time saving
  • Best support

Develop Cross-Platform Apps and Games 50% Faster!

  • Voted the best supported, most time-saving and easiest to learn cross-platform development tool
  • Based on the Qt framework, with native performance and appearance on all platforms including iOS and Android
  • Offers a variety of plugins to monetize, analyze and engage users
FREE!
create apps
create games
cross platform
native performance
3rd party services
game network
multiplayer
level editor
easiest to learn
biggest time saving
best support