Forums

OverviewFelgo 2 Support (Qt 5) › Push and hold button funtion

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #16088

    Todd

    Hey everyone, so I am a newbie programmer and I am working on a game from the v play flappy bird template. I would like to change the button push function that makes the bird do its movement vector.

    I ideally would like to have a button function in place for the player that as you hold down the button the player will vertically raise and when let go it will continuously fall. So more of a push and hold function instead of a tapping function. This is the code that controls the current movement, what would I need to do to implement this?

    
      function push() {
        wabbleX.stop()
        wabbleY.stop()
        audioManager.play(audioManager.idWING)
        collider.body.linearVelocity = Qt.point(0,0)
        var localForwardVector = collider.body.toWorldVector(Qt.point(0, upwardforce));
        collider.body.applyLinearImpulse(localForwardVector, collider.body.getWorldCenter());
      }

     

    #16094

    Günther
    Felgo Team

    Hi Todd!

    You can use a Timer to apply the force repeatedly as long as the user holds the button:

      property bool rising: false
      Timer {
        interval: 10
        repeat: true
        running: rising == true // set rising false when user releases touch to stop rising
        onTriggered: {
          collider.body.linearVelocity = Qt.point(0,0)
          collider.body.applyLinearImpulse(Qt.point(0,upwardforce), collider.body.getWorldCenter());
        }
      }
    
      function push() {
        wabbleX.stop()
        wabbleY.stop()
        audioManager.play(audioManager.idWING)
        rising = true
      }

    Best,
    Günther

    #16095

    Todd

    Hey Gunther thanks for the help, now when i run the game the player will rise, but when the button is held down and released the player will not fall back down. Was something left out?

    This is the whole player.qml

     

    import Felgo 3.0
    import QtQuick 2.0
    
    EntityBase {
      id: player
      entityType: "player"
    
      property real upwardforce: -85
      property int resetX: 0
      property int resetY: 0
    
      width: collider.radius * 2
      height: collider.radius * 2
    
      signal gameOver()
    
      Component.onCompleted: reset()
    
      onGameOver: {
        spriteSequence.running = false
      }
    
      SpriteSequenceVPlay {
        id: spriteSequence
    
        anchors.centerIn: parent
    
        SpriteVPlay {
          name: "idle"
          frameCount: 3
          frameRate: 10
    
          frameWidth: 19
          frameHeight: 28
          source: "../../assets/img/redplanesprite.png"
        }
        rotation: wabbleX.running ? 0 : collider.linearVelocity.y/10
      }
    
      CircleCollider {
        id: collider
    
        radius: spriteSequence.height/2
        bodyType: Body.Dynamic
      }
    
      function reset() {
        player.x = resetX
        player.y = resetY
        collider.body.linearVelocity = Qt.point(0,0)
        activateWabbling()
        spriteSequence.running = true
      }
    
    
      property bool rising: false
      Timer {
        interval: 10
        repeat: true
        running: rising == true // set rising false when user releases touch to stop rising
        onTriggered: {
          collider.body.linearVelocity = Qt.point(0,0)
          collider.body.applyLinearImpulse(Qt.point(0,upwardforce), collider.body.getWorldCenter());
        }
      }
    
      function push() {
        wabbleX.stop()
        wabbleY.stop()
        audioManager.play(audioManager.idWING)
        rising = true
      }
    
      NumberAnimation on x {running: false; id: wabbleX; duration: 4000; loops: Animation.Infinite; easing.type: Easing.CosineCurve}
      NumberAnimation on y {running: false; id: wabbleY; duration: 4000; loops: Animation.Infinite; easing.type: Easing.SineCurve}
    
      function activateWabbling() {
        var wableVal = 15
        var rand = Math.random()
        var dir = (rand < 0.5 ? -wableVal/4*rand : wableVal/4*rand )
        wabbleX.from = player.x+dir
        wabbleX.to = player.x-dir
        wabbleX.start()
        rand = Math.random()
        dir = (rand < 0.5 ? -wableVal*rand : wableVal*rand )
        wabbleY.from = player.y+dir
        wabbleY.to = player.y-dir
        wabbleY.start()
      }
    }

     

    #16098

    Günther
    Felgo Team

    As long as the Timer is running the player will rise. As mentioned in the code comment of my post, the running property of the Timer is bound to:

        running: rising == true // set rising false when user releases touch to stop rising
    

    So by setting the Player::rising property to false, the timer will be deactivated. You can do this for example at the onReleased event of the MouseArea that handles your touch input.

    #16099

    Todd

    Hey Gunther,

    So I think I understand what you are saying, I went into the mainitem.qml and added a mouse area to the GameScene, which looks like this

      GameScene {
        id: gameScene
    
        onMenuPressed: {
          mainItem.state = "menu"
        }
        onNetworkPressed: {
          vplayGameNetworkScene.initialStateBeforeShow = "game"
          mainItem.state = "gameNetwork"
        }
        onUseCoinsPressed: {
        }
        MouseArea {
          anchors.fill: scene.gameWindowAnchorItem
          onPressed: {
            player.push()
          }
          onReleased: {
            player.push == false
          }
        }
      }

    This project I am working with is from the source code. So I just now added that mouse area, so would i need to set the onReleased somewhere else?

    Thanks you for all the help!

    #16102

    Günther
    Felgo Team

    Instead of setting the rising property to false, your code in onReleased sets the player.push function to false, which makes no sense. You can set rising to false in the MouseArea within GameScene like this:

      MouseArea {
        id: mouseControl
        anchors.fill: scene.gameWindowAnchorItem
        onPressed: {
          if(gameIsRunning) {
            player.push()
          }
        }
        onReleased: {
            player.rising = false
        }
      }

    Please have a closer look at the code and try to understand how everything works together – this will help you to avoid similar mistakes in the future.

    Best,
    Günther

Viewing 6 posts - 1 through 6 (of 6 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