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

2021/10/25

How to showText for a seconds and remove it after few seconds

loremipsum loremipsum

2021/10/25

#
i try to show stage 1 text, and remove it after a few seconds ex. for 5 seconds, how can i do with my code?, ive try with if else but i know text dissapear only if i press the keydown.
if(Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("a"))
        {
            showText("",400,100);
        }
        else 
        {
            showText("STAGE 1",400,100);
        }
ive try to add delay but that also delaying the entire program
showText("STAGE 1",400,100);
Greenfoot.delay(100);
Gbasire Gbasire

2021/10/25

#
you can try to add some timer variable :
int timer;
public void act()
{
    timer--;
    if(condition) //anything
    {
        showText("STAGE 1",400,100);
        timer = 50;
        condition = false;
    }
    if(timer == 1)
    {
        showText("",400,100);
    }
}
this will check if the timer variable is equal to 1, and as it starts at 0 and goes -1 every act turn, it will never be equal to 1 unless the condition is true. If the condition is true, it will set the timer to 50, and the timer will go -1 every turn, going to 1 after 50 act turns and yes, don't use Greenfoot.delay(), it is not very useful.
loremipsum loremipsum

2021/10/25

#
wow thanks it works!, but i have new problem, the text keep showing after the condition is true, and i wanted to make the text is shown just 1 times after the condition is true. how can i do with my code
int = timer;
boolean condition = Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("a");
public void act()
    {    
        timer--;
        if(Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("a"))
        {
            showText("STAGE 1",400,100);
            timer = 50;
            condition = false;
        }
        if(timer == 1)
        {
            showText("",400,100);
        }
Gbasire Gbasire

2021/10/25

#
you forgot to put if(condition) haha, it never gets false if you keep pressing d or a :)
int = timer;
boolean condition = Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("a");
public void act()
    {    
        timer--;
        if(condition) //just put condition here
        {
            showText("STAGE 1",400,100);
            timer = 50;
            condition = false;
        }
        if(timer == 1)
        {
            showText("",400,100);
        }
loremipsum loremipsum

2021/10/25

#
if i put if(condition), why the text wont show XD?, here my code
int timer;
    boolean condition = Greenfoot.isKeyDown("d") || Greenfoot.isKeyDown("a");
    public void act()
    {    
        timer--;
        if(condition)
        {
            showText("STAGE 1",400,100);
            timer = 50;
            condition = false;
        }
        if(timer == 1)
        {
            showText("",400,100);
        }
Gbasire Gbasire

2021/10/25

#
yeah, "condition" is only checked at the creation of the world, that's why it doesn't work. try this instead :
int timer;
boolean condition = true;
public void act()
{
    timer--;
    if(condition && (Greenfoot.isKeyDown("a") || Greenfoot.isKeyDown("d"))) //if a key is used, it will work once and set condition to false
    {
        showText("STAGE 1",400,100);
        timer = 50;
        condition = false;
    }
    if(timer == 1)
    {
        showText("",400,100);
    }
loremipsum loremipsum

2021/10/25

#
Thanks Gbasire :), working well, i just use condition true, not any condition isKeyDown like before, but i think thats ok. here my code
int timer;
    boolean condition = true;
    public void act()
    {    
        timer--;
        if(condition)
        {
            showText("STAGE 1",400,100);
            timer = 100;
            condition = false;
        }
        if(timer == 1)
        {
            showText("",400,100);
        }
Thank you very much.
Gbasire Gbasire

2021/10/25

#
No problem ! If you use this code, you just won't have any key to press, it will directly show the text. By the ways, for what type of game/scenario are you doing this ?
loremipsum loremipsum

2021/10/25

#
iam newbie, and i do it for my simple game haha
Gbasire Gbasire

2021/10/25

#
Ok, good luck then ! If you want to look at more complete code of games, all my games are open source, you can go to the games on my profile, download the code and look at it if you're interested !
danpost danpost

2021/10/25

#
The delay was only delaying the entire program above because the stage was being shown repeatedly/continually. The following will only show the stage once, delaying only while the stage is being shown:
boolean stageShown = false;

public void act()
{
    if (! stageShown)
    {
        showText("STAGE 1", 400, 100);
        Greenfoot.delay(200);
        showText("", 400, 100);
        stageShown = true;
    }
    ...
}
loremipsum loremipsum

2021/10/25

#
danpost wrote...
The delay was only delaying the entire program above because the stage was being shown repeatedly/continually. The following will only show the stage once, delaying only while the stage is being shown:
boolean stageShown = false;

public void act()
{
    if (! stageShown)
    {
        showText("STAGE 1", 400, 100);
        Greenfoot.delay(200);
        showText("", 400, 100);
        stageShown = true;
    }
    ...
}
woah Thankyou so much, finally got it hehe Thanks!.
You need to login to post a reply.