×

Discussion Board

Results 1 to 7 of 7
  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    16

    Dynamic Object Management (Create, Delete, Create, ....)

    Hello All,

    I am in process to build a game using Qt and C++ for Meego. I have come across a scenario in which a box2d word is created and game get started. After quitting game, user go to the main menu and then as he want to play a new game. As user presses new game button and 'gamePage' (which is actually a Rectangle Element) is displayed. In this page I have to place box2d world again with initialized bodies. but in my case bodies in box2d world are in previous positions.

    So what I want is that every time new game starts new world should be created and will be placed in 'gamePage'.

    I am using following box2d demo to build my game application 'http://projects.developer.nokia.com/mototrialracer'

    I have used Loader and Component Elements but these didn't show required results.

    here are some code segments to give you more info about the matter.



    MouseArea {
    anchors.fill : parent

    onPressed: {
    newGameImage.source = "menu_item_h.jpg"
    }

    onReleased: {
    newGameImage.source = "menu_item.jpg"
    }

    onClicked: {
    newGameImage.source = "menu_item.jpg"

    mainPage.opacity = 0.0
    gamePage.opacity = 1.0

    //gamePage.loadWorld()
    }
    }

    above code segment is from file MainPage.qml

    following is from file GamePage.qml

    Component {
    id: worldComponent

    CarromWorld
    {
    id: world
    objectName: "worldObject"

    width: worldWidth
    height: worldHeight
    }
    }

    Loader {
    id: worldLoader

    sourceComponent : worldComponent

    onLoaded: {
    if(initCtrlElement)
    {
    console.log("World Loaded: "+ initCtrlElement)
    initControls()
    }
    }
    }

    Kindly guide me.

    Regards,

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    449

    Re: Dynamic Object Management (Create, Delete, Create, ....)

    Hi,

    "After quitting game, user go to the main menu and then as he want to play a new game. "

    At this point, while quitting you can try unloading the currently loaded item, by setting the SourceComponent to empty or undefined.
    http://doc.qt.nokia.com/4.7-snapshot/qml-loader.html

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    16

    Re: Dynamic Object Management (Create, Delete, Create, ....)

    Hi,

    I have done so but it not working so. on new game box2dwolrd does not exits.

  4. #4
    Super Contributor
    Join Date
    Mar 2009
    Posts
    1,024

    Re: Dynamic Object Management (Create, Delete, Create, ....)

    Hi,
    the code you pasted doesn't really show how you load your component.
    The most intersting line is commented out.
    //gamePage.loadWorld()

    BTW the kusumk comment is valid. You can try that approach!
    If it doesn't work is because you are doing something wrong.
    Paste here your code if you want to get some help.

    http://doc.qt.nokia.com/4.7-snapshot/qml-loader.html

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    16

    Re: Dynamic Object Management (Create, Delete, Create, ....)

    Following code segment is from MainPage.qml

    Rectangle {

    anchors.horizontalCenter: parent.horizontalCenter
    y: 225

    width: itemWidth
    height: itemHeight

    Image {
    id: newGameImage
    anchors.fill: parent
    source: "menu_item.jpg"
    }

    Text {
    width: 375
    height: 55

    anchors.verticalCenter: parent.verticalCenter
    anchors.horizontalCenter: parent.horizontalCenter

    color: "white"
    //text: "NEW GAME"
    text: "New Game"
    style: Text.Outline
    horizontalAlignment: Text.AlignHCenter
    verticalAlignment: Text.AlignVCenter
    font.pixelSize: fontPixelSize
    }

    MouseArea {
    anchors.fill : parent

    onPressed: {
    newGameImage.source = "menu_item_h.jpg"
    }

    onReleased: {
    newGameImage.source = "menu_item.jpg"
    }

    onClicked: {
    newGameImage.source = "menu_item.jpg"

    mainPage.opacity = 0.0
    gamePage.opacity = 1.0

    gamePage.initWorld()
    }
    }
    }

    following is code from GamePage.qml

    Rectangle {

    //id: gamePlay
    objectName: "gamePage"

    width: 480
    height: 854
    radius: 10

    function initWorld(){
    flickAbleWorld.initWorldCtrl()
    }

    Image {
    id: backgroundImage

    anchors.fill: parent
    source: "background.jpg"
    }

    Flickable {

    id: flickAbleWorld
    objectName: "flickAbleWorld"

    x: (parent.width - worldWidth) / 2
    y: (parent.height - worldHeight) / 2

    interactive: false

    width: worldWidth
    height: worldHeight

    contentWidth: worldWidth
    contentHeight: worldHeight

    Image{

    id: boardImage

    anchors.fill: parent

    width: parent.width
    height: parent.height

    source: "board.png"
    }

    Component {
    id: worldComponent

    CarromWorld
    {
    id: world
    objectName: "worldObject"

    width: worldWidth
    height: worldHeight
    }
    }

    Component {
    id: dummy

    Rectangle {
    anchors.fill: parent
    }
    }

    function initWorldCtrl(){

    console.log("Init World Control")
    if(worldLoaded)
    {
    appWindow.initCtrls() //emitting signal to C++ to initialize controls
    console.log("World Initialized")
    }
    }

    Loader {
    id: worldLoader
    anchors.centerIn: parent

    sourceComponent: worldComponent

    onLoaded: {
    if(initCtrlElement)
    {
    console.log("World Loaded: "+ initCtrlElement)
    worldLoaded = true
    }
    }
    }

    }//Flickable
    }

    hopefully above is enough code to sort out the issue

    with this implementation I am not getting 'box2dworld' for the second time

  6. #6
    Super Contributor
    Join Date
    Mar 2009
    Posts
    1,024

    Re: Dynamic Object Management (Create, Delete, Create, ....)

    Hi,
    Above code is enough. that code basically load the component.. and that's it.
    You are not unloading and re-loading anything.
    As we suggested in our previous post, you should set sourceComponent to "" when you want to unload the component and set it back when you want to load it again.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    449

    Re: Dynamic Object Management (Create, Delete, Create, ....)

    To be specific please try doing something like this -
    in GamePage.qml, in the below initWorld() method

    function initWorld(){
    flickAbleWorld.clearWorld()
    flickAbleWorld.initWorldCtrl()
    }

    and in Flickable {} add -

    function clearWorld(){
    worldLoader.source = ""
    console.log("Clearing World")

    }

    Hope this helps

Similar Threads

  1. Explanation on Dynamic Object Management in QML
    By VVNart in forum [Archived] Qt Quick
    Replies: 10
    Last Post: 2011-05-09, 09:30
  2. Create dynamic checkbox
    By cool_ranjeet in forum [Archived] Flash Lite on Nokia Devices
    Replies: 3
    Last Post: 2009-12-05, 13:06
  3. create Dynamic SettingItemList
    By CSB in forum Symbian
    Replies: 5
    Last Post: 2009-08-27, 09:11
  4. How to create dynamic array ?
    By rohanwaugh in forum Symbian
    Replies: 2
    Last Post: 2009-07-06, 16:11
  5. HELP! How to create a dynamic tab?
    By joseph.m in forum Symbian
    Replies: 4
    Last Post: 2008-08-05, 01:26

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×