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

2012/6/2

removing object help

seal308 seal308

2012/6/2

#
I need help removing an object after a certain amount of time. Here is my code for the object i want to remove.
public class Ready extends Actor
{
private int counter = 0;
private boolean exists = true;
    /**
     * Act - do whatever the Ready wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        System.out.println("counter nummber = " + counter);
        
        if (exists == true){
        
        if (counter < 20)
        {
            counter++;
        }
        else {
            getWorld().removeObject(this);
            exists=false;
        }
    }
    }    
}
I used system.out.println to see the value of the counter Here is the output with the terminal window
counter nummber = 16
counter nummber = 15
counter nummber = 14
counter nummber = 13
counter nummber = 12
counter nummber = 11
counter nummber = 10
counter nummber = 9
counter nummber = 8
counter nummber = 7
counter nummber = 6
counter nummber = 5
counter nummber = 4
counter nummber = 3
counter nummber = 2
counter nummber = 1
counter nummber = 0
273
273
340
340
275
361
271
363
This is a timer 55
counter nummber = 20
counter nummber = 19
counter nummber = 18
counter nummber = 17
counter nummber = 16
counter nummber = 15
counter nummber = 14
counter nummber = 13
counter nummber = 12
counter nummber = 11
counter nummber = 10
counter nummber = 9
counter nummber = 8
counter nummber = 7
counter nummber = 6
counter nummber = 5
counter nummber = 4
counter nummber = 3
counter nummber = 2
counter nummber = 1
counter nummber = 0
seal308 seal308

2012/6/2

#
i took note of the whole does not exist thing with the boolean exist. logic is that once counter is greater than 20 it will go straight to the else statement remove the object and set exist to false so the program won't repeat b/c it doesn't exist anymore but it still does not work.
seal308 seal308

2012/6/2

#
i took note of the whole does not exist thing with the boolean exist. logic is that once counter is greater than 20 it will go straight to the else statement remove the object and set exist to false so the program won't repeat b/c it doesn't exist anymore but it still does not work.
danpost danpost

2012/6/2

#
Did you re-compile? The print-out shows the counter counting down, but your code shows that it should be going up.
seal308 seal308

2012/6/3

#
yeah i pressed compile on both the code and the actual scenerio Not sure what's going on. Also quite greenfoot and opened it again. Still no dice, please help
seal308 seal308

2012/6/3

#
Also i think i should note that i'm calling this object from another object after a few seconds here is my code for calling it. Again calling it is not the problem, it's removing the object that's not working for me.
public class Rocket extends Actor
{
private int timer = 0;
private boolean initialTurn = false;
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        if (initialTurn == false)
        {
            turn (-90);
            initialTurn= true;
        }
        if (Greenfoot.isKeyDown("left"))
        {
            turn (-3);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turn (3);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            move(4);
            
        }
        
        System.out.println("This is a timer " + timer);
        
        
        if (timer < 5)
        {
            timer++;
        }
        else 
        {
            int x = (getWorld().getWidth())/2;
        int y = (getWorld().getHeight())/2;
        getWorld().addObject(new Ready(), x, y);
        timer++;
    }
danpost danpost

2012/6/3

#
OK, I think I see what is going on. The Ready object(s) are being removed! The problem is you are creating an abundance of them, one on top of another. So, when one is removed, you see a different one in the same spot. In your Rocket class, remove lines 40 and 41 (int x... and int y...), and change line 42 (getWorld().addObj...) to getWorld().addObject(new Ready(), getX(), getY() + getImage().getWidth() / 2); BTW, I can only guess as to what you want (cause 'Ready' does not really say much as to what this object is) and this is my guess.
seal308 seal308

2012/6/3

#
I'm sorry i think i should explain better what i'm trying to do. I have this rocket and it's going to do something. However i want there to be a counter so that the user gets ready to go. Ready is just an picture that says get ready. Right now i delayed the get ready sign to appear after the timer reaches 5. (see if else statement starting from line 34 in rocket class. Once the get ready sign appears/exists. There will be a counter that goes up. see line 17 of ready class. Once the counter reaches 20 (see line 19 to 21 of ready class) I want it to remove itself (this) from the world. The code you gave me sticks the sign on the middle of the rocket. I want the sign to be in the middle of the world. (that is why i did getWorld().getWidth... ) so the position i had was ok. It appeared at the centre of the world. From looking at your post you tell me that the ready object is being removed. But the problem is i'm making new one over the old ones. So i don't think my problem is position but really repetition of adding the object. So i thought the best way for me to fix this was to make a boolean called added. initially set it to false (b/c initially the sign is not there) Then in the else statment make it an else if statment So here is my code
public class Rocket extends Actor
{
private int timer = 0;
private boolean initialTurn = false;
private boolean added = false;
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        
        if (initialTurn == false)
        {
            turn (-90);
            initialTurn= true;
        }
        if (Greenfoot.isKeyDown("left"))
        {
            turn (-3);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            turn (3);
        }
        if (Greenfoot.isKeyDown("up"))
        {
            move(4);
            
        }
        
        System.out.println("This is a timer " + timer);
        
        
        if (timer < 5)
        {
            timer++;
        }
        else if (added == false)
        {
  
        getWorld().addObject(new Ready(), ((getWorld().getWidth())/2 ), (getWorld().getHeight())/2);
        timer++;
        added = true;
    }   
    }    
}
And so now it works!!!! Thanks for all your help
danpost danpost

2012/6/3

#
That would have been my next suggestion after your clarification. Glad you worked it out :+) But let's clean it up a little (there is no need to keep counting after the Ready object is added to the world). Replace lines 35 through 44 with the following
if (!added) // this check should be first (no need to continue if 'added')
{
    if (++timer < 5) return;
    getWorld().addObject(new Ready(), getWorld().getWidth() / 2, getWorld().getHeight() / 2);
    added = true;
}
Line 3 in the above post is equivalent to:
timer++;
if (timer < 5) return;
bluewise bluewise

2012/6/3

#
i need a credit card checker. how do i go about this?
danpost danpost

2012/6/3

#
@bluewise, what does that have to do with this discussion? Please, in the future, just start a new discussion. Check out this scenario that comes with source code (you could have used the search bar with 'credit card checker' and easily have found it).
You need to login to post a reply.