Forums

OverviewV-Play 1 Support › Accessing Properties

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #7178

    Preston

    Hi,

    The best way to ask my question is to first present the code:

    // import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
    import QtQuick 1.1
    import VPlay 1.0
    
    SceneBase {
    
        property bool onOrOff
        onOnOrOffChanged: {
            toggleSplBtn.color = (onOrOff == true) ? "red" : "darkblue"
        }
    
        Sound {
            id: soundChosen
        }
    
        SimpleButton {
            id: toggleSplBtn
            anchors.top: parent.top
            anchors.right: parent.right
    
            text: "Spell"
    
            Component.onCompleted: {
                if(onOrOff == true){
                    toggleSplBtn.color = "red"
                }else{
                    toggleSplBtn.color = "darkblue"
                }
            }
    
            onClicked: {
                onOrOff = !onOrOff
                console.debug(onOrOff)
            }
        }
    
        SimpleButton {
            anchors.left: parent.left
            anchors.bottom: parent.bottom
    
            text: "Back"
    
            onClicked: {
                console.debug("BACK TO MAIN MENU")
                window.state = "menu"
            }
        }
    
        Scene {
            id: movableScene
    
            MultiTouchArea {
                //anchors.fill: parent
                width: parent.width
                height: parent.height
    
                //NOTE: I want to set height above based on the calculation below
    
                enabled: true
                multiTouch.target: parent
                multiTouch.dragAxis: MultiTouch.YAxis
                multiTouch.maximumY: 0
                onPressed: {
                    console.debug("multi touch for movableScene")
                }
            }
    
            EntityManager {
                id: entityManager
                entityContainer: movableScene
            }
    
            property url jsonFile: "json/ThirtyConsonantsWithVowels.json"
    
            Component {
                    id: btnComp
    
                    EntityBase {
    
                        id: entityBaseX
    
                        property int index: 1
                        property string character
                        property double spacing: 50
                        property double widthValue: 40
                        property double heightValue: 40
                        property double numOfCol: 8
                        property double widthOfSet: numOfCol * spacing
    
                        entityId: index
                        entityType: "buttonEntity"
                        SimpleButton {
                            id: btn
    
                            property int col: index % numOfCol + 1
                            property int row: Math.floor(index / numOfCol) + 1
    
                            x: col * spacing + (parent.parent.width -  widthOfSet)/2 - spacing + (spacing - widthValue)/2
                            y: row * spacing
    
                            width: widthValue
                            height: heightValue
    
                            //text: col + "/" + row
                            text: character
    
                            onClicked: {
                                console.debug("button " + btn.text + " clicked")
                                if(onOrOff){
                                    soundChosen.source = "snd/" + btn.text + "-complete" + ".wav"
                                    soundChosen.play()
    
                                }else{
                                    soundChosen.source = "snd/" + btn.text + ".wav"
                                    soundChosen.play()
    
                                }
                                console.debug(soundChosen.source)
                            }
                        }
                        Component.onCompleted: console.debug("btn created " + index)
                    }
            }
    
            Component.onCompleted: {
                console.debug("consonantsPlusVowelsScene - Component.onCompleted")
                console.debug(jsonFile)
                console.debug("inside consonantsPlusVowels")
                var data = fileUtils.readFile(jsonFile)
                console.debug(data)
    
                var jsonObj = JSON.parse(data)
                console.debug("jsonObj")
                console.debug(jsonObj)
    
                for(var i = 0; i < jsonObj.length; i++)
                {
                    console.debug(jsonObj[i])
                    entityManager.createEntityFromComponentWithProperties(btnComp, {index: i, character: jsonObj[i]})
                    console.debug("create button")
                }
    
    
    
              
                //console.debug("entityBaseX.numOfCol")
                //console.debug(entityBaseX.numOfCol)
                //var numOfRow = jsonObj.length / entityBaseX.numOfCol
                //console.debug("numOfRow is below " + numOfRow)
                //console.debug(numOfRow)
                //movableScene.height = numOfRow * btn.spacing
                //movableScene.height = 100
            }
        }
    }
    

     

    If you look at the commented lines above:

    //console.debug("entityBaseX.numOfCol")
                //console.debug(entityBaseX.numOfCol)
                //var numOfRow = jsonObj.length / entityBaseX.numOfCol
                //console.debug("numOfRow is below " + numOfRow)
                //console.debug(numOfRow)
                //movableScene.height = numOfRow * btn.spacing
                //movableScene.height = 100

    You will see that I am trying to access the property called “numOfCol” of EntityBase with id of entityBaseX.

    The problem is, this does not work.

    I am getting this error:

    <!–StartFragment–>file:///Users/bliss/Coder/V-Play/TP-build-desktop-Desktop_Qt_4_8_1_for_GCC__Qt_SDK__Debug/TP.app/Contents/Resources/qml/ConsonantsPlusVowelsScene.qml:148: ReferenceError: Can’t find variable: entityBaseX

    EntityManager: onCompleted()<!–EndFragment–>

     

    So how do I access the property numOfCol in this case???

     

    Thank you very much…..

     

     

     

    #7179

    Christian
    V-Play Team

    Hi bliss,

    nice to see you’re working full steam on your project :)

    To your question, your code would be correct but the thing is, you can’t access an element inside a Component from the outside. This is because Components describe the structure of elements that will be created in the future and do not exist yet.

    So to access properties of elements created from Components with EntityManager, you need to access them via entityManager.getEntityById().

    Your code could look like this:

    
    //find entity with ID == 1
    var entity = entityManager.getEntityById(1);
    
    //access entity property
    var numOfRow = jsonObj.length / entity.numOfCol;

    Hope this helps!

    Cheers, Chrisu

    • This reply was modified 4 years, 4 months ago by  Chrisu.
Viewing 2 posts - 1 through 2 (of 2 total)

RSS feed for this thread

You must be logged in to reply to this topic.

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