Forums

OverviewFelgo 2 Support (Qt 5) › Lives bar

Tagged: 

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #15747

    Isak

    Hello,

     

    I have been trying to get my lives bar to work, basicaly i just want sprites of my ship to show in the right bottom corner. So i made an QML with the name of Lives. And have an property called lives which i add and subtract from.

    Now how to mange how many ship to be seen is my problem, i tried to use if and else and also for, but you cant use that in an Flow{}. so i did the following and it works but i feels like it would be an better way with better code?

            Flow {
                width: 100
                height: 20
    
                anchors.bottom: scene.gameWindowAnchorItem.bottom
                anchors.right: scene.gameWindowAnchorItem.right
    
                layoutDirection: "RightToLeft"
    
                Lives{visible: lives == 3}
                Lives{visible: lives == 3}
                Lives{visible: lives == 3}
    
                Lives{visible: lives == 2}
                Lives{visible: lives == 2}
    
                Lives{visible: lives == 1}
    
    
            }

    I am thinking if i want in the future to add some kind of explosion in the menu this solution wont work.

    Would love if someone could give me some advice=)?

     

    /Isak

    #15748

    Günther
    Felgo Team

    Hi Isak!

    You can probably use a Repeater to display the correct amount of Lives, e.g. like this:

    Flow {
    
      // ...
    
      // add Lives Item multiple times, based on amount of lives
      Repeater {
        model: lives 
        Lives { }
      }
    
    }

     

    Best,
    Günther

    #15749

    Isak

    GT said:

    Hi Isak!

    You can probably use a Repeater to display the correct amount of Lives, e.g. like this:

    Flow {
    
      // ...
    
      // add Lives Item multiple times, based on amount of lives
      Repeater {
        model: lives 
        Lives { }
      }
    
    }

     

    Best,
    Günther

    Hello Günther,

     

    Awesome, that looks exactly what i was looking for last night=)

     

    Thanks

     

    /Isak

    #15758

    Isak

    Hello Günter,

     

    I feels like it is problem when removing items from the repeater often two lives is removed but i only collide once. I am changing the lives property in fixture.onBeginContact with lives–.

    My first thought was that i collides several times and it counts down two but dont know how to force it to only remove one life when colliding with an enemy?

     

    Thanks

     

    /Isak

    #15759

    Marcin

    Hi Isak,

    I am not sure if it’s going to work in your situation, but what about storing some local variable in onBeginContact and set it to false on onEndContact ?
    Some pseudo code:

    onBeginContact:
    {
    if (playerContactInProgress) {
    return;
    }
    playerContactinProgress = true
    }

    onEndContact: {
    playerContactinProgress = false
    }

    Of course probably it needs to be more sophisticated to check only if contact with exact entity started.

    But it looks like the problem can be somewhere else.
    The pseudo code above is just a hack, not the proper solution.
    What does your debugging tell you?
    Did you try to console.log something on contact begin with the id of the entity you collide with?
    Maybe there is some logic issue there.

    Also you can debug any property change with onLivesChange and see how many times do you call it etc

    Best,
    Marcin

    #15784

    Isak

    senkal said:

    Hi Isak,

    I am not sure if it’s going to work in your situation, but what about storing some local variable in onBeginContact and set it to false on onEndContact ?
    Some pseudo code:

    onBeginContact:
    {
    if (playerContactInProgress) {
    return;
    }
    playerContactinProgress = true
    }

    onEndContact: {
    playerContactinProgress = false
    }

    Of course probably it needs to be more sophisticated to check only if contact with exact entity started.

    But it looks like the problem can be somewhere else.
    The pseudo code above is just a hack, not the proper solution.
    What does your debugging tell you?
    Did you try to console.log something on contact begin with the id of the entity you collide with?
    Maybe there is some logic issue there.

    Also you can debug any property change with onLivesChange and see how many times do you call it etc

    Best,
    Marcin

    Hello Marcin,

     

    Thanks for the tip, i think that actually may work, Is going to test it tonight.

     

    /Isak

    #15785

    Isak

    senkal said:

    Hi Isak,

    I am not sure if it’s going to work in your situation, but what about storing some local variable in onBeginContact and set it to false on onEndContact ?
    Some pseudo code:

    onBeginContact:
    {
    if (playerContactInProgress) {
    return;
    }
    playerContactinProgress = true
    }

    onEndContact: {
    playerContactinProgress = false
    }

    Of course probably it needs to be more sophisticated to check only if contact with exact entity started.

    But it looks like the problem can be somewhere else.
    The pseudo code above is just a hack, not the proper solution.
    What does your debugging tell you?
    Did you try to console.log something on contact begin with the id of the entity you collide with?
    Maybe there is some logic issue there.

    Also you can debug any property change with onLivesChange and see how many times do you call it etc

    Best,
    Marcin

    Thanks for the tip Marcin!

     

    But it still behaves that way, instead of just removing one it removes two, i am thinking if its the Repeater i use that makes it that way. Maybe should send it in to the support… getting really frustraded on this now have been stuck a few days on it.

     

    /Isak

    #15788

    Günther
    Felgo Team

    Hi Isak!

    Sorry for the late reply, I just tried this issue by adding a lives property to our FlappyBird demo and it seems to work.

    FlappyBirdMain.qml:

    GameWindow {
    
    
      property int lives: 5
    
      Flow {
        // add Lives Item multiple times, based on amount of lives
        Repeater {
          model: lives
          Item { width: 50; height: 50;
            Rectangle { color: "red"; width: 25; height: 25 } }
        }
        z: 1 // show lives above other items below this one
      }
    
     //  ...
    
    }

    Pipes.qml:

    EntityBase {
    
      // ...
    
    BoxCollider {
        // ...
        fixture.onBeginContact: {
          window.lives--
          console.log("LIVES: "+window.lives)
          if(window.lives === 0)
            player.gameOver()
        }
    }
    
    }

     

    With these two modifications it works to collide 5 times with a Pipe that reduces the lives property before the gameOver occurs. The onBeginContact is only fired once for me.

    Can you try if this works for you with FlappyBird? If yes, maybe you can find out what’s wrong by comparing the Flappy code with your own for possible differences. If your issue persists, please create a small example project with a minimum code setup that does not work and send it to us at support@felgo.com. We can have a closer look if we can reproduce the issue then.

    Best,
    Günther

     

     

     

    #15790

    Isak

    GT said:

    Hi Isak!

    Sorry for the late reply, I just tried this issue by adding a lives property to our FlappyBird demo and it seems to work.

    FlappyBirdMain.qml:

    GameWindow {
    
    
      property int lives: 5
    
      Flow {
        // add Lives Item multiple times, based on amount of lives
        Repeater {
          model: lives
          Item { width: 50; height: 50;
            Rectangle { color: "red"; width: 25; height: 25 } }
        }
        z: 1 // show lives above other items below this one
      }
    
     //  ...
    
    }

    Pipes.qml:

    EntityBase {
    
      // ...
    
    BoxCollider {
        // ...
        fixture.onBeginContact: {
          window.lives--
          console.log("LIVES: "+window.lives)
          if(window.lives === 0)
            player.gameOver()
        }
    }
    
    }

     

    With these two modifications it works to collide 5 times with a Pipe that reduces the lives property before the gameOver occurs. The onBeginContact is only fired once for me.

    Can you try if this works for you with FlappyBird? If yes, maybe you can find out what’s wrong by comparing the Flappy code with your own for possible differences. If your issue persists, please create a small example project with a minimum code setup that does not work and send it to us at support@felgo.com. We can have a closer look if we can reproduce the issue then.

    Best,
    Günther

     

     

     

     

     

    Hello Günther,

     

    Thanks for the reply, i still dont get in to work it removes two lives almost every time. So i have zipped and sent you some code hope that you can find what i am doing wrong=)

     

    Have a good weekend.

     

    /Isak

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