OK! i have a problem. I'm making a game with a character you control and theres monsters that move when you do and when you run into a monster i have it so you lose 1 life.

(iLife --; )

i set iLife equal to 4 at the start.

if iLife is 3 i have an image show of 3 full hearts and one empty

if its 2 theres 2 empty

1 1 empty you get the point.

my problem is this. when i collide with a monster iLife keeps subtracting it doesnt pause at all so you go from full health to dead instantly i need some kind of delay after it subtracts one from iLife but the delay cant stop the program so Sleep(); wont work please help if you need code or clarification let me know.

Recommended Answers

All 6 Replies

If you can't pause the program, perhaps you can spawn a child process and have THAT sleep. Or a thread. But I doubt the real solution is to sleep. The real problem, I would imagine, is that it subtracts more than once for a collision, so change it so it only subtracts once.

yea that too but i still am not sure how to do that im using SDL for the collision.

what would be really cool is, like in some games when you get hit by something youll go into a transparent state for a few seconds and you cant get hit in that time. thats kind of what i want

I imagine that how the collision is detected isn't as important as how it's handled. What I imagine you don't want is this...

bool collisionHasOccurred; // set true somewhere when monster and player share the same coordinates, however that is determined
while(true)
{
    // code
    if(collisionHasOccurred)
    {
        // subtract a life
    }
    // code
}

This might be better...

bool collisionHasOccurred
time_t lastCollisionTime;
while(true)
{
    // code
    if(collisionHasOccurred && (time(0) - lastCollisionTime > 5))
    {
        // subtract a life
        lastCollisionTime = time(0);
    }
    // code
}

You can now only lose a life at most every 5 seconds. Enough time to get un-collided however you want to do that.

I'd just like to say that i love you <3. lol that worked so perfectly. The first example was exactly what i was doing too. thanks a ton

>> I'd just like to say that i love you <3. lol that worked so perfectly. The first example was exactly what i was doing too. thanks a ton

I love you too. :* Glad it worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.