Cara Mudah Membuat Game Java – > lanjutan (2)

Artikel berikut merupakan ulasan lanjutan dari step by step java mobile game sebelumnya, disini ditambahkan coding untuk menambahkan sprite(npc) yang bergerak secara random dalam game world (latar game).

yang perlu anda siapkan yaitu dari project yang sudah dibuat sebelumnya (pada Cara Mudah Membuat Game Java (1)) tambahkan sprite yang akan digunakan untuk npc, dalam hal ini saya membuat sprite laba, tambahkan code berikut dalam methode init()

this.spriteLaba = gameDesign.getLaba();
//define the reference in the midle of sprites frame so that transformations work well
this.spriteLaba.defineReferencePixel(8, 8);
this.spriteLabaAnimator = new SpriteAnimationTask(this.spriteLaba, true);


this.spriteLabaAnimator.setMoving(true);
this.timer.scheduleAtFixedRate(this.spriteLabaAnimator, 0, gameDesign.labaBawahSeqDelay);
this.spriteLabaRandomMovement = new SpriteRandomMovement(this, spriteLaba);
this.spriteLabaRandomMovement.setSequences(
gameDesign.labaBawahSeq, Sprite.TRANS_NONE,
gameDesign.labaBawahSeq, Sprite.TRANS_ROT180,
gameDesign.labaKananSeq, Sprite.TRANS_ROT180,
gameDesign.labaKananSeq, Sprite.TRANS_NONE
);
(new Thread(spriteLabaRandomMovement)).start();

tambahkan pula variable yang dibutuhkan sebagai berikut :

private Sprite spriteLaba;
private SpriteAnimationTask spriteLabaAnimator;
private SpriteRandomMovement spriteLabaRandomMovement;

kemudian buat class java yang akan menggerakkan sprite tersebut secara random, codingnya juga sudah ada di example game builder sebagai berikut, beri nama class dengan SpriteRandomMovement :

package hello;

import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.Sprite;

/**
* Animate a sprite on canvas using a simple algorithm
* to recover from collisions.
*
* @author Karel Herink
*/
public class SpriteRandomMovement implements Runnable {

private static final int SPEED = 3;
private runGame canvas;
private Sprite sprite;
private byte previousDirection = GameCanvas.DOWN;
private byte direction = GameCanvas.DOWN;
private boolean interrupted;
private int[] downSeq;
private int downTrans;
private int[] upSeq;
private int upTrans;
private int[] leftSeq;
private int leftTrans;
private int[] rightSeq;
private int rightTrans;

public SpriteRandomMovement(runGame canvas, Sprite sprite) {
this.canvas = canvas;
this.sprite = sprite;
}

public void setSequences(int[] downSeq, int downTrans, int[] upSeq, int upTrans, int[] leftSeq, int leftTrans, int[] rightSeq, int rightTrans) {
this.downSeq = downSeq;
this.downTrans = downTrans;
this.upSeq = upSeq;
this.upTrans = upTrans;
this.leftSeq = leftSeq;
this.leftTrans = leftTrans;
this.rightSeq = rightSeq;
this.rightTrans = rightTrans;
}

public void stop() {
this.interrupted = true;
}

public void run() {
while (!this.interrupted) {
if (this.direction == GameCanvas.DOWN) {
if (this.previousDirection != this.direction) {
this.sprite.setFrameSequence(this.downSeq);
this.sprite.setTransform(this.downTrans);
this.previousDirection = this.direction;
}
this.sprite.move(0, SPEED);
if (this.canvas.spriteCollides(this.sprite)) {
this.sprite.move(0, -SPEED);
this.direction = GameCanvas.LEFT;
continue;
}
} else if (this.direction == GameCanvas.UP) {
if (this.previousDirection != this.direction) {
this.sprite.setFrameSequence(this.upSeq);
this.sprite.setTransform(this.upTrans);
this.previousDirection = this.direction;
}
this.sprite.move(0, -SPEED);
if (this.canvas.spriteCollides(this.sprite)) {
this.sprite.move(0, SPEED);
this.direction = GameCanvas.RIGHT;
continue;
}
} else if (this.direction == GameCanvas.LEFT) {
if (this.previousDirection != this.direction) {
this.sprite.setFrameSequence(this.leftSeq);
this.sprite.setTransform(this.leftTrans);
this.previousDirection = this.direction;
}
this.sprite.move(-SPEED, 0);
if (this.canvas.spriteCollides(this.sprite)) {
this.sprite.move(SPEED, 0);
this.direction = GameCanvas.UP;
continue;
}
} else if (this.direction == GameCanvas.RIGHT) {
if (this.previousDirection != this.direction) {
this.sprite.setFrameSequence(this.rightSeq);
this.sprite.setTransform(this.rightTrans);
this.previousDirection = this.direction;
}
this.sprite.move(SPEED, 0);
if (this.canvas.spriteCollides(this.sprite)) {
this.sprite.move(-SPEED, 0);
this.direction = GameCanvas.DOWN;
continue;
}
}
try {
Thread.sleep(300);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
}

game loop sendiri ada di methode run() yang akan di jalankan terus-menerus selama game berjalan.

klo anda memiliki sprite dengan nama bukan laba maka gantilah nama variabel spriteLaba dengan nama Sprite yang anda buat .

nah jika project dijalankan maka akan terdapat sprite hero yang dapat digerakkan secara interaktif dari joystick atau arrow dari keyboard hp dan sprite yang digerakkan secara random.

selamat mencoba