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

2019/1/21

setSpeed() for a single Actor

coreXtream coreXtream

2019/1/21

#
Hey guys, I was wondering if you can help me with my problem. I read a lot of different posts but what they get solved is quite different from my problem. I want to increase the speed of a single actor. I want to let a bullet fly faster than the object shooting it, but I don't want the bullet to skip a cell
public class Bullet extends Actor
{
    private int speed;    
    public Bullet(int rotation)
    {
        setImage("images/bullet.png");
        setRotation(rotation);
        speed = 1;
    }

    public void act() 
    {
        if(außerReichweite())
        {
            getWorld().removeObject(this);        
        }
        move(speed);
    } 
    
    public boolean außerReichweite()
    {
        if(this.getX() >= 20 || this.getY() >= 13 || this.getX() == 0 || this.getY() == 0)
        {
            return true;
        }
        return false;
    }
    
}
public class Rover extends Actor
{
    int Ammo = 10;
    int reloadDelay = 20;
    
    public void act() 
    {
           shoot();
           tastatur();
    } 

    public void tastatur()
    {
        if(Greenfoot.isKeyDown("left"))
            {
                this.setRotation(180);
                move(1);
            } else if(Greenfoot.isKeyDown("up"))
            {
                this.setRotation(270);
                move(1);
            } else if(Greenfoot.isKeyDown("right"))
            {
                this.setRotation(0);
                move(1);
            } else if(Greenfoot.isKeyDown("down") )
            {
                this.setRotation(90);
                move(1);
            }
    }

   
    public void shoot()
    {
        if (Greenfoot.isKeyDown("0") && Ammo > 0)
       {
           getWorld().addObject(new Bullet(getRotation()), xPos(), yPos());
           Ammo--;
       }
       if (Ammo <= 0)
       {
           reloadDelay--;
           if (reloadDelay <= 0)
           {
               Ammo = 10;
               reloadDelay = 20;
            }
       }
    }   
    
    
    public int xPos()
    {
        if(getRotation()==0)
        {
            return getX()+1;
        } else if(getRotation()==180)
        {
            return getX()-1;
        }
        return getX();
    }
    
    public int yPos()
    {
        if(getRotation()==90)
        {
            return getY()+1;
        } else if(getRotation()==270)
        {
            return getY()-1;
        }
        return getY();
    }
    
    
}
danpost danpost

2019/1/21

#
coreXtream wrote...
I want to let a bullet fly faster than the object shooting it, but I don't want the bullet to skip a cell << Code Omitted >>
I think I understand your issue. You are working in a grid world of size (21 x 14) and you do not want the bullet to skip over any actor as it might when you increase its speed. You will probably have to move collision checking into the bullet class so that you can make two "moves" (with collision checking after each) at one time. You could check for each type object it could hit and then call a method on hit object to perform resultant actions. The in-world state of the bullet will then need to be checked before trying the move to the second cell (unless it is known that any object hit will cause the bullet to be removed. Speaking of in-world state, as the bullet act method is coded above, you will get an IllegalStateException (actor not in world) at line 17 when a bullet reaches an edge of the world. Another concern is what happens when the rover fires a bullet along an edge. The bullet would be immediately removed from the world. I doubt this is wanted. There are also some improvements you can make in your code. Overall, I feel it might be best for you to go to a pixel cell sized world. There would be no "jumping" from one cell to the next, meaning things would move more smoothly; you could easily have the bullets move faster than the rover without skipping over anything; and edges will be contacted by a bullet only after that bullet moves toward the edge.
danpost danpost

2019/1/22

#
I guess the other possibility is to double the width and height of the world and halve the cell size. Then, without changing the size of the images, you can have the bullet move faster. Actually from 21 x 14, you can go to 43 x 29, place and allow turning only on odd valued x- and y-coordinates so as not to really change anything other than non-bullet objects "half-stepping" it. This would also solve the bullet/edge issue. Restrict the rover and other non-bullet objects to x >= 1, y >= 1, x <= world width -2, and y <= world height - 2.
You need to login to post a reply.