Forums

OverviewFelgo 2 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.

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