I have so when the user left clicks it (is supposed to) shoot a red ball directed at the mouse. This is the code for the ball actor.
It only seems to shoot at either 90, 45, or 0 degree angles. Any reason?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class redportal_ball here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class redportal_ball extends Actor
{
/**
* Act - do whatever the redportal_ball wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public MouseInfo mouse = Greenfoot.getMouseInfo();
public GameWorld board = (GameWorld) getWorld();
public boolean created = false;
public void act()
{
move(1);
Actor wall;
//wall = getOneObjectAtOffset(0, 0, wall.class);
if (getOneObjectAtOffset(0, 0, wall.class) != null)
{
board.removeObjects(board.getObjects(redportal.class));
redportal portal = new redportal();
board.addObject(portal, this.getX(), this.getY());
board.removeObject(this);
}
if (!created)
{
setRotation(-45);
}
created = true;
}
public int findangle()
{
int mouseX = mouse.getX();
int mouseY = mouse.getY();
double deltaX = (mouseX - this.getX());
double deltaY = (mouseY - this.getY());
double angle = Math.atan(deltaX/deltaY);
angle = angle * 180 / 3.14;
int angle_return = (int)(angle);
System.out.println(angle_return + " " + angle + " " + mouseX + " " + mouseY);
return angle_return;
}
}


