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

2014/7/17

How do I delete one of many actors?

1
2
3
NikZ NikZ

2014/7/17

#
Integer.toString(value) should be "right" on line 2, sorry.
danpost danpost

2014/7/17

#
NikZ wrote...
< Quote Omitted > < Code Omitted > This might work--correct me if I'm wrong.
NikZ wrote...
Integer.toString(value) should be "right" on line 2, sorry.
That will pause the whole scenario until the key is released.
Evil_Lan Evil_Lan

2014/7/18

#
erdelf wrote...
ok, maybe u should change the location of this method, i think the world class is a much more suitable class for that
if(Greenfoot.isKeyDown("right") == true)
{
     List blueDots = getObjects(BlueDot.class);
     if(!blueDots.isEmpty())
     {
        Actor dot = blueDots.get(1);
        for(int i = 0; i<blueDots.size(); i++)
        {
          Actor tempDot = blueDots.get(i);
          if(tempDot.getY()>dot)
          {
               dot = tempDot;
          }
        }
        removeObject(dot);
     }
 }
Okay, I put this in my world.class file and changed the Line 10 to if (tempDot.getY() > dot.getY()) like danpost said. When I try to compile it a window opens up saying; size() in java.awt.Component has been deprecated. (I imported the java.awt.List.
Evil_Lan Evil_Lan

2014/7/18

#
danpost wrote...
Line 10 of the code erdelf gave should be 'if (tempDot.getY() > dot.getY())'. Also, using 'isKeyDown' without any control will cause multiple dots to be removed (one per act, as long as the key is down). If you want only one to be removed per keystroke (press AND release of the key), then use 'getKey' instead of 'isKeyDown'. This can be coded to work for both keys/classes by using the following in your World subclass act method (or a method it calls):
Class clss = null;
String key = Greenfoot.getKey();
if ("left".equals(key)) clss = BlueDot.class;
if ("right".equals(key)) clss = RedDot.class;
if (clss != null)
{
    Actor dot = null;
    for (Object obj : getObjects(clss)) if (dot == null || dot.getY() < ((Actor)obj).getY()) dot = (Actor)obj;
    if (dot != null) getWorld().removeObject(dot);
}
and there is no need to import the List class.
I would like it to remove only 1 dot every time I press the Right/Left arrow key. Im not sure where I should put your code. I put this code in the world.class and I get an error here (Line 9) : if (dot != null) getWorld().removeObject(dot);
erdelf erdelf

2014/7/18

#
the method is in the world class and the getWorld() part has to be removed, just
if(dot != null) removeObject(dot);
and the size method i used is in the java.util.List if u need the java.awt.List for some reason in this class change line 3 to
java.util.List blueDots = getObjects(BlueDot.class);  
i think this works
Evil_Lan Evil_Lan

2014/7/18

#
erdelf wrote...
the method is in the world class and the getWorld() part has to be removed, just
if(dot != null) removeObject(dot);
and the size method i used is in the java.util.List if u need the java.awt.List for some reason in this class change line 3 to
java.util.List blueDots = getObjects(BlueDot.class);  
i think this works
I changed danpost's line to 'if(dot != null) removeObject(dot);' But what do I do for this: "If there is a blue dot falling then a red dot." I dont want to be able to remove the red dot before the blue dot. I would like it to say "Game over" if the player presses the wrong arrow for the wrong color of the dot with the greatest Y.
erdelf erdelf

2014/7/18

#
ok, lets say the dots are subclasses of the dot class
    Class clss = null;  
    String key = Greenfoot.getKey();  
    if ("left".equals(key)) clss = BlueDot.class;  
    if ("right".equals(key)) clss = RedDot.class;  
    if (clss != null)  
    {  
        Actor dot = null;  
        for (Object obj : getObjects(Dot.class)) if (dot == null || dot.getY() < ((Actor)obj).getY()) dot = (Actor)obj;  
        if (dot != null) 
        {
             if(dot instanceof clss)
                   removeObject(dot);  
             else
                   gameOver(); // write here, how the gameover is supposed to be visualized
        }
    }  
not quite sure, but i think this works
Evil_Lan Evil_Lan

2014/7/18

#
erdelf wrote...
ok, lets say the dots are subclasses of the dot class
    Class clss = null;  
    String key = Greenfoot.getKey();  
    if ("left".equals(key)) clss = BlueDot.class;  
    if ("right".equals(key)) clss = RedDot.class;  
    if (clss != null)  
    {  
        Actor dot = null;  
        for (Object obj : getObjects(Dot.class)) if (dot == null || dot.getY() < ((Actor)obj).getY()) dot = (Actor)obj;  
        if (dot != null) 
        {
             if(dot instanceof clss)
                   removeObject(dot);  
             else
                   gameOver(); // write here, how the gameover is supposed to be visualized
        }
    }  
not quite sure, but i think this works
My red and blue dots are not sub classes, they are actors. So is this correct? : (This code is in my world.class and in a public void act())
        Class clss = null;    
        String key = Greenfoot.getKey();    
        if ("left".equals(key)) clss = BlueDot.class;    
        if ("right".equals(key)) clss = RedDot.class;    
        if (clss != null)    
        {    
            Actor dot = null;    //This is for the blue dot
            for (Object obj : getObjects(BlueDot.class)) if (dot == null || dot.getY() < ((Actor)obj).getY()) dot = (Actor)obj; 
                        
            if (dot != null)   
            {  
                if(dot instanceof clss) {  
                        removeObject(dot);
                        
                   }else {                                                              
                        Greenfoot.stop();
                        System.out.println("GAME OVER!");                                                                             
                }
                
            } 
            
            Actor dot2 = null;  //This is for the red dot
            for (Object obj2 : getObjects(RedDot.class)) if (dot2 == null || dot2.getY() < ((Actor)obj2).getY()) dot2 = (Actor)obj2; 
            
            if (dot2 != null)   
            {  
                if(dot2 instanceof clss) {  
                        removeObject(dot2);
                        
                   }else {
                        Greenfoot.stop();
                        System.out.println("GAME OVER!");
                }  
                
            } 
            
        }
For now I kept it simple. I told it to just stop the game with the 'Greenfoot.stop()' and print 'Game Over'. When I try to compile this, Greenfoot highlights the 'clss' in red from the line 'if(dot instanceof clss)'
danpost danpost

2014/7/18

#
You cannot use 'instanceof' on a Class, only on a type. That is:
 // this is not allowed
if (actor instanceof RedDot.class) // trying to use a class
// where this is allowed
if (actor instanceof RedDot) // using a type
Considering the context, I believe that your dots will all be falling at the same rate of speed. If you only have RedDot and BlueDot objects in your world, then the following code would work:
String key = Greenfoot.getKey();
if (key != null && !getObjects(null).isEmpty())
{
    Object obj = getObjects(null).get(0);
    if (("right".equals(key) && obj instanceof RedDot) || ("left".equals(key) && obj instanceof BlueDot))
        removeObject((Actor)obj);
    else
        Greenfoot.stop();
}
If you have other objects in the world, then if you have an intermediate class between Actor and both your RedDot and BlueDot classes (maybe called 'Dot'), you can replace 'null' with 'Dot.class' in the 'getObjects' calls and have it work.
NikZ NikZ

2014/7/18

#
You can also use removeTouching()
Evil_Lan Evil_Lan

2014/7/18

#
danpost wrote...
You cannot use 'instanceof' on a Class, only on a type. That is:
 // this is not allowed
if (actor instanceof RedDot.class) // trying to use a class
// where this is allowed
if (actor instanceof RedDot) // using a type
Considering the context, I believe that your dots will all be falling at the same rate of speed. If you only have RedDot and BlueDot objects in your world, then the following code would work:
String key = Greenfoot.getKey();
if (key != null && !getObjects(null).isEmpty())
{
    Object obj = getObjects(null).get(0);
    if (("right".equals(key) && obj instanceof RedDot) || ("left".equals(key) && obj instanceof BlueDot))
        removeObject((Actor)obj);
    else
        Greenfoot.stop();
}
If you have other objects in the world, then if you have an intermediate class between Actor and both your RedDot and BlueDot classes (maybe called 'Dot'), you can replace 'null' with 'Dot.class' in the 'getObjects' calls and have it work.
Okay, this is weird... I did what you said:
        String key = Greenfoot.getKey();  
        if (key != null && !getObjects(null).isEmpty())  
        {  
            Object obj = getObjects(null).get(0);  
            if (("right".equals(key) && obj instanceof RedDot) || ("left".equals(key) && obj instanceof BlueDot))  
            removeObject((Actor)obj);  
            else  
            Greenfoot.stop(); 
            //This is the weird part...
            System.out.println("GAME OVER! Wrong KEY PRESSED!");
        }  
Ok so it works great and all, it removes only one of the falling dots and if the player presses the wrong key it displays 'game over' and stops the game. But the thing is in the Greenfoot Terminal Window, "GAME OVER! Wrong KEY PRESSED!" appears even though the correct arrow key is pressed and does not stop the game. It only prints "GAME OVER! Wrong KEY PRESSED!". Why?
erdelf erdelf

2014/7/19

#
you have to add brackets before line 8 and after line 10
Evil_Lan Evil_Lan

2014/7/19

#
erdelf wrote...
you have to add brackets before line 8 and after line 10
OMG Thank you so much everyone! it finally works just the way I want it to work. Thousand thanks!
Evil_Lan Evil_Lan

2014/7/19

#
danpost wrote...
You cannot use 'instanceof' on a Class, only on a type. That is:
 // this is not allowed
if (actor instanceof RedDot.class) // trying to use a class
// where this is allowed
if (actor instanceof RedDot) // using a type
Considering the context, I believe that your dots will all be falling at the same rate of speed. If you only have RedDot and BlueDot objects in your world, then the following code would work:
String key = Greenfoot.getKey();
if (key != null && !getObjects(null).isEmpty())
{
    Object obj = getObjects(null).get(0);
    if (("right".equals(key) && obj instanceof RedDot) || ("left".equals(key) && obj instanceof BlueDot))
        removeObject((Actor)obj);
    else
        Greenfoot.stop();
}
If you have other objects in the world, then if you have an intermediate class between Actor and both your RedDot and BlueDot classes (maybe called 'Dot'), you can replace 'null' with 'Dot.class' in the 'getObjects' calls and have it work.
I have a Score.class which adds 1 point every time I remove a dot. And I will probably add more actors into my game will this be an issue?
int score = 0;   //This counter is for the score
   
   public World()
   {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels. (true/false) is to make the game UNBOUNDED
        super(400, 600, 1, false); 
        
        Score score = new Score();
        addObject (score, 200, 25);
   }
I replaced the 'null' by Score.class but it just stops the game when I press Right/Left
        String key = Greenfoot.getKey();  
        if (key != null && !getObjects(Score.class).isEmpty())  
        {  
            Object obj = getObjects(Score.class).get(0);  
            if (("right".equals(key) && obj instanceof RedDot) || ("left".equals(key) && obj instanceof BlueDot)) { 
            removeObject((Actor)obj);  
            score++;
            System.out.println(score);
           }else  {
            Greenfoot.stop(); 
            System.out.println("GAME OVER! Wrong KEY PRESSED!");
         }
        }  
danpost danpost

2014/7/19

#
NikZ wrote...
You can also use removeTouching()
There is no 'touching' going on here. 'removeTouching' has no place within the required code.
Evil_Lan wrote...
< Quote Omitted > Okay, this is weird... I did what you said: < Code Omitted > Ok so it works great and all, it removes only one of the falling dots and if the player presses the wrong key it displays 'game over' and stops the game. But the thing is in the Greenfoot Terminal Window, "GAME OVER! Wrong KEY PRESSED!" appears even though the correct arrow key is pressed and does not stop the game. It only prints "GAME OVER! Wrong KEY PRESSED!". Why?
Just to clear things up -- 'if', 'while', 'do', 'for' or 'else' statements are expected to have something executable after them. It can be a single statement (not enclosed in curly brackets) or a block (enclosed in curly brackets). Whichever it is, it is the first thing after the 'if', 'while', 'do' 'for' or 'else' statement. Line 10 is the second thing after the 'else' statement and therefore is not considered within the 'if-else' part of the code and, hence, executed only on the condition of line 2 (line 5 has no bearing on its execution). As far as a single statement is concerned, a simple semi-colon, ';', concludes an empty statement. That is, with this
if (getX() == 0);
any following code is executed without regard to the condition of the statement.
There are more replies on the next page.
1
2
3