Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 3 Support (Qt 5) › movement keys for player

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #16270

    Todd

    Hi I am making a quick program of a guy in my game and I need the right key to make him move right, same with left. But For some reason it wont work but my code has no errors. Can an animated sprite not be assigned functions like this? Do I need to make an alias or something? Here is my code

     

    import QtQuick 2.7
    import QtQuick.Window 2.2
    
    
    Item {
    width:Screen.width
    height:Screen.height-10
    
    Keys.onPressed: {
    
    if (event.key===Qt.Key_up){
    event.accepted = true;
    sprite2.y=(sprite2.y) +25
    }
    
    }
    
    Flickable{
    width:Screen.width
    height:Screen.height
    contentHeight: Screen.height *4
    contentWidth: Screen.width
    interactive: true
    boundsBehavior: Flickable.StopAtBounds
    
    Image{
    id: box
    anchors.fill: parent
    source: "artwork/rect.png"
    sourceSize.width: Screen.width
    sourceSize.height: Screen.height*4
    }
    
    AnimatedSprite {
        id: sprite2
        width: 100
        height: 200
        anchors.centerIn: parent
        source: "artwork/WalkingManSpriteSheet.png"
        frameCount: 8
        frameRate: 6
        frameWidth: 40
        frameHeight:60
        loops: 100
    }
    
    Image{
    id:twitter
    source: "artwork/twitter.png"
    x:500
    y:horizontalCenter
    }
    AnimatedSprite {
        id: sprite
        width: 300
        height: 425
        anchors.centerIn: hoizontalCenter
        source: "artwork/cannon.png"
        frameCount: 12
        frameRate: 8
        frameWidth: 128
        frameHeight:155
        loops: 100
    }
    
    
    }
    }

     

    #16272

    Alex
    Felgo Team

    Hi Todd,

    the reason is most likely that the item you has no focus, and thus is not actively listening to key press events. Also I want to point you to our TwoAxisController that was designed exactly for your use-case. In the example usage of this component you also see the solution to your problem. The item that has focus must forward the key events to the key listener, in our case the TwoAxisController. The easiest way to keep track of the focus in your game is just using the activeScene property of the GameWindow. This will also set the focus property of the scene to true. Whenever you switch a scene, just update the activeScene property accordingly and you can be sure that your active scene is also focused.

    Hope this was not too confusing 🙂

    As a quick fix for you, you could also simply add

    focus: true

    to your Item, but it never hurts to get deeper insights right 😉

    Cheers,
    Alex

    #16277

    Todd

    Hey so i tried setting the item’s focus to true and im not getting anything 🙁

    this is the code

    import QtQuick 2.7
    import QtQuick.Window 2.2
    
    
    Item {
    width:Screen.width
    height:Screen.height-10
    focus: true
    
    Keys.onPressed: {
    
    if (event.key===Qt.Key_Right){
    event.accepted = true;
    sprite2.y=(sprite2.y) +25
    }
    
    }
    
    Flickable{
    width:Screen.width
    height:Screen.height
    contentHeight: Screen.height *4
    contentWidth: Screen.width
    interactive: true
    boundsBehavior: Flickable.StopAtBounds
    
    Image{
    id: box
    anchors.fill: parent
    source: "artwork/rect.png"
    sourceSize.width: Screen.width
    sourceSize.height: Screen.height*4
    }
    
    AnimatedSprite {
        id: sprite2
        width: 100
        height: 200
        anchors.centerIn: parent
        source: "artwork/WalkingManSpriteSheet.png"
        frameCount: 8
        frameRate: 6
        frameWidth: 40
        frameHeight:60
        loops: 100
    
    }
    
    Image{
    id:twitter
    source: "artwork/twitter.png"
    x:500
    y:horizontalCenter
    }
    AnimatedSprite {
        id: sprite
        width: 300
        height: 425
        anchors.centerIn: hoizontalCenter
        source: "artwork/cannon.png"
        frameCount: 12
        frameRate: 8
        frameWidth: 128
        frameHeight:155
        loops: 100
    }
    
    
    }
    }

     

    #16278

    Todd

    I am kind of confused because for some reason when i switch the event to listen for twitter.png it will move the image when i press the keys. Just not for the sprite why is this?

    #16279

    Todd

    Gunther,

    Thanks for the help I found that after changing the anchors.Centerin property it worked

Viewing 5 posts - 1 through 5 (of 5 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded