Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Expected token

Expected token

Scheduled Pinned Locked Moved QML and Qt Quick
qml
10 Posts 3 Posters 7.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elmoterich
    wrote on last edited by p3c0
    #1

    Hallo!

    I'm writing a little Game, called Elchjagd. It is very simple:

    import QtQuick 2.3
    import QtQuick.Window 2.2
    
    Window {
        width: 1920
        height: 1080
        visible: true
    
    
        
       Image {
            id: backgorund
            anchors.fill: parent
            source: "qrc:/waldi.png"
            //fillMode: ImagePreserveAspectCrop
        }
    
    
    
       Timer {
               id: zeit
               interval: 3000; running: true; repeat: true
               onTriggered: {
                   elchi.x = Math.random() * 1620
                   elchi.y = Math.random() * 780
               }
            }
    
        Image {
            id: elchi
            source: "qrc:/elchi2.png"
            height: 300
            width: 300
    
    
    
        MouseArea {
             id: area
             anchors.fill: elchi
    
            onClicked: {
                PropertyAnimation { target: elchi; property: "opacity"; to: 0 }
    
                elchi.x = Math.random() * 1620
                elchi.y = Math.random() * 780
    
                zeit.restart()
                }
    
        }
    
    
    }
    }
    

    I get the following error:

    qrc:/main.qml:61 Expected token ,' qrc:/main.qml:61 Expected token }'
    qrc:/main.qml:63 Expected token `:'

    when I erase

                elchi.x = Math.random() * 1620
                elchi.y = Math.random() * 780
    
                zeit.restart()
    

    and the {} it works.

    Greetings

    elmoterich

    1 Reply Last reply
    0
    • E Offline
      E Offline
      elmoterich
      wrote on last edited by
      #2

      oh, sorry for the bad formatting!

      p3c0P 1 Reply Last reply
      0
      • Hamed.MasafiH Offline
        Hamed.MasafiH Offline
        Hamed.Masafi
        wrote on last edited by Hamed.Masafi
        #3

        You CAN'T put qml element (PropertyAnimation in this case) inside a scripting block.

        Solution #1:

        Image {
        	id: elchi
        	source: "qrc:/elchi2.png"
        	height: 300
        	width: 300
        
        	Behavior on opacity { NumberAnimation {  } }
        
        	MouseArea {
        		id: area
        		anchors.fill: elchi
        
        		onClicked: {
        			elchi.opacity = 0
        
        			elchi.x = Math.random() * 1620
        			elchi.y = Math.random() * 780
        
        			zeit.restart()
        			}
        
        	}
        }
        

        Solution #2:

        Image {
        	id: elchi
        	source: "qrc:/elchi2.png"
        	height: 300
        	width: 300
        
        
        	PropertyAnimation { 
        		id: opacityAnim
        		target: elchi; 
        		property: "opacity"; 
        		to: 0 
        	}
        
        	MouseArea {
        		id: area
        		anchors.fill: elchi
        
        		onClicked: {
        			opacityAnim.start()
        
        			elchi.x = Math.random() * 1620
        			elchi.y = Math.random() * 780
        
        			zeit.restart()
        			}
        
        	}
        }
        

        Remote object sharing (OO RPC)
        http://forum.qt.io/topic/60680/remote-object-sharing-oo-rpc-solved

        Advanced, Powerful and easy to use ORM for Qt5
        https://forum.qt.io/topic/67417/advanced-powerful-and-easy-to-use-orm-for-qt5

        1 Reply Last reply
        0
        • E elmoterich

          oh, sorry for the bad formatting!

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @elmoterich To format it properly surround the code with ``` (3 backticks).

          157

          1 Reply Last reply
          0
          • E Offline
            E Offline
            elmoterich
            wrote on last edited by
            #5

            Hey!

            Thank you very much for your help!

            I tried the first solution and it works fine! The new problem now is, that the program doesn't wait the fadeout animation to be finished. By clicking the "elchi", it change its position but the animation cannot be seen.

            Greetings

            elmoterich

            1 Reply Last reply
            0
            • Hamed.MasafiH Offline
              Hamed.MasafiH Offline
              Hamed.Masafi
              wrote on last edited by Hamed.Masafi
              #6

              Your new solution:

              Image {
                  id: elchi
                  source: "qrc:/elchi2.png"
                  height: 300
                  width: 300
                  
                  
                  SequentialAnimation{
                      id: opacityAnim
                      PropertyAnimation { 
                          target: elchi; 
                          property: "opacity"; 
                          to: 0 
                      }
                      
                      ParallelAnimation{
                          PropertyAction{
                              target: elchi
                              property: "x"
                              value: Math.random() * 1620
                          }
                          
                          PropertyAction{
                              target: elchi
                              property: "y"
                              value: Math.random() * 780
                          }
                          ScriptAction{
                              script: zeit.restart()
                          }
                      }
                      
                  }
                  
                  MouseArea {
                      id: area
                      anchors.fill: elchi
                      
                      onClicked: opacityAnim.start()            
                  }
              }  
              

              Remote object sharing (OO RPC)
              http://forum.qt.io/topic/60680/remote-object-sharing-oo-rpc-solved

              Advanced, Powerful and easy to use ORM for Qt5
              https://forum.qt.io/topic/67417/advanced-powerful-and-easy-to-use-orm-for-qt5

              1 Reply Last reply
              0
              • E Offline
                E Offline
                elmoterich
                wrote on last edited by
                #7

                Thanks again!

                I understand your solution only a bit.
                The elchi fades out, but does not appear again. So i added

                PropertyAnimation {
                                         target: elchi;
                                         property: "opacity"
                                         to: 1
                                  }
                
                 SequentialAnimation {
                            id: opacityAnim
                            PropertyAnimation {
                                    target: elchi;
                                    property: "opacity"
                                    to: 0
                                 }
                
                
                            ParallelAnimation {
                                 PropertyAction {
                                     target: elchi
                                     property: "x"
                                     value: Math.random() * 1620
                                 }
                                 PropertyAction {
                                     target: elchi
                                     property: "y"
                                     value: Math.random() * 780
                                 }
                
                                 ScriptAction {
                                     script: zeit.restart()
                                 }
                            }
                             PropertyAnimation {
                                         target: elchi;
                                         property: "opacity"
                                         to: 1
                                 }
                
                
                    }
                

                If I run the program like this, the first jump works correctly, but then it just fades in and out at the same position.

                1 Reply Last reply
                0
                • E Offline
                  E Offline
                  elmoterich
                  wrote on last edited by
                  #8

                  Anyone an idea?

                  1 Reply Last reply
                  0
                  • E Offline
                    E Offline
                    elmoterich
                    wrote on last edited by elmoterich
                    #9

                    Hey!

                    I found a solution for my probelm:
                    i edited my sourcecode like this:

                    import QtQuick 2.3
                    import QtQuick.Window 2.2
                    import QtMultimedia 5.5
                    
                    
                    Window {
                        id: fenster
                        width: 1920
                        height: 1080
                        visible: true
                    
                        property var punkte: 0
                    
                           //SoundEffect {
                        //    id: mukke
                        //    source: "qrc:/titelsound.mp3"
                       // }
                    
                        Audio {
                            id: music
                            source: "qrc:/titelsound.mp3"
                        }
                    
                        music.play() // hier kopmmt es zu einem Syntax-Fehler
                    
                        MouseArea {
                            id: vollbild
                            anchors.fill: parent
                            cursorShape: Qt.CrossCursor
                    
                         }
                    
                     
                        
                        Image {
                            id: backgorund
                            anchors.fill: parent
                            source: "qrc:/waldi.png"
                    
                            Text {
                                id: text1
                                x: 1806
                                y: 982
                                width: 29
                                height: 14
                                color: "#e00000"
                                text: qsTr(punkte)
                                font.bold: true
                                font.pixelSize: 36
                            }
                    
                            Text {
                                id: text2
                                x: 1618
                                y: 982
                                color: "#aa1029"
                                text: qsTr("Punkte:")
                                font.bold: true
                                font.pixelSize: 36
                            }
                    
                    
                            //fillMode: ImagePreserveAspectCrop
                    
                        }
                    
                    
                    
                       Timer {
                               id: zeit
                               interval: 1000; running: true; repeat: true
                    
                               onTriggered: {
                                   area.zuf1 = Math.random() * 1620
                                   area.zuf2 = Math.random() * 780
                                   opacityAnim.start()
                                   // elchi.x = Math.random() * 1620
                                  // elchi.y = Math.random() * 780
                               }
                            }
                    
                    
                        Image {
                            id: elchi
                            source: "qrc:/elchi2.png"
                            height: 300
                            width: 300
                    
                    
                    
                            SequentialAnimation {
                                id: opacityAnim
                                PropertyAnimation {
                                        target: elchi;
                                        property: "opacity"
                                        to: 0
                                     }
                    
                                ParallelAnimation {
                                     PropertyAction {
                                         target: elchi
                                         property: "x"
                                         value:area.zuf1
                                     }
                                     PropertyAction {
                                         target: elchi
                                         property: "y"
                                         value: area.zuf2
                                      }
                                   /*  PropertyAction {
                                         target: playSound
                                         porperty: "onPressed
                    
                                         property: "text"
                                         value: area.zuf2
                                     } */
                    
                    
                                     ScriptAction {
                                         script: zeit.restart()
                                     }
                                }
                                 PropertyAnimation {
                                             target: elchi;
                                             property: "opacity"
                                             to: 1
                                     }
                      }
                      SoundEffect {
                          id: playSound
                          source: "qrc:/peng_wave.wav"
                      }
                    
                    
                    
                    
                      MouseArea {
                            id: area
                            anchors.fill: elchi
                    
                    
                            cursorShape: Qt.SizeAllCursor
                    
                            property var zuf1
                            property var zuf2
                    
                            onClicked: {
                                zuf1 = Math.random() * 1620
                                zuf2 = Math.random() * 780
                                playSound.play()
                                opacityAnim.start()
                    
                                punkte = punkte + 10
                                text1.text = punkte
                              }
                    
                      }
                        }
                    
                    
                    }
                    
                    

                    So I created the random numbers earlier!

                    So, I have a new problem:

                    I want to have backgroundmusic played.

                    I used:

                    
                        Audio {
                            id: music
                            source: "qrc:/titelsound.mp3"
                        }
                    
                        music.play()  // hier kopmmt es zu einem Syntax-Fehler
                    

                    It is only possible to put "the music.play()" in an onClicked function, but I want to have the music, when the program starts

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      elmoterich
                      wrote on last edited by
                      #10

                      Ok, I found the answer!
                      It works with Component.onCompleted: {musicplay()}

                      Greetings elmoterich

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved