tutorial game j2me
DESCRIPTION
Game Mobile, Belajar Game Java NetbeansTRANSCRIPT
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
1
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
2
Berikut ini saya akan menjelaskan tutorial pembuatan game J2ME menggunakan Netbeans.
1. Kita harus menyipakan gambar *.png seperti gambar berikut :
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
3
2. Buat Project baru Java ME di netbeans.
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
4
3. Membuat New Game Desain
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
5
4. Copy Gambar *.png yang ada di langkah pertama tadi.
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
6
5. Buka Game Desain .
Langkah membuat Desain Game.
a. Buat Tiled Layaers dengan cara Create tiled Layer…
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
7
NB */Buatlah Tiled sesuai kebutuhan Anda
b. Membuat Sprite, Create Sprite :
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
8
c. Untuk Mengganti gambar susai urutan , misalnya berjalan tinggal di drag saja :D
Kemudian untuk menambah karakter tinggal di Duplicate, dan Rename untuk
mengganti nama :D
d. Membuat Scane Baru >> Create Scane
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
9
e. Untuk menambahkan Tiled Layer dan yang telah kita buat caranya adalah , klik
tampak gambar seperti berikut
*/NB : untuk menambahkan Sprite tinggal klik Add Sprite seperti gambardi atas
f. Untuk mengatur posisi letak atur di Navigator :D
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
1
0
6. Jika sudah kelar, kita buat Java Class baru , ikuti langkah berikut :D
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
1
1
7. Kemudian ketikan Koding Berikut ini :D
import java.io.IOException; import java.util.Random; import java.util.Timer; import java.util.TimerTask; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; import javax.microedition.lcdui.game.GameCanvas; import javax.microedition.lcdui.game.Layer; import javax.microedition.lcdui.game.LayerManager; import javax.microedition.lcdui.game.Sprite; import javax.microedition.lcdui.game.TiledLayer; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Ekiq */ public class GameCalss extends GameCanvas implements Runnable { int posx, posy, waktu = 120 , score = 0, bgpos, max=500; Graphics g = getGraphics(); GameDesign gd ; Sprite karakter, gOver; // Layer lantai, tembok ; TiledLayer lantai ; Sprite buah; LayerManager lm; Timer t; Random r; boolean start = false; boolean karakter1 = true; private int lastDirection; public GameCalss() { super(true); setFullScreenMode(true); init(); new Thread(this).start(); } private void init() { t = new Timer(); t.schedule(new TimerTask() { public void run() { waktu--; if (waktu == 0) { t.cancel(); } } }, 1000, 1000); gd = new GameDesign(); lm = new LayerManager(); try { r = new Random(); karakter = gd.getKarakter(); lantai = gd.getLantai(); gd.updateLayerManagerForLevel1(lm);
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
1
2
this.karakter = gd.getKarakter(); buah = new Sprite(Image.createImage("start.png")); gOver = new Sprite(Image.createImage("youlose.png")); // burung = new Sprite(Image.createImage("br_kanan.png"), 128/4, 30); } catch (IOException ex) { ex.printStackTrace(); } } public void kendali() { int keyStates = getKeyStates(); if(keyStates == 0){ lastDirection=0; } if ((keyStates & RIGHT_PRESSED)!=0){ karakter.move(+5, 0); if(lastDirection != RIGHT){ lastDirection = RIGHT; karakter.setFrameSequence(gd.Kiri); } karakter.nextFrame(); if(posx < 205){ posx += 3; } } if ((keyStates & LEFT_PRESSED)!=0){ karakter.move(+5, 0); if(lastDirection != LEFT){ lastDirection = LEFT; karakter.setFrameSequence(gd.Kanan); } karakter.nextFrame(); if(posx > 5){ posx -= 3; } } if ((keyStates & DOWN_PRESSED)!=0){ karakter.move(0, +5); if(lastDirection != DOWN){ lastDirection = DOWN; karakter.setFrameSequence(gd.Depan); } karakter.nextFrame(); if(posy<270){ posy += 3; } } if ((keyStates & UP_PRESSED)!=0){ karakter.move(0, +5); if(lastDirection != UP){ lastDirection = UP; karakter.setFrameSequence(gd.Belakang); } karakter.nextFrame(); if(posy > 5){ posy -= 3; } }
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
1
3
karakter.setPosition(posx,posy); } private void bintang() { if (buah.collidesWith(karakter, true)) { start= false; } buah.paint(g); } private void start() { if (buah.collidesWith(karakter, true)) { buah.setPosition(r.nextInt(205), r.nextInt(300)); score += 10; max -=10; } buah.paint(g); } private void buah() { buah.setPosition(r.nextInt(205), r.nextInt(300)); buah.paint(g); } private void gOver(){ gOver.setPosition(100, 100); gOver.paint(g); start = true; } private void winner(){ g.drawString("Lanjut ke Level2", getWidth()/2, getHeight()/2+70, g.TOP|g.HCENTER); } private void bom() { buah.move(0,5); if (buah.getY() > getHeight()) { buah.setPosition(r.nextInt(getWidth() - buah.getWidth()), 0); } buah.paint(g); } /* private void burungku() { burung.nextFrame(); burung.move(0, 3); if (burung.getY() > getHeight()) { burung.setPosition(0, r.nextInt(getWidth() - burung.getWidth())); } burung.paint(g); } */ public void run() { while (true) { lm.paint(g, 0, 0); kendali(); bom(); start(); bintang(); // burungku();
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
1
4
g.drawString("Score :" + score, 5, 5, 0); g.drawString("Poin Max :" + max, 85, 5, 0); g.drawString("Time :" + waktu, 180,5, 0); if (waktu <= 0) { gOver(); } if (score >=500){ waktu = 0; winner(); } flushGraphics(); try { Thread.sleep(20); } catch (InterruptedException ex) { ex.printStackTrace(); } } } }
8. Selanjutnya membuat MIDlet Game :D
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
1
5
9. Ketikkan Koding berikut ini :D
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import javax.microedition.lcdui.Display; import javax.microedition.midlet.*; /** * @author ekiq */ public class Midlet extends MIDlet { private GameCalss gameCalss; private Display d; public void startApp() { d = Display.getDisplay(this); gameCalss = new GameCalss(); d.setCurrent(gameCalss); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } }
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
1
6
10. Coba di Running Gamenya :D maka hasilnya sebagai berikut
AH
MA
D S
UB
KI w
ww.pelajar-m
ania.blogspot.com
1
7
Hahahaha kalo masih Error….
Ganti nama di Game Desain seperti gambar ini ya :D