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

2021/6/15

Problem with removing Objects

Turbo_Thorsten Turbo_Thorsten

2021/6/15

#
I want my Screens (nuke/freeze/fasterShooting) removed after the timer runs down. Why aren't they removed? Code below
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public static double enemieskilled;
    public static int b;
    int c;
    int timernuke;
    int timerfreeze;
    int timershooting;
    public void act() 
    {
        if (timernuke > 0)
        {
            timernuke--;
            if (timernuke == 0)
            {
                getWorld().removeObjects(getWorld().getObjects(NukeScreen.class));
            }
        }
        if (timerfreeze > 0)
        {
            timerfreeze--;
            if (timernuke == 0)
            {
                getWorld().removeObjects(getWorld().getObjects(FreezeScreen.class));
            }
        }
        if (timershooting > 0)
        {
            timershooting--;
            if (timershooting == 0)
            {
                getWorld().removeObjects(getWorld().getObjects(FasterShootingScreen.class));
            }
        }
        move (10);
        if (isTouching(Border.class))
        {
            removeTouching (Bacteria.class);  
         getWorld().removeObject(this);
        }
        else if (isTouching(Bacteria.class))
        {
         removeTouching (Bacteria.class);  
         getWorld().removeObject(this);
         enemieskilled++;
        }
        else if (isTouching(Virus.class))
        {
         removeTouching (Virus.class); 
         enemieskilled++;
         if(Greenfoot.getRandomNumber(100) < 100)
         {
             c = Greenfoot.getRandomNumber(3);
             switch(c)
             {
                 case 0:
                 nuke();
                 break;
                 case 1:
                 Player.fasterShooting();
                 fasterShootingScreen();
                 break;
                 case 2:
                 MyWorld.freezeTimer = MyWorld.freezeTimer + 200;
                 freezeScreen();
                }
         } 
         getWorld().removeObject(this);
        }
        else if (isAtEdge()) 
        {
         getWorld().removeObject(this);
        }
    }   
    public void nuke()
    {
    if (getWorld() != null)
        {
        getWorld().removeObjects(getWorld().getObjects(Bacteria.class));
        getWorld().removeObjects(getWorld().getObjects(Virus.class));
        NukeScreen nukescreen1 = new NukeScreen ();
        getWorld().addObject(nukescreen1, 350, 50);
        timernuke = timernuke + 50;
    }
    }
    public void freezeScreen()
    {
        FreezeScreen freezescreen1 = new FreezeScreen ();
        getWorld().addObject(freezescreen1, 450, 45);
        timerfreeze = timerfreeze + 50;
    }
    public void fasterShootingScreen()
    {
        FasterShootingScreen fastershooting = new FasterShootingScreen ();
        getWorld().addObject(fastershooting, 250, 50);
        timershooting = timershooting + 50;
    }
}
Roshan123 Roshan123

2021/6/15

#
//in act method
nuke();
freezeScreen();
fasterShootingScreen();
Paste it in act method
Turbo_Thorsten Turbo_Thorsten

2021/6/15

#
Roshan123 wrote...
//in act method
nuke();
freezeScreen();
fasterShootingScreen();
Paste it in act method
No the 3 Methods are executed in line 68-77. With my code, the screen object aren't removed that's my problem.
Gbasire Gbasire

2021/6/15

#
when you type
int timernuke;
int timerfreeze;
int timershooting;
it will start at 0, so your code will never be executed, because it will never be over 0
Turbo_Thorsten Turbo_Thorsten

2021/6/15

#
Gbasire wrote...
when you type
int timernuke;
int timerfreeze;
int timershooting;
it will start at 0, so your code will never be executed, because it will never be over 0
In line 95, 102 and 108, 50 is added to each variable. So that's not the problem
Gbasire Gbasire

2021/6/15

#
Ohhh, I got it hahahaha look at this piece of code :
 if (timernuke > 0)
        {
            timernuke--;
            if (timernuke == 0)
            {
                getWorld().removeObjects(getWorld().getObjects(NukeScreen.class));
            }
        }
your first line checks is if it is higher than 0, and your 4th line checks if it is exactly 0, so it cannot work. put something like 1 instead of 0 on the 4th line
Roshan123 Roshan123

2021/6/15

#
Turbo_Thorsten wrote...
Roshan123 wrote...
//in act method
nuke();
freezeScreen();
fasterShootingScreen();
Paste it in act method
No the 3 Methods are executed in line 68-77. With my code, the screen object aren't removed that's my problem.
Ohhh sorry. That's my bad
Turbo_Thorsten Turbo_Thorsten

2021/6/15

#
Gbasire wrote...
Ohhh, I got it hahahaha look at this piece of code :
 if (timernuke > 0)
        {
            timernuke--;
            if (timernuke == 0)
            {
                getWorld().removeObjects(getWorld().getObjects(NukeScreen.class));
            }
        }
your first line checks is if it is higher than 0, and your 4th line checks if it is exactly 0, so it cannot work. put something like 1 instead of 0 on the 4th line
No this should all work cause I use the exact same code for my faster Shooting (see below code). I think sth about line 28 (in the orgiginal posted code) isn't working.
if (timerA > 0)
        {
         timerA--; 
         if(timerA == 0)
         {
          k = 30;
         }
        }
danpost danpost

2021/6/15

#
Turbo_Thorsten wrote...
In line 95, 102 and 108, 50 is added to each variable.
Yes, but you then immediately remove the bullet from the world (line 69 calls nuke, line 80 removes bullet). So no more act methods are executed for it. Nuke timer would be best placed in NukeScreen class.. None of those timers should be in Bullet class.
Turbo_Thorsten Turbo_Thorsten

2021/6/15

#
danpost wrote...
Turbo_Thorsten wrote...
In line 95, 102 and 108, 50 is added to each variable.
Yes, but you then immediately remove the bullet from the world (line 69 calls nuke, line 80 removes bullet). So no more act methods are executed for it. Nuke timer would be best placed in NukeScreen class..
But how do I execute a method in NukeScreen from Bullet? And also: Is there a way to do that without static?
Turbo_Thorsten Turbo_Thorsten

2021/6/15

#
Found a Solution
You need to login to post a reply.