0
\$\begingroup\$

I read an online introduction on using QML for game development and I find felgo is easy to pick up. I am trying to create a code to move a block along a given path and I would like to extract the current position of the block when I press on the mouse anywhere on the scene. I create two QML, main.qml is holding the GameWindow and the scene, entities/rect.qml is the block (EntityBase)

import QtQuick 2.0
import Felgo 3.0
import "entities"

// You get free licenseKeys from https://felgo.com/licenseKey
// With a licenseKey you can:
//  * Publish your games & apps for the app stores
//  * Remove the Felgo Splash Screen or set a custom one (available with the Pro Licenses)
//  * Add plugins to monetize, analyze & improve your apps (available with the Pro Licenses)
//licenseKey: "<generate one from https://felgo.com/licenseKey>"

GameWindow {
    id: game
    screenWidth: 800
    screenHeight: 400

    activeScene: scene

    EntityManager {
      id: em
      entityContainer: scene

      dynamicCreationEntityList: [
        Qt.resolvedUrl("entities/rect.qml")
      ]
    }


    Timer {
        interval: 3000
        repeat: false
        running: true
        onTriggered: {
            em.createEntityFromEntityTypeAndVariationType( {entityType: "shapeType"} );
        }
    }

    Scene {
        id: scene
        width: parent.width
        height: parent.height

        MouseArea {
            anchors.fill: parent
            onPressed: {
                em.getLastAddedEntity().showMe()
            }
        }

        focus: true
    }
}

the entities\rect.qml

import QtQuick 2.0
import Felgo 3.0

EntityBase {
  id: theentity
  entityType: "shapeType"


  function showMe() {
      console.debug("coord: " + thepath.waypoints[thepath.currentWaypointIndex].x + ", " + thepath.waypoints[thepath.currentWaypointIndex].y)
  }

  Rectangle {
    id: therect
    width: 80
    height: 50
    color: "red"

    PathMovement {
      id: thepath
      velocity: 60
      loops: 1

      waypoints: [
          {x:80, y:60},
          {x:680, y:60},
          {x:680, y:200},
          {x:120, y:200},
          {x:120, y:150},
          {x:20, y:150},
          {x:20, y:330}
      ]

      onPathCompleted: {
          removeEntity()
      }
    }
  }
}

This code will move the block along the path defined by waypoints at the given velocity (pixel per second). I interface the code and the mouse action to show the coordinates when the mouse is pressed, however, I find that the PathMovement seems only support to show the current waypoint instead of the actual coordinate of the block on the scene. I started the QML programming 2 weeks ago and don't have much experience in it. I wonder if there is any way to extract the real and actual coordinates of the block when PathMovement is used. If not, any suggestion to approach the solution? Thanks.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.