I have a problem where i have a character whos supposed to be able and through "Short_Brian" left and right but for some reason he the Short Brian only moves right... Here is my code.. This is the code for "Mover"(subclass of actor).
And This is the code for "Brian" (subclass of Mover)
If anyone can help, Thanks.
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
//BTW ignore the comments, was a copy and paste from other projects.
//
// A thing that can move around with a certain speed.
//The mover maintains a movement vector of current movement.
public abstract class Mover extends Actor
{
private Vector movement = new Vector();
private double x = 0;
private double y = 0;
private static final int acceleration = 1; // down (gravity)
private static final int speed = 7; // running speed (sideways)
public boolean test;
private int vSpeed = 0; // current vertical speed
public void moveRight()
{
setLocation ( getX() + speed, getY() );
}
public void moveLeft()
{
setLocation ( getX() - speed, getY() );
}
public boolean onGround()
{
Object under = getOneObjectAtOffset(0, getImage().getHeight()/2-2 , null);
return under != null;
}
public void setVSpeed(int speed)
{
vSpeed = speed;
}
public void fall()
{
setLocation ( getX(), getY() + vSpeed);
vSpeed = vSpeed + acceleration;
if ( isAtEdge() ){
Greenfoot.stop();
}
}
private boolean atBottom()
{
return getY() >= getWorld().getHeight() - 2;
}
public boolean tester(){
if (Greenfoot.isKeyDown("left") )
{
test = false;
}
if (Greenfoot.isKeyDown("right") )
{
test = true;;
}
return test;
}
public Mover()
{
}
/**
* Create new mover initialised with given speed vector.
*/
public Mover(Vector speed)
{
movement = speed;
}
/**
* Move forward one step. Direction and speed are dtermined by the
* internal movement vector. Movement wraps around at the world edges.
*/
public void move()
{
x = x + movement.getX();
y = y + movement.getY();
if(x >= getWorld().getWidth()) {
x = 0;
}
if(x < 0) {
x = getWorld().getWidth() - 1;
}
if(y >= getWorld().getHeight()) {
y = 0;
}
if(y < 0) {
y = getWorld().getHeight() - 1;
}
setLocation(x, y);
}
public void setLocation(double x, double y)
{
this.x = x;
this.y = y;
super.setLocation((int) x, (int) y);
}
public void setLocation(int x, int y)
{
setLocation((double) x, (double) y);
}
/**
* Increase the current speed with the given vector.
*/
public void increaseSpeed(Vector s)
{
movement.add(s);
}
/**
* Return the current movement.
*/
public Vector getMovement()
{
return movement;
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Brian here.
*
* @author (yMysteryMan)
* @version (a version number or a date)
*/
public class Brian extends Mover
{
private static final int jumpStrength = 15;
private int attackDelay = 27; // The minimum delay between firing the gun.
private int attackDelayCount;
private int attacksFired;
private int test;
public Brian(){
attackDelayCount = 0;
attacksFired = 0;
}
public void act()
{
checkKeys();
checkFall();
attackDelayCount++;
}
private void checkKeys()
{
if (Greenfoot.isKeyDown("left") )
{
setImage("Brian-left.png");
moveLeft();
}
if (Greenfoot.isKeyDown("right") )
{
setImage("Brian-right.png");
moveRight();
}
if (Greenfoot.isKeyDown("up") )
{
if (onGround())
jump();
}
if (Greenfoot.isKeyDown("m")){
longRange();
}
}
public int getattacksUsed(){
return attacksFired;
}
public void setAttackPauseLength(int reloadTime){
attackDelay = reloadTime;
}
private void jump()
{
setVSpeed(-jumpStrength);
fall();
}
private void checkFall()
{
if (onGround()) {
setVSpeed(0);
}
else {
fall();
}
}
private void longRange(){
if(attackDelayCount >= attackDelay){
Short_Brian shoot = new Short_Brian(getMovement().copy(), getRotation());
getWorld().addObject(shoot, getX(), getY());
attacksFired++;
attackDelayCount = 0;
}
}
}
And lastly this is the code for Short_Brian
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* A bullet that can hit asteroids.
*
* @author Secret lol
*
* @version 2.0
*/
public class Short_Brian extends Mover
{
/** A bullet looses one life each act, and will disappear when life = 0 */
private int life = 4000;
/** The damage this bullet will deal */
private int knockBack = Greenfoot.getRandomNumber(8)+1;
public Short_Brian()
{
}
public Short_Brian(Vector speed, int rotation)
{
super(speed);
setRotation(rotation);
increaseSpeed(new Vector(rotation, 15));
}
/**
* The bullet will damage asteroids if it hits them.
*/
public void act()
{
if(life <= 0) {
getWorld().removeObject(this);
}
else {
move();
life--;
}
checkBounds();
turn(5);
}
public void checkBounds(){
if (isAtEdge()){
getWorld().removeObject(this);
if(getWorld() == null) return;
}
}
}
