Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Drawing and undrawing text after collision and mouse pressed event  (Read 4139 times)

0 Members and 1 Guest are viewing this topic.

Narok

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
This is part of what I have:
                while (hoverEnemy == true)
                {
                        window.draw(cop);
                        std::cout << "Hover\n";

                        if (clickEnemy)
                        {
                                numofclicks + 1;
                                std::stringstream text;
                                text << "Cop has " << EnemyObject.eHp(copHitPoints, numofclicks) << " hp left ";
                                copHp.setString(text.str());

                               
                                window.draw(copHp);
                                std::cout << "Clicked\n";

                                if(copHitPoints == 0)
                                {
                                        EnemyObject.die();
                                }
                                clickEnemy = false;
                        }

                        hoverEnemy = false;
                }
               

What I want to do is make the health (copHitPoints) go down by 20 every click. The problem is when I set the clickEnemy to false at the end of the code above window.draw(copHp) pops up for a slight second. But when I leave out the clickEnemy = false; click is set to true infinity. What I also want is for the window.draw(cop) to "undraw" when clickEnemy is true.

The EnemyObject.eHp code:

 
        int eHp(int intialHp, int numClicks)
        {
                if (numClicks == 1)
                {
                        intialHp = 80;
                        return intialHp;
                }

                if (numClicks == 2)
                {
                        intialHp = 60;
                        return intialHp;
                }

                if (numClicks == 3)
                {
                        intialHp = 40;
                        return intialHp;
                }

                if (numClicks == 4)
                {
                        intialHp = 20;
                        return intialHp;
                }

                if (numClicks == 5)
                {
                        intialHp = 0;
                        return intialHp;
                }
               
                if (numClicks < 6)
                {
                        intialHp = 0;
                        return intialHp;
                }
        }

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Drawing and undrawing text after collision and mouse pressed event
« Reply #1 on: August 11, 2012, 09:43:02 am »
I'm not trying to be rude or insulting, but I pretty much understood nothing you asked. ;D So I will only point you to some things I think are weird.

This line (numofclicks + 1;) does nothing. You want to write numofclicks = numofclicks + 1; (or ++numofclicks;)

It doesn't seem that copHitPoints is ever updated. EnemyObject.eHp returns the new value but you don't use it to update copHitPoints.

window.draw(copHp); is called only when clickEnemy is true, it's no surprise that you can only catch a glimpse of it.

You want to "undraw" cop when clickEnemy is true, it means you want to draw it only when clickEnemy is false. Put it in the else of your if (clickEnemy)

Why do you use a while?

Instead of assigning false to hoverEnemy and clickEnemy, shouldn't you get this value from the function where you check if it hovers / clicks or not?

Narok

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: G.
« Reply #2 on: August 11, 2012, 04:40:59 pm »
numofclicks = numofclicks + 1, noted

I don't use it to update copHitPoints because I put it into where I want copHitPoints to display.

I know it's no surprise, I'm asking how to make it so it will update with clicks

What!?!?

I use while so it doesn't bug out on me and freeze

I think I'm going to try if (clickEnemy == true && numofclicks == 1)
{
//something
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Drawing and undrawing text after collision and mouse pressed event
« Reply #3 on: August 11, 2012, 04:57:10 pm »
The first problem with your post is, that it only contains statements and no questions so it's a bit hard to write an answer.
The second and bigger problem is, that you're lacking knowledge in C++ and/or programming logic. As you've noticed you actually know what you want but you're unable to express it in C++, so I highly suggest you read a good C++ book and get your logical think up to speed for programming. ;)

As for your code, have a look at the example in the documentation, do you see how nicely the updating & event handling is split from the drawing part? You should definitely follow that rule. Don't ever draw stuff within in your logic part (updating/event handling) and only use a small if-statement switch for drawing or not drawing something with in the drawing part. :)


(ot: thanks Laurent for that website, otherwise you'd read that wrong spelling once more ;D)
« Last Edit: August 11, 2012, 05:26:39 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Narok

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Drawing and undrawing text after collision and mouse pressed event
« Reply #4 on: August 11, 2012, 05:05:43 pm »
As for your code, have a look at the example in the documentation, do you see how nicely the updating & event handling is split from the drawing part? You should definitely follow that rule. Don't ever draw stuff within in your logic part (updating/event handling) and only use a small if-statement switch for drawing or not drawing something with in the drawing part. :)
(ot: thanks Laurent for that website, otherwise you'd read that wrong spelling once more ;D)

I know how it goes: Events, Logic, then Rendering. And my question is how do I fix the clickEnemy thing so it will record the number of clicks, but wont appear and disappear in a millisecond

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Drawing and undrawing text after collision and mouse pressed event
« Reply #5 on: August 11, 2012, 05:24:41 pm »
This was already answered. ;)

window.draw(copHp); is called only when clickEnemy is true, it's no surprise that you can only catch a glimpse of it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Narok

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Drawing and undrawing text after collision and mouse pressed event
« Reply #6 on: August 11, 2012, 05:29:37 pm »
This was already answered. ;)

window.draw(copHp); is called only when clickEnemy is true, it's no surprise that you can only catch a glimpse of it.

No it was commented on, the millisecond is my problem, and the fix is my question

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Drawing and undrawing text after collision and mouse pressed event
« Reply #7 on: August 11, 2012, 05:48:36 pm »
It was a comment 'cause there wasn't any clear question.


Do you use a sf::Clock to measure the elapsed time by frame? If not it's pretty easy, see the tutorial, especially the last piece of code.

Let's say you want to display copHp for 5 seconds:
  • Create a float variable to represent the elapsed time between NOW and the last time you clicked on your enemy. Set it to 5.
  • Each frame, add the elapsed time of that frame to your variable.
  • When you click your enemy, set your variable to 0.
  • Before your call to display(), if your variable is less than 5 then draw copHp

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Drawing and undrawing text after collision and mouse pressed event
« Reply #8 on: August 11, 2012, 05:56:39 pm »
G pointed out where the problem is and now it's your task to think about how to change it, we're not here to correct your code or write you new code, we're here to help you understand where you did your mistakes and how you can fix it. And as I implied earlier programming is about logic, you need quite a good potion of it, so you should practice it with more basic elements (at best in combination with a book). ;)
A further thing I pointed out is that you should keep the draw call seperated from the logic.

So with those two points in mind it should be fairly easy to write diffrent code.
Another (repeated) hint: Create a boolean for copHp and cop and if someone clicks set the boolean of copHp to true and the boolean of cop to false. Then at the drawing part you simply do:
if(booleanOfcopHp)
    window.draw(copHp);
if(booleanOfcop)
    window.draw(cop);
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything