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

2017/3/24

Remove Object by Hitting "Fels"

salvo salvo

2017/3/24

#
Hello Everyone (PLZ HELP) my rocket is shooting a Bullet (ObjectA) " ProjektilGelb" and if this hits the Object B i would want that the Bullet (object A) disappears which methode should i Use and should I programm it in the Bullet or in the Object B here is the code from the bullet i think i should put the methode in this class right ?
public class ProjektilGelb extends Actor
{
    
    /**
     * Act - do whatever the ProjektilGelb wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()//ProjektilGelb tut was auch immer in der "act" Methode drin steht
    {
        moveEast();//bewegt sich nach Osten (rechts)
  
        if (atWorldEdge())  
        {  
             getWorld().removeObject(this);  
        }   
        
        
    }

        public boolean atWorldEdge()
             
    {  
        if(getX() == 100)  
            return true;  
        else 
            return false;  
    }  
    

   public void moveEast()//bewegung von ProjektilGelb wird deffiniert 
   {
        setLocation(getX()+1,getY());
   }
}
salvo salvo

2017/3/24

#
I NEED THE LEGEND DAAAAANPOST
danpost danpost

2017/3/24

#
Better would probably to place collision detection between ObjectA and ObjectB in the class of ObjectB.using 'isTouching' and 'removeTouching'.
salvo salvo

2017/3/25

#
ok thx so i should use public boolean isTouching if .... else and then ? how do i involve the removeTouching i would have used is Touching and then getWorld. removeObject(this)
salvo salvo

2017/3/25

#
so i would add this in ObjectB
public class Fels  extends Actor
{
    /**
     * Konstruktor der Klasse Fels.
     * Schreibe dort alle Befehle hinein, die im Zeitpunkt der
     * Erzeugung eines Felses ausgeführt werden soll.
     */
    public Fels() 
    { 
    }    
    
    /**
     * Act - do whatever the Fels wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveRandomly();//bewege dich zufällig in der Mitte des Spielfeldes (an der Stelle Y=50)
       
   
    }
    public void moveRandomly()// bewegt sich zufällig horizontal 
  
    {
        int zahl = Greenfoot.getRandomNumber(2);// Zufallszahl zwischen 0,1 zwei Zahlen soll ausgegeben werden; fängt von null an zu zählen
        
        if(zahl == 0) // bedeutet Vergleich
        { moveSouth();
        }
        if(zahl == 1) 
        { moveNorth();
        }
    }
    
   public void moveSouth()
    {
        setLocation(getX(),getY()+1); //die y-Koo wird eins größer
    }
   public void moveNorth()
    {
        setLocation(getX(),getY()-1);
    }
}
danpost danpost

2017/3/25

#
salvo wrote...
so i should use (1) public boolean isTouching (2) if .... else
You would use 'if' at least (I do not think you will need the 'else' part. The 'isTouching' would produce the boolean condition for the 'if'.
how do i involve the removeTouching
The 'removeTouching' would go in the block that is executed when the 'if' condition is 'true'.
i would have used is Touching and then getWorld. removeObject(this)
You could get that to work in the ObjectA class; but, there will be complications for two reasons: (1) the projectiles already remove themselves from the world when hitting an edge of the world and you would have to control the flow of execution for a second way to have them remove themselves; and (2) it is really ObjectB that is the cause of the projectile being removed from the world; so, the code should be in that class.
salvo salvo

2017/3/25

#
thanks DANPOST :D <3
salvo salvo

2017/3/25

#
hey dan could i ask you another question how do I control the bullets for example i don't want that my rocket shoot the bullets frequently but only every 2 seconds
You need to login to post a reply.