Expected token
-
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
-
oh, sorry for the bad formatting!
-
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() } } }
-
@elmoterich To format it properly surround the code with ``` (3 backticks).
-
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
-
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() } }
-
Thanks again!
I understand your solution only a bit.
The elchi fades out, but does not appear again. So i addedPropertyAnimation { 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.
-
Anyone an idea?
-
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
-
Ok, I found the answer!
It works with Component.onCompleted: {musicplay()}Greetings elmoterich