troubleshooting Question

Java Sprites

Avatar of tango2009
tango2009 asked on
Programming Languages-OtherGame ProgrammingProgramming
23 Comments1 Solution694 ViewsLast Modified:
I am creating a java midlet game. I have created sprites that drift back and forth. I now want to have these sprites travel along a path. My sprite class is below.  

I want to have my sprites go in a circular path around a number of points. How would I go about doing this?
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;

public class DriftSprite extends Sprite {
  private Random     rand;
  private int        speed;
  private TiledLayer barrier;

  public DriftSprite(Image image, int frameWidth, int frameHeight, int driftSpeed,
    TiledLayer barrierLayer) {
    super(image, frameWidth, frameHeight);

    // Initialize the random number generator
    rand = new Random();

    // Set the speed
    speed = driftSpeed;

    // Set the tiled layer barrier
    barrier = barrierLayer;
  }

  public void update() {
    // Temporarily save the position
    int xPos = getX();
    int yPos = getY();

    // Randomly move the sprite to simulate drift
    switch (Math.abs(rand.nextInt() % 4)) {
    // Drift left
    case 0:
      move(-speed, 0);
      break;
    // Drift right
    case 1:
      move(speed, 0);
      break;
    // Drift up
    case 2:
      move(0, -speed);
      break;
    // Drift down
    case 3:
      move(0, speed);
      break;
    }

    // Check for a collision with the barrier
    if ((barrier != null) && collidesWith(barrier, true)) {
      // Move the sprite back to its original position
      setPosition(xPos, yPos);
    }

    // Move to the next animation frame in the sequence
    nextFrame();
  }
}
ASKER CERTIFIED SOLUTION
Member_2_5069294

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 23 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 23 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros