This site requires JavaScript, please enable it in your browser!
Greenfoot back
rsdt
rsdt wrote ...

2011/6/7

How to make an invincibility code for a video game

rsdt rsdt

2011/6/7

#
Only for a short period of time in the beginning of the game. We want to make it so that the player is able to flash as a visual to indicate that it is invincible when they are killed and revived. We want to make a countdown with the invincibility code so that there's a countdown before the code is turned off starting from the moment they player is revived and is able to continue the game.
DonaldDuck DonaldDuck

2011/6/8

#
You will need to switch a boolean to true when the player dies. Have a method in your act() that runs when this boolean is true and it counts an integer (if x < 50 than x++, otherwise set the boolean to false and reset x to 0) Also you`ll need to modify your get_hurt() method to only hurt the player if your invinsible boolean is false. To make the image flash, you just need to toggle the transparency from 255 to 0 after a count... (setTransparency(255) WAIT FOR x TIME setTransparency(0)) I would write some code snippets for you, but my keyboard is acting out. For an example of this invinsibility code being used, and for some code snippet, check out my Super Mario game by clicking my name (DonaldDuck) to the left here. Hope this was helpful. Cheers.
rsdt rsdt

2011/6/8

#
this is what we have so far for out invincibility: the problem is that it goes into transparent mode and the blinking is defected, it does not blink fast enough or something might be wrong with it. public void invincible() { if ((!canGetHit)) { if (invincibleTimer <= 200) {getImage().setTransparency(200); invincibleTimer += 1; } else{ canGetHit = true; invincibleTimer = 0; getImage().setTransparency(0);
DonaldDuck DonaldDuck

2011/6/9

#
Here is a code snippet that should work for you. The player should be invinsible when invinsibleTimer != 0 and you should run this method once when you want the player to invinsibil-ize. This is untested code, but I think it should work.
private void invinsible_flash()
{
    do
    {
        for(int counter = 0; counter < 400; counter++)
        {
            // This will make sure the players image is full opaque when int counter is less than 201
            if(counter <= 200 && getImage().getTransparency() != 255)
            {
                getImage().setTransparency(255);
                invinsibleCounter++;
            }
            else if(counter > 200 && getImage().getTransparency() != 0)
            {
                getImage().setTransparency(0);
            }
        } // End for
    } // End do

    // The do loop will repeat the contents until the while term is met. When it has flashed 5 times, it will exit the loop.
    while(invinsibleTimer < 5)

    //Reset the invinsibleTimer.

    if(invinsibleTimer == 5)
        invinsibleTimer = 0;
}
davmac davmac

2011/6/9

#
@DonaldDuck, there is a lot of danger in posting untested code :) line 11 refers to "invisibleCounter", line 21 to "invisibleTimer". I'm sure they're meant to refer to the same thing. Also, you do all the flashing in a do/while loop, which means it not be done in parallel with the rest of the scenario continuing to run. This is probably not what's wanted. Also, because there is no repainting occurring, the image won't actually visibly flash. Finally, the speed of the "for" loop and the do/while loop will depend completely on the speed of the machine on which the code runs, and also it will be independent of the speed slider; use Greenfoot.delay() for a delay (and it also allows repainting, I think).
DonaldDuck DonaldDuck

2011/6/9

#
My mistake! I'm not good with for, do, or while loops yet. I thought it sounded convincing though :) Thanks for clearing that up davmac
You need to login to post a reply.