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

2018/6/30

Bullets following my gun

Mirco420 Mirco420

2018/6/30

#
Hey, I have following problem: I have got turrets which can shoot automatically. They can turn aswell but as soon as they turn, they bullets come out of the centre of the turret and not out of the gun anymore:D I would really appreciate it if anybody could tell me, how the bullets always come out of the gun and if there is any possibility to make this happen.
danpost danpost

2018/6/30

#
Mirco420 wrote...
I have got turrets which can shoot automatically. They can turn aswell but as soon as they turn, they bullets come out of the centre of the turret and not out of the gun anymore:D I would really appreciate it if anybody could tell me, how the bullets always come out of the gun and if there is any possibility to make this happen.
You will need to provide your turret and bullet classes (cannot fix unseen code).
Mirco420 Mirco420

2018/6/30

#
Tank
import greenfoot.*; 

/**
 * This class defines a Tank.
 */
public class Panzer extends Actor 
{
   long time = System.currentTimeMillis();
   int HP = 3;
   
   public void act(){
       checkKeyPress();
       Treffer();
       DestroyTank();
      
    }
      
    
   public void checkKeyPress () {
       
        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;
            }
        }
   }
   
   public void Treffer()
    { 
       if(isTouching(TowerRocket.class))
       {
           removeTouching(TowerRocket.class);
           HP--;
        }
    }
    
    public void DestroyTank(){
        
       if(HP == 0)
       {
           /*Greenfoot.setWorld (new Tankworld1());*/
           getWorld().removeObject(this);
           
           
        }
    }
    
}
   
Bullet
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Bullet extends Actor
{
      
    
    private int direction, speed;
    public Bullet(int dir)
    {
        direction = dir;
        speed = 15;
    }
    public void act()
    {
        setRotation(direction);
        move(speed);
        berührtRand();
    }
    
     
    
    public void berührtRand()
    {
        
       if (isAtEdge())
       {
          getWorld().removeObject(this);

       
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Base here. * * @author (your name) * @version (a version number or a date) */ public class Tower extends Actor { int HP=3; long time = System.currentTimeMillis(); public void act() { automaticMovement(); if(isTouching(Bullet.class)) { removeTouching(Bullet.class); HP--; } if(HP == 0) { setImage("explosion.png"); Greenfoot.delay (30); getWorld().removeObject(this); Greenfoot.playSound("Explosion.wav"); } {long time1 = System.currentTimeMillis(); if (time1 >= time + (500)){ getWorld().addObject(new TowerRocket(getRotation()-20), getX()-80, getY()); time = time1; } } } public void automaticMovement() { Panzer P = getObjectsInRange (9999, Panzer.class).get(0); turnTowards (P.getX(), P.getY()); turn(200); } }
Mirco420 Mirco420

2018/6/30

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

/**
 * Write a description of class Base here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Tower extends Actor
{
    int HP=3;
    long time = System.currentTimeMillis();
      

    public void act()
    { 
       automaticMovement();
       
       if(isTouching(Bullet.class))
       {
           removeTouching(Bullet.class);
           HP--;
        }
       if(HP == 0)
       {
           setImage("explosion.png");
           Greenfoot.delay (30);
           getWorld().removeObject(this);
           Greenfoot.playSound("Explosion.wav");
        }
       {long time1 = System.currentTimeMillis(); 
            if (time1 >= time + (500)){
               getWorld().addObject(new TowerRocket(getRotation()-20), getX()-80, getY());
                time = time1;
            }
        }
    }
    
    public void automaticMovement()
    {
       Panzer P = getObjectsInRange (9999, Panzer.class).get(0);
       turnTowards (P.getX(), P.getY());
       turn(200);
       
}
}
danpost danpost

2018/6/30

#
Mirco420 wrote...
I have got turrets which can shoot automatically. They can turn aswell but as soon as they turn, they bullets come out of the centre of the turret and not out of the gun anymore:D
In the Tower class, remove the "-80" from line 33 and insert at line 34 the following:
move(80);
Do similarly in the Panzer class, at line 45.
You need to login to post a reply.