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

2016/11/11

Click and Shooter Game

dlamc dlamc

2016/11/11

#
i'm trying to create a shooter game, that involves a tank that shoots bullets. I have the code the get the bullet actor to spawn and move. Right now I have the bullet actor set to move in a certain direction based on the key pressed. The issue that I'm having is that when the tank changes direction so do the bullets. What code would help me achieve the goal of the keeping the bullet moving in one direction despite a direction change of the tank? Any suggestions ~Thanks. bullet actor
 int bSpeed = 10;
        move(bSpeed);
        if (Greenfoot.isKeyDown("up"))
        {
            setRotation(270);
        }
         if (Greenfoot.isKeyDown("down"))
        {
            setRotation(90);
        }
         if (Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
        }
         if (Greenfoot.isKeyDown("left"))
        {
            setRotation(180);
        }
        if (isAtEdge())
        {
            getWorld().removeObject(this);
        }
Tank Actor
if(Greenfoot.isKeyDown("Space"))
        {
            World w = getWorld();
  
                int x = (int)(this.getX());
                int y = (int)(this.getY());
            w.addObject(new Bullet(),x,y);
            
            
        }
danpost danpost

2016/11/11

#
If you do not want the bullets to change directions when an arrow key is pressed, then you should remove lines 3 through 18 in your bullet code above. It might work if those lines were in the constructor of the class; however, it would probably be best to set the rotation of the bullet when it is created. Unfortunately, you did not give enough Tank class code to allow anyone to help you fix it there.
You need to login to post a reply.