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

2018/7/1

Wall that doesnt let Tank pass

Mirco420 Mirco420

2018/7/1

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


public class Mauer extends Actor
{
     public void act()
    { 
      berührtBullet();
      berührtPanzer();
      berührtTowerRocket();
    }   
    public void berührtBullet()
    {
        if(isTouching(Bullet.class))
       {
           removeTouching(Bullet.class);
           Greenfoot.playSound("MetalExplosion.wav");



           
        }
    }
    public void berührtTowerRocket()
    {
        if(isTouching(TowerRocket.class))
       {
           removeTouching(TowerRocket.class);
           /*Greenfoot.playSound("MetalExplosion.wav");*/



           
        }
    }
    public void berührtPanzer()
      {
          
        if(isTouching(Panzer.class))
        {
            removeTouching(Panzer.class);
        }
    }
}
This is my code at the moment. If the walls touch a tank the tank gets removed. How can I make a method to just let the tanks stop and not make them get passed the wall?
Super_Hippo Super_Hippo

2018/7/1

#
The wall shouldn't do anything. The tank (and the bullet) should check if it touches (they touch) the wall. Basically, what you need to do is: - move the tank - check if it touching a wall and if yes, move back to where it was
Mirco420 Mirco420

2018/7/1

#
But what is the code for that? It basically doesn't midn if I put the isTouching in Wall or Tank/Bullet?
danpost danpost

2018/7/1

#
Mirco420 wrote...
But what is the code for that?
For any change in position or rotation of the tank, you would (1) determine what change is desired; (2) make appropriate change; (3) test for collision; and (4) if collision, undo the change done by (2) above.
It basically doesn't midn if I put the isTouching in Wall or Tank/Bullet?
Collision between two object can be tested by either of the colliding objects. You should test with one or the other (not both). Normally, I would have main objects (main actor, enemies, etc) test for projectiles that hit them and have the projectiles only check for world edge and unaffected objects (walls, platforms, etc.)
Mirco420 Mirco420

2018/7/1

#
    private void checkCollision() 
    {
        Mauer m = (Mauer) getOneIntersectingObject(Mauer.class);
        if (m != null) {
           
        }
    }
what do I have to put in the if-clause?
danpost danpost

2018/7/1

#
Mirco420 wrote...
<< Code Omitted >> what do I have to put in the if-clause?
I will presume this is in your tank class. The collision code needs to be with your movement code. You will need to move it there and then post what you then have for help.
Mirco420 Mirco420

2018/7/1

#
import greenfoot.*; 
import java.util.List;

/**
 * This class defines a Tank.
 */
public class Panzer extends Actor 

{
   long time = System.currentTimeMillis();
   int HP = 4;

   
   
   public void act(){
       checkKeyPress();
       Destroyed();
    }
    
       public void Destroyed()
        {
        if(isTouching(TowerRocket.class))
       {
           removeTouching(TowerRocket.class);
           HP--;
           getWorld().removeObjects(getWorld().getObjects(Leben1.class)); 
        }
           if(HP == 2)
           {
                      
           getWorld().removeObjects(getWorld().getObjects(Leben2.class)); 
        }
           
        /*if (HP == 1)
        {       

           getWorld().removeObjects(getWorld().getObjects(Leben3.class));
           getWorld().removeObject(this);*/
          
        
    
    
        if (HP == 1)
        {
           getWorld().removeObjects(getWorld().getObjects(Leben3.class));
           
          
           World world = getWorld();
           world.removeObjects(world.getObjects(null));
    
           world.setBackground("game_over.png");
           
           Greenfoot.stop();                
          
    }
}
   public void checkKeyPress() {
         Mauer m = (Mauer) getOneIntersectingObject(Mauer.class);
    if (m != null) {
        
    }

        if (Greenfoot.isKeyDown("left")){
            
                move(-8);
            }
        
        
        if (Greenfoot.isKeyDown("right")){
            
                move(8);
            }
        
        
        if (Greenfoot.isKeyDown("up")){
            setLocation(getX(),(getY()-10));
        }
        
        if (Greenfoot.isKeyDown("down")){
            setLocation(getX(), (getY()+10));
        }
       
        if(Greenfoot.isKeyDown("space") )
        {
            long time1 = System.currentTimeMillis();
            if (time1 >= time + (500)){
               getWorld().addObject(new Bullet(getRotation()), getX()+115, getY()+5);
                
               time = time1;
            }
        }
    }
    
   
}
    
    


I dont quite know what to put in here now :S I have got my move with arrow etc.
danpost danpost

2018/7/1

#
Mirco420 wrote...
<< Code Omitted >> I dont quite know what to put in here now :S I have got my move with arrow etc.
The first part (and the majority of) your checkKeyPress method is your movement code -- that is where collision checking should be (after moving -- not before). The last little bit is for shooting -- not moving; so, the checkKeyPress method can be refactored:
private void checkKeyPress()
{
    moving();
    shooting();
}
As is, your movement code has four (initial) setLocation statements which would then require four sets of collision checking. Before moving in any direction, take the state of all the arrow keys into account:
int dirX = 0; // for horizontal direction
if (Greenfoot.isKeyDown("left")) dirX--;
if (Greenfoot.isKeyDown("right")) dirX++;
int dirY = 0; // for vertical direction
if (Greenfoot.isKeyDown("up")) dirY--;
if (Greenfoot.isKeyDown("down")) dirY++;
Now, only one initial move is needed and the move can be taken back, if needed. For example:
setLocation(getX()+dirX*10, getY()+dirY*10);
if (isTouching(Mauer.class)) setLocation(getX()-dirX*10, getY()-dirY*10);
Mirco420 Mirco420

2018/7/1

#
Vielen Dank! Habe noch eine Frage: ich will mehrere Level machen, aber weiß nicht genau wo ich public und wo private verwenden muss. Wenn ich eine neue Welt erstellen gibt es genau dieselben Methode etc. und wenn ich die Objekte, die ich am Anfang automatisch erscheinen lasse veränder (z.b. coords ) passiert trotzdem nichts. Mach ich da was falsch oder geht das nicht
danpost danpost

2018/7/2

#
Mirco420 wrote...
Habe noch eine Frage ...
English please -- and if this is a different issue, please start a new discussion thread and show related codes.
You need to login to post a reply.