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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 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; } } |