Can someone kindly look at my code and tell me what I have done wrong? When I compile the program, two of the Orbs will at times stick to the roof or the floor if they start out there. I am just starting out and would really appreciate any help.
The Assignment: Create a method called handleOrbCollision(). It should store the intersecting Orb identified in Step 1(c) above in a temporary variable called collider. If a collision has occurred, the Velocity of the collider object should be assigned to a placeholder Velocity object. Use the methods you wrote in Steps 1(a) and 1(b) to exchange the Velocity objects associated with the colliding Orbs.
Code:
public class Orb extends Actor
{
private Velocity vel;
private int exactX;
private int exactY;
private int xSpeed;
private int ySpeed;
private GreenfootImage getImage;
private Orb getIntersectingOrb;
private Orb collider;
public Orb(int xSpeed,int ySpeed)
{
vel = new Velocity(xSpeed, ySpeed);
vel = vel;
getImage=new GreenfootImage("gold-ball.png");
}
public void act()
{
move();
turnAtFloor();
turnAtLeftWall();
turnAtRoof();
turnAtRightWall();
}
public void addOrbs()
{
List <Orb> orbs = getWorld().getObjects(Orb.class);
for(Orb orb : orbs)
{
setLocation(getX(), getY());
}
}
public void move()
{
exactX = exactX + vel.getXSpeed();
exactY = exactY + vel.getYSpeed();
setLocation( (int) exactX, (int) exactY );
}
public void turnAtFloor()
{
if (getY() + getImage().getHeight()/2 >= getWorld().getHeight())
{
Greenfoot.playSound("tock.wav");
vel.reverseY();
}
}
public void turnAtLeftWall()
{
if(getX() - getImage().getWidth()/2 <= 0)
{
Greenfoot.playSound("tock.wav");
vel.reverseX();
}
}
public void turnAtRoof()
{
if (getY() - getImage().getHeight()/2 <= 0)
{
Greenfoot.playSound("tock.wav");
vel.reverseY();
}
}
public void turnAtRightWall()
{
if(getX()+ getImage().getWidth()/2 >= getWorld().getWidth())
{
Greenfoot.playSound("tock.wav");
vel.reverseX();
}
}
public void addedToWorld(World world)
{
exactX = getX();
exactY = getY();
}
public int getXSpeed()
{
return xSpeed;
}
public int getYSpeed()
{
return ySpeed;
}
public Velocity getVelocity()
{
return vel;
}
public void setVelocity(Velocity newvel)
{
vel = newvel;
}
private Orb getIntersectingOrb()
{
return (Orb) getOneIntersectingObject(Orb.class);
}
public void handleOrbCollision()
{
collider = getIntersectingOrb;
if (collider == getIntersectingOrb)
{
vel.reverseY();
}
else if(collider == getIntersectingOrb)
{
vel.reverseX();
}
}

