ReferenceError: "..." is not defined



  • Im trying run this code, and it lunch the error: ReferenceError: "vi" is not defined. someone who can help me, please?

    import QtQuick 2.6
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.1
    import QtQml 2.2

    Window {
    id: window
    visible: true
    visibility: "FullScreen"

    Item{
        property double xb: 0;
        property double yb: 0;
        property double vx: 0;
        property double vy: 0;
        property int tita: 45;
        property int vi: 20;
        property double g: 9.8;
    
        Timer {
            id:time
            interval: 500
            repeat: true
            running: true
            onTriggered:{
                vx = vi*cos(tita);
                vy = vi*sin(tita)-(g*time);
                xb =  vi*cos(tita)*time;
                yb = vi*sen(tita)*time-(1/2*(g*(time^2)));
                console.log(vx,vy,xb,yb)
            }
    
        }
    
    }
    
    
    
    
    
    
    
    Background{
    id: background
    }
    
    Bird{
    id: bird
    x: 20
    y: 600
    
        Behavior on x{
            NumberAnimation{
            duration: 600
    
    
            }
        }
    
    }
    
    MouseArea{
        anchors.fill: parent
        onClicked: {
            bird.z = bird.z +1
            bird.x= 1000
        }
    }
    

    Button {
    text: "Salir"
    onClicked: window.close()

    }
    

    }



  • Please try to give id for your "Item" element, and try to access vi with "item_id.vi"

    Item{
      id: myItem
      .... ..... ...
    }
    
    Timer{
     ....
     onTriggered :{
       myItem.vx = myItem.vi * cos(myItem.tita)
     }
    }
    


  • Thank you, but it doesnt work! It works if I declare variables inside of the timer, but Im not sure if this are going to have any repercurssion in my final result.



  • Hi Patcs,

    Move the Timer out of Item and use Math operation as bellow

    Window {
    id: window
    visible: true

    // Item{
    property double xb: 0;
    property double yb: 0;
    property double vx: 0;
    property double vy: 0;
    property int tita: 45;
    property int vi: 20;
    property double g: 9.8;

        Timer {
            id:time
            interval: 500
            repeat: true
            running: true
            onTriggered:{
                vx = vi * Math.cos(tita);
                vy = vi * Math.sin(tita)-(g*time);
                xb = vi * Math.cos(tita) * time;
                yb = vi * Math.sin(tita) * time-(1/2*(g*(time^2)));
                console.log(vx,vy,xb,yb)
            }
        }
    

    // }
    }

    The properties belongs to Window. And what you represent "time" in your calculation. time is id for Timer



  • I was trying and the only solution that I found was this:

    import QtQuick 2.6
    import QtQuick.Window 2.2
    import QtQuick.Controls 2.1
    import QtQml 2.2

    Window {
    id: window
    visible: true
    visibility: "FullScreen"

    Item{

    Timer {
        id:time
        interval: 500
        repeat: true
        running: true
    
        property double xb: 0;
        property double yb: 0;
        property double vx: 0;
        property double vy: 0;
        property int tita: 45;
        property int vi: 20;
        property double g: 9.8;
    
        onTriggered:{
            vx = vi*cos(tita);
            vy = vi*sin(tita)-(g*time);
            xb =  vi*cos(tita)*time;
            yb = vi*sen(tita)*time-(1/2*(g*(time^2)));
            console.log(vx,vy,xb,yb)
        }
    
    }
    

    }

    Background{
    id: background
    }

    Bird{
    id: bird
    x: 20
    y: 600

    Behavior on x{
        NumberAnimation{
        duration: 600
    
    
        }
    }
    

    }

    MouseArea{
    anchors.fill: parent
    onClicked: {
    bird.z = bird.z +1
    bird.x= 1000
    }
    }
    Button {
    text: "Salir"
    onClicked: window.close()

    }
    }

    So, if someone need the solution, i found this, however I dont know if it is the best one. Thank you to everyone who have helped me!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.