Koding tuk Loncat Player M-Gatot Games

Posted: Oktober 17, 2010 in Tutorial M-Jeni Java Mobile

nih aku poskan sedikit kode untuk buat player’s loncat kebawah sendiri,,, tinggal tulis kodenya dengan buat methode baru,,

 

public void move() {
int key = getKeyStates();
prevPlayerX = playerX;
prevPlayerY = playerY;
if (key == LEFT_PRESSED && !(player.getX() < 0)) {
playerX = Math.max(0, playerX – 3);
player.setTransform(Sprite.TRANS_MIRROR);
posSx = -65;
if (lastDirect != LEFT){
lastDirect = LEFT;
player.setFrameSequence(gd.DonoJalan);
}playerAnim.setMoving(true);
sen = false;
}
else if (key == RIGHT_PRESSED && !(player.getX() > ground.getWidth())) {
playerX = Math.min(jalan.getWidth() – player.getWidth(), playerX + 3);
player.setTransform(Sprite.TRANS_NONE);
posSx = 40;
if (lastDirect != RIGHT) {
lastDirect = RIGHT;
player.setFrameSequence(gd.DonoJalan);
}playerAnim.setMoving(true);
sen = false;
}else{
playerAnim.setMoving(false);
}
if (key  == UP_PRESSED) {
jc += 1;
player.defineCollisionRectangle(0, player.getHeight() – 2, player.getWidth(), 2);
if (!loncat && player.collidesWith(jalan, false) && !(player.getY()<20)) {
loncat = true;
if (lastDirect != UP) {
lastDirect = UP;
player.setFrameSequence(gd.DonoLoncat);
}playerAnim.setMoving(true);
sen = false;
}
}else if (key == DOWN_PRESSED) {
player.setFrameSequence(gd.DonoNunduk);
playerAnim.setMoving(true);
playerY += 3;
}else if (key == FIRE_PRESSED){
senjata.setPosition(player.getX()+posSx, player.getY()-30);
sen = true;
if(lastDirect == LEFT)
playerX +=3;
if(lastDirect == RIGHT)
playerX -=3;
}
if (loncat) {
jc -= 1;
playerY -= 2;
player.setFrameSequence(gd.DonoLoncat);
player.setPosition(playerX, playerY);
player.defineCollisionRectangle(0, 0, getWidth(), 10);
if (jc <= 0 || player.collidesWith(jalan, true)  && player.getY()<20) {
loncat = false;
playerAnim.setMoving(true);
}
} else {
player.defineCollisionRectangle(2, player.getHeight() – 2, player.getWidth() – 2, 2);
if (!player.collidesWith(jalan, true)) {
player.setFrameSequence(gd.DonoJalan);
playerY += 2;
jc = 20;
playerAnim.setMoving(true);
}
player.setPosition(playerX, playerY);
}
player.defineCollisionRectangle(0, 0, player.getWidth(), player.getHeight() – 2);
if (player.collidesWith(jalan, true)) {
playerX = prevPlayerX;
player.setPosition(playerX, playerY);
}
player.defineCollisionRectangle(2, 0, player.getWidth() – 2, player.getHeight() – 2);
if (player.collidesWith(jalan, true)) {
playerY = prevPlayerY;
player.setPosition(playerX, playerY);
}
if (playerX – layarX >= getWidth() – 120) {
layarX = Math.min(jalan.getWidth() – getWidth(), layarX + 3);
}
if (playerX – layarX <= 120) {
layarX = Math.max(0, layarX – 3);
}
lm.setViewWindow(layarX, 0, getWidth(), getHeight());
}

 

Thank.

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s