I am trying to fire bullets from the location of the gun that is firing. So when the mouse is moved the gun moves at a direction. I would like the bullet to move at the direction which the gun is pointing to. So i can fire at any direction.
Ive tried using the turntowards() method. but the bullets only shot to the right of the screen despite where it is rotated.
Any suggestions?
I have a character class :
import greenfoot.*;
public class Gun extends Actor
{
private int speed;
public void act()
{
// Add your action code here.
MouseInfo mouse = Greenfoot.getMouseInfo();
if (mouse !=null)
setRotation((int)(180*Math.atan2(mouse.getY()-getY(),mouse.getX()-getX())/Math.PI));
move(speed);
if(Greenfoot.mouseClicked(null))
{
getWorld().addObject(new bullet(getRotation()),getX(),getY());
turnTowards(mouse.getX(), mouse.getY());
}
}
}
I have a bullet class :
import greenfoot.*;
public class bullet extends Actor
{
private int direction;
public void act()
{
setLocation(getX()+5,getY());
}
public bullet(int dir)
{
this.direction=dir;
}
}
And i have a baddie class :
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class balloon here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class baddie extends Actor
{
public void act()
{
setLocation(getX(), getY()-1);
}
}