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

2016/12/6

i want my bullet to move in the direction i was facing at the time i shoot it

nathen nathen

2016/12/6

#
  public void act()
      {
       
        if (Greenfoot.isKeyDown("a") )
        {
            setRotation(270);
            setImage("bullet.png");
            setLocation(getX()-3,getY());
            moveLeft();
        }
        if (Greenfoot.isKeyDown("d") )
        {
            setRotation(90);
            setImage("bullet.png");
             setLocation(getX()+3,getY());
             moveRight();
        }
         if (Greenfoot.isKeyDown("w") )
        {
            setRotation(0);
            setImage("bullet.png");
            setLocation(getX(),getY()-3);
            moveUp();
        }
         if (Greenfoot.isKeyDown("s") )
        {
            setRotation(180);
            setImage("bullet.png");
            setLocation(getX(),getY()+3);
            moveDown();
        }
        if(getY()<5)
        {
            getWorld().removeObject(this);
       
        }
        
  }
Super_Hippo Super_Hippo

2016/12/6

#
In what class is this code? I am bit confused because you use the 'bullet' image there and you control it with the arrow keys. Usually it would look like this:
Bullet b = new Bullet(); //create a new bullet
getWorld().addObject(b, getX(), getY()); //add the bullet on the position of the shooting actor
b.setRotation(getRotation()); //set the rotation of the bullet to the rotation of the shooting actor
You need to login to post a reply.