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

2018/3/19

Starting timer if boolean is true?

Recorsi Recorsi

2018/3/19

#
Hello, I tried to add a timer that does nothing if the boolean is false but activates if the boolean is true. But it doesn't work. This is the code i've used:
private boolean startedResetTimer = false;
private int resetTimer = 100; 
public void hitEnemy()
  {
      if (isTouching(Enemy.class))
      {
         explode2();
         startedResetTimer = true; 
         System.out.println("hit");
      }//else startedResetTimer = false;
      if (startedResetTimer == true)
      {
          System.out.println("timer started");
          resetTimer--;
          System.out.println(resetTimer);
      }
      if (resetTimer <= 0)
      {
         Greenfoot.setWorld(new Spielfeld());
      }
  }
The timer is set to 100 and it should start substracting till it reaches 0, and then reset the world. But it only subracts one. I think i don't really know how booleans work yet :D how do i fix my code? Thanks.
danpost danpost

2018/3/19

#
You cannot expect the act method to continue to call the hitEmeny method once the actor is removed from the world. Maybe the reset timer should be in your Speilfeld class.
Recorsi Recorsi

2018/3/19

#
Thanks, i added this code to my "Spielfeld" but now it resets every 2 seconds. Am i missing something?
private boolean startedResetTimer = false;
private int resetTimer = 100; 
public void checkSpaceship()
    {
        if (!getObjects(Spaceship.class).isEmpty())
        {
            //Continue
        }
        else 
        {
            startedResetTimer = true;
        }
        if (startedResetTimer = true)
        {
            resetTimer--;
        }
        if (resetTimer <= 0)
        {
            Greenfoot.setWorld(new Spielfeld());
        }
    }
danpost danpost

2018/3/19

#
Replace line 3 through 7 with the following:
if (!startedResetTimer && getObjects(Spaceship.class).isEmpty())
Then move line 14 to around line 18 (moving the last if block inside the previous one). Report back with results.
Recorsi Recorsi

2018/3/19

#
The code now looks like this:
public void checkSpaceship()
    {
        if (!startedResetTimer && getObjects(Spaceship.class).isEmpty())
        {
            startedResetTimer = true;
        }
        if (startedResetTimer = true)
        {
            resetTimer--;
            if (resetTimer <= 0)
            {
                Greenfoot.setWorld(new Spielfeld());
            }
        }
    }
But it still resets the game every second Its correct that i call checkspaceship in the act method, right?
danpost danpost

2018/3/19

#
Recorsi wrote...
Its correct that i call the checkspaceship in the act method, right?
Yes -- that is fine that it is called from the act method. Increase the initial value of the resetTimer field. A hundred act cycles will normally execute in less than 2 seconds. If n is the number of seconds you wish to allow, then 60*n would be an appropriate initial value.
Recorsi Recorsi

2018/3/19

#
Ok nice, but the problem is that the world gets resetted even if the ship is still in the world.
danpost danpost

2018/3/19

#
Recorsi wrote...
Ok nice, but the problem is that the world gets resetted even if the ship is still in the world.
Alright, show the entire class code.
Recorsi Recorsi

2018/3/20

#
Here it is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The world.
 * 
 * @author Julian 
 * @version 23.09.17
 */
public class Spielfeld extends World
{
    Spaceship spaceship = new Spaceship();
    Rock rock = new Rock();
    GifImage background = new GifImage("background.gif");
    HitCounter hitcounter = new HitCounter();
    BoostBar boostbar = new BoostBar();
    private boolean startedResetTimer = false;
    private int resetTimer = 60*5; 
    public Spielfeld()
    {    
      super(900, 900, 1,false); 
      addShip();
      placeRocksBeginning();
      prepareHitCounter();
      FPS();
      addObject(boostbar, 90, 850);
      setPaintOrder(DeathScreen.class,ResetInfo.class,BoostBar.class,HitCounter.class,FPS.class,explosion.class,Enemy.class,Rock.class, Spaceship.class, Cloud1.class, Cloud2.class);
    }
    public void act()
    {
       placeRocks();  
       setBackground(background.getCurrentImage());
       placeClouds();
       placeEnemies(); 
       resetGame();
       resetShip();
       //checkSpaceship();
    }
    public void checkSpaceship()
    {
        if (!startedResetTimer && getObjects(Spaceship.class).isEmpty())
        {
            startedResetTimer = true;
        }
        if (startedResetTimer = true)
        {
            resetTimer--;
            if (resetTimer <= 0)
            {
                Greenfoot.setWorld(new Spielfeld());
            }
        }
    }
    public BoostBar getBoostBar()
    {
        return boostbar;
    }
    public HitCounter getHitCounter()
    {
       return hitcounter; 
    }
    public void placeClouds()
    {  
        if(Math.random()<0.01)
            {
                if(Math.random()<0.5)
                {
                    addObject(new Cloud1(), Greenfoot.getRandomNumber(getWidth()), getHeight()/getHeight());
                }
                else
                {
                    addObject(new Cloud2(), Greenfoot.getRandomNumber(getWidth()), getHeight()/getHeight());
                }
            }
    }
    private void prepareHitCounter()
    {
       addObject(hitcounter, 840, 880); 
    }
    public void placeRocksBeginning()
    {
       for (int i = 0; i < 25; i++) 
       {
          int x = Greenfoot.getRandomNumber(900);
          int y = Greenfoot.getRandomNumber(900);
          addObject (new Rock(), x , y);
          rock.getRotation();
          rock.setRotation(Greenfoot.getRandomNumber(260));
       }
    } 
    public void placeRocks()
    {
      {
        if(Greenfoot.getRandomNumber(70) == 1)
        {
          int x = Greenfoot.getRandomNumber(900);
          int y = Greenfoot.getRandomNumber(900);
          addObject(new Rock(), x, y);
          rock.setRotation(Greenfoot.getRandomNumber(260));
        }
      } 
    }
    public void placeEnemies()
    {
       if(Greenfoot.getRandomNumber(200) == 1)
       {
          int y = Greenfoot.getRandomNumber(900);
          addObject(new Enemy(), 0, y);
       }
    } 
    public void FPS() //Author: Michael Berry
    {
      FPS fps = new FPS();
      addObject(fps, 24, 19);
      fps.setLocation(54, 10);
    }
    private void addShip()
    {
       addObject(spaceship,450,865);
       spaceship.setRotation(90); 
    }
    public void resetShip()
    {
       if (Greenfoot.isKeyDown("E"))
       {
          removeObject(spaceship);
          addObject(spaceship,450,865);
       }
    }
    public void resetGame()
    {
        GreenfootSound GameMusic = Startscreen.GameMusic;
        GreenfootSound BG = Startscreen.BG;
        if (Greenfoot.isKeyDown("escape"))
        {
           GameMusic.stop();
           BG.playLoop();
           Greenfoot.setWorld(new Startscreen());
        }
    }
}
danpost danpost

2018/3/20

#
Line 44 sets and does not compare.
Recorsi Recorsi

2018/3/21

#
danpost wrote...
Line 44 sets and does not compare.
Sorry i don't really know what you mean with that
Super_Hippo Super_Hippo

2018/3/21

#
You need to use == instead of = or remove the "= true" completely.
Recorsi Recorsi

2018/3/21

#
Thanks danpost and hippo :) Just to make it clear: if (boolean) is the same thing as ==true and if (!boolean) is the same thing as ==false, right?
danpost danpost

2018/3/21

#
Recorsi wrote...
Just to make it clear: if (boolean) is the same thing as ==true and if (!boolean) is the same thing as ==false, right?
I believe you got it right. if (!boolean) is also the same as != true.
You need to login to post a reply.