Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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?

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();
}
}
```

I'm not used to Java. This code is as close as I can guess to the right syntax. You may have to tweak it a bit.

```
public class LoopSprite extends Sprite
{
private float angle;
private int frame;
private int speed;
public LoopSprite(Image image, int frameWidth, int frameHeight, int loopSpeed,
TiledLayer barrierLayer) {
super(image, frameWidth, frameHeight);
angle = 0;
frame = 0;
speed = loopSpeed;
barrier = barrierLayer;
}
public void update()
{
switch (Math.floor (frame / 5))
{
case 3:
case 6:
case 10:
case 13:
angle += (Math.Pi / 10);
}
++frame;
if (frame == 70) frame = 0;
move (Math.cos (angle) * speed, Math.sin (angle) * speed);
}
```

It works in 5 frame groups, every time you call update is another frame. It goes right for 15 frames, turns for 5 frame, down for 10 frames, turns again, left for 15 frames, turns again, up for 10 frames, then turns back to where it started. The entire pattern takes 70 frames, on the 70th it starts from 0 again. It still uses a speed variable, so that you can control how fast it moves.

Math.Pi equates to 180 degrees. Math.Pi / 10 is 18 degrees. 5 turns of 18 degrees gets you 90 degrees from whatever angle you started with.

possible loss of precision

found : double

required: int

switch (Math.floor( frame /5))

and this one

javax.microedition.lcdui.g

move (Math.cos (angle) * speed, Math.sin (angle) * speed);

move uses integers. How can I get around this?

```
switch ((int) (frame / 5));
```

```
move ((int) Math.round (Math.cos (angle) * speed), (int) Math.round (Math.sin (angle) * speed));
```

Though that becomes less accurate if speed is small. If speed is less the 0.5, the sprite won't move at all.A better way is to keep the sprites x and y coordinates as double values. Then move it to the nearest integer position. This code keeps track of the integer position of the sprite. There's probably an easier way to do this, either the sprite can be positioned (rather than moved) or you can get it's pixel x and y rather than having to keep track of them.

```
public class LoopSprite extends Sprite
{
private float angle;
private int frame;
private int speed;
private float x, y; // accurate x and y
private int px, py; // pixel x and y
public LoopSprite(Image image, int frameWidth, int frameHeight, int loopSpeed, TiledLayer barrierLayer)
{
angle = 0;
frame = 0;
speed = loopSpeed;
x = ?; // needs a start position
y = ?; // needs a start position
px = (int) Math.round (x);
py = (int) Math.round (y);
}
public void update()
{
int dx , dy;
switch (Math.floor (frame / 5))
{
case 3:
case 6:
case 10:
case 13:
angle += (Math.Pi / 10);
}
x += Math.cos (angle) * speed;
y += Math.sin (angle) * speed;
dx = (int) Math.round (x);
dy = (int) Math.round (y);
move (dx - px, dy - py);
px = dx;
py = dy;
++frame;
if (frame == 70) frame = 0;
}
```

cannot find symbol

symbol : method round(float)

location: class java.lang.Math

px = (int) Math.round(x);

round doesn't seem to be an option that I can use with Math. Is there anything I can use to do a similar job.

```
px = (int) Math.floor (x + 0.5);
```

angle += (Math.Pi / 10);

```
switch (Math.floor (frame / 5))
{
case 3:
```

This divides the frame count by five, if the result is between 3 and 4 (a frame count between 15 and 20) then it runs the code in the switch statement. If it did this -```
switch (Math.floor (frame / 3))
{
case 2:
case 5:
```

The code would run on frames 6-9 and 15-18.
```
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
public class LoopSprite extends Sprite {
private Random rand;
private double speed;
private TiledLayer barrier;
private int frame;
private int angle;
private float x, y; // accurate x and y
private int px, py; // pixel x and y
public LoopSprite(Image image, int frameWidth, int frameHeight, int driftSpeed,
TiledLayer barrierLayer) {
super(image, frameWidth, frameHeight);
x = 10.4f; // needs a start position
y = 5.4f; // needs a start position
px = (int) Math.floor(x + 0.5);
py = (int) Math.floor (y + 0.5);
// Initialize the random number generator
rand = new Random();
angle = 0;
frame = 0;
// Set the speed
speed = 0.4;
// Set the tiled layer barrier
barrier = barrierLayer;
}
public void update() {
// Temporarily save the position
int xPos = getX();
int yPos = getY();
int dx , dy;
// switch (Math.floor(frame / 5))
switch ((int)( frame /5))
{
case 3:
case 6:
case 10:
case 13:
angle += (Math.PI / 10);
}
++frame;
// 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;
}*/
x += Math.cos (angle) * speed;
y += Math.sin (angle) * speed;
dx = (int) Math.floor (x);
dy = (int) Math.floor (y);
move (dx - px, dy - py);
px = dx;
py = dy;
if (frame == 70) frame = 0;
// move (Math.cos (angle) * speed, Math.sin (angle) * speed);
move ((int) Math.floor (Math.cos (angle) * speed), (int) Math.floor (Math.sin (angle) * speed));
// 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();
}
}
```

```
move ((int) Math.floor (Math.cos (angle) * speed), (int) Math.floor (Math.sin (angle) * speed));
```

This line will make it move twice as fast as it should. I don't think thats the problem but please delete it.```
angle += (Math.PI / 10);
```

Could you set a breakpoint on this line and run the program? It appears like the program never gets to this line. It could be something outside of the function that prevents this happening. Is something setting 'frame' back to zero or changing one of the other variables?
I think at the moment just looking I might be setting the position twice could this be the problem?

breakpoint.jpg

When you say setting the position, do you mean using setPosition()? It's used in the update function, is it used somewhere else?

x = 10.4f; // needs a start position

y = 5.4f; // needs a start position

could this be the problem?

The problem is that angle stay at zero and I've just spotted why, angle is declared as an int, it needs to be a float.

switch ((int)( frame /15))

and here

angle += (Math.PI / 20);

However this either distorts the path or makes the sprite just leave the screen. How can I increase the size of the path without distorting the path?

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.