Hi,
I'm having issues figuring out how to code the handleOrbCollision(). I've declared the local varialbes collider (type Orb) and vel (type Velocity). I've checked if a collision occurred using the condition, getIntersectingOrb()!= null.
Next, if a collision has indeed occurred we are to get the intersection object by calling the getIntersectingOrb()method; store this in collider call the collider's getVelocity() method to get the velocity of the collider.
The next step is to interchange the velocity of the colllider with that of the current orb. Use collider's setVelocity() method, with parameter this.getVelocity() to set the velocity of the collider to that of the current orb. Now set the velocity of the current orb to to that of the intersecting orb.
Here is what I have thus far....
public class Orb extends Actor
{
// instance variables
private Velocity vel;
private Velocity newVel;
private int xSpeed;
private int ySpeed;
private int exactX;
private int exactY;
private GreenfootImage getImage;
private Orb collider;
/** * Create the constructors for Orb
*/
public Orb(int xSpeed, int ySpeed)
{
vel= new Velocity(xSpeed, ySpeed);
this.vel=vel; getImage = new GreenfootImage("gold-ball.png");
}
/** * Return velocity object associated with Orb
*/
public Velocity getVelocity()
{
return vel;
}
/** * Replaces the current Velocity object with new Velocity
*/
public Velocity setVelocity()
{
return newVel;
}
/**
* Act - do whatever the Orb wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
turnIfNeeded();
move();
handleOrbCollision();
}
public Orb getIntersectingOrb()
{
Actor a = getOneIntersectingObject(Orb.class);
if( a!= null)
{
return (Orb)a;
}
else { return null; }
}
public Orb handleOrbCollision()
{
Actor a = getOneIntersectingObject(Orb.class);
if(a != null)
{
}
}
/**
* Move in the current movement direction.
*/
public void move()
{
exactX = exactX + vel.getXSpeed();
exactY = exactY + vel.getYSpeed();
super.setLocation( (int) exactX, (int) exactY);
}
/** Enables the Orb to collide on each of the four walls, when necessary.
*/
public void turnIfNeeded()
{
turnAtFloor();
turnAtRoof();
turnAtRightWall();
turnAtLeftWall();
}
/** Orb has been added to the World.
*/
public void addedToWorld(World world)
{
exactX = getX();
exactY = getY();
}
/** Enables the Orb to hit the floor of world from edge of orb object.
*/
public void turnAtFloor()
{
if (getY() + getImage().getHeight()/2>=getWorld().getHeight() )
{
vel.reverseY();
Greenfoot.playSound("boop.wav");
}
}
/** Enables the Orb to hit the right wall of world from edge of orb object.
*/
public void turnAtRightWall()
{
if (getX() + getImage().getWidth()/2>=getWorld().getWidth() )
{
vel.reverseX();
Greenfoot.playSound("boop.wav");
}
}
/** Enables the Orb to hit the top of world from edge of orb object.
*/
public void turnAtRoof()
{
if (getY() - getImage().getHeight()/2<= 0 )
{
vel.reverseY();
Greenfoot.playSound("boop.wav");
}
}
/** Enables the Orb to hit the left of world from edge of orb object.
*/
public void turnAtLeftWall()
{
if (getX() - getImage().getWidth()/2<= 0 )
{
vel.reverseX();
Greenfoot.playSound("boop.wav");
}
}

