Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.
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();
}
}
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);
}
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.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;
}
px = (int) Math.floor (x + 0.5);
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?