mobile gameand application withj2me - collision detection

36
J J 2ME 2ME Mobile games Mobile games

Upload: jenchoke-tachagomain

Post on 29-May-2015

1.853 views

Category:

Documents


2 download

DESCRIPTION

Collision Detection

TRANSCRIPT

Page 1: Mobile Gameand Application withJ2ME  - Collision Detection

JJ2ME2MEMobile gamesMobile games

Page 2: Mobile Gameand Application withJ2ME  - Collision Detection

การตรวจพบการกระทบก�นการตรวจพบการกระทบก�น ((Collision DetectionCollision Detection))

เป็�นวิ�ธี�การตรวิจสอบวิ�า สไป็รต� เป็�นวิ�ธี�การตรวิจสอบวิ�า สไป็รต� (Sprite) (Sprite) เคลื่��อนที่��เคลื่��อนที่��กระที่บก�บ สไป็รต� กระที่บก�บ สไป็รต� (Sprite) (Sprite) ต�วิอ��นหร�อไม่� ต�วิอ��นหร�อไม่� ม่�ป็ระโยชน�ม่ากในการน"าไป็ใช#ก�บภาพเคลื่��อนไหวิม่�ป็ระโยชน�ม่ากในการน"าไป็ใช#ก�บภาพเคลื่��อนไหวิของเกม่ส� แต�ลื่ะสไป็รต� ของเกม่ส� แต�ลื่ะสไป็รต� (Sprite) (Sprite) จะม่�ขอบเขตเป็�นจะม่�ขอบเขตเป็�นส��เหลื่��ยม่ม่)ม่ฉาก บางส�วินของภาพจะโป็ร�งแสงส��เหลื่��ยม่ม่)ม่ฉาก บางส�วินของภาพจะโป็ร�งแสงเม่��อเคลื่��อนที่��กระที่บก�นจะที่"าให#ม่)ม่ของส��เหลื่��ยม่เม่��อเคลื่��อนที่��กระที่บก�นจะที่"าให#ม่)ม่ของส��เหลื่��ยม่ม่)ม่ฉากเหลื่��อม่ลื่"+าก�น ที่"าให#ตรวิจพบได้#ที่�นที่�วิ�าม่�ม่)ม่ฉากเหลื่��อม่ลื่"+าก�น ที่"าให#ตรวิจพบได้#ที่�นที่�วิ�าม่�การกระที่บก�นการกระที่บก�น

Page 3: Mobile Gameand Application withJ2ME  - Collision Detection

Collision DetectionCollision Detection ((Sprite Vs SpriteSprite Vs Sprite)) สร#างสร#าง Work space Work space ช��อช��อ CollisionCollision

สร#าง สร#าง J2ME Class J2ME Class ช��อ ช��อ SpriteCollideMIDlet.javaSpriteCollideMIDlet.java

สร#างสร#าง GameCanvasGameCanvas โด้ยให#แสด้งโด้ยให#แสด้ง sprite sprite สองต�วิสองต�วิ

หากหาก spritesprite ชน อ�กต�วิ ให#หย)ด้เด้�นชน อ�กต�วิ ให#หย)ด้เด้�น

Page 4: Mobile Gameand Application withJ2ME  - Collision Detection

SpriteCollideMIDlet.javaSpriteCollideMIDlet.javaimport java.io.IOException;import java.io.IOException;import javax.microedition.lcdui.*;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import javax.microedition.lcdui.game.*;import javax.microedition.midlet.*;import javax.microedition.midlet.*;public class SpriteCollideMIDlet extends MIDlet {public class SpriteCollideMIDlet extends MIDlet { SpriteCollideCanvasSpriteCollideCanvas canvas = new canvas = new SpriteCollideCanvas();SpriteCollideCanvas(); public public SpriteCollideMIDlet() {}SpriteCollideMIDlet() {} protected void protected void destroyApp(boolean arg0) throws destroyApp(boolean arg0) throws

MIDletStateChangeException {}MIDletStateChangeException {} protected void protected void pauseApp() {}pauseApp() {} protected void protected void startApp() throwsstartApp() throws MIDletStateChangeException MIDletStateChangeException

{{Display display = Display.Display display = Display.getDisplaygetDisplay(this);(this);canvas.start();canvas.start();display.setCurrent(canvas);display.setCurrent(canvas);

}}}}

Page 5: Mobile Gameand Application withJ2ME  - Collision Detection

classclass SpriteCollideCanvasSpriteCollideCanvas extendsextends GameCanvas GameCanvas implementsimplements RunnableRunnable {{

staticstatic intint FRONT_DIRECTIONFRONT_DIRECTION = 0; = 0;staticstatic intint LEFT_DIRECTIONLEFT_DIRECTION = 1; = 1;staticstatic intint RIGHT_DIRECTIONRIGHT_DIRECTION = 2; = 2;staticstatic intint BACK_DIRECTIONBACK_DIRECTION = 3; = 3;Sprite hero,hero2;Sprite hero,hero2;booleanboolean running; running;intint[][] sequence = {{0,1,2,3},[][] sequence = {{0,1,2,3}, //front//front

{4,5,6,7},{4,5,6,7}, //left//left{8,9,10,11},{8,9,10,11}, //right//right{12,13,14,15}};{12,13,14,15}}; //back//back

intint s2[] ={8,9,10,11}; s2[] ={8,9,10,11};intint w, h; w, h;Image img_bg = Image img_bg = nullnull;;intint cx,cy,cx2,cy2; cx,cy,cx2,cy2;intint currentDirection = currentDirection = FRONT_DIRECTIONFRONT_DIRECTION;;

Page 6: Mobile Gameand Application withJ2ME  - Collision Detection

protectedprotected SpriteCollideCanvas() { SpriteCollideCanvas() {supersuper((truetrue););Image im = Image im = nullnull, hm2 = , hm2 = nullnull;;trytry { {

im = Image.im = Image.createImagecreateImage("/sprites/c1.png");("/sprites/c1.png");hm2 = Image.hm2 = Image.createImagecreateImage("/sprites/c4.png");("/sprites/c4.png");img_bg = img_bg =

Image.Image.createImagecreateImage("/world/roundedbg.png");("/world/roundedbg.png");

} } catchcatch (IOException e) {} (IOException e) {}

hero = hero = newnew Sprite(im,32,48); Sprite(im,32,48);hero2 = hero2 = newnew Sprite(hm2,32,48); Sprite(hm2,32,48);w = getWidth();w = getWidth();h = getHeight();h = getHeight();cx = w/2;cx = w/2;cy = h/2;cy = h/2;hero.setFrameSequence(sequence[0]);hero.setFrameSequence(sequence[0]);hero2.setFrameSequence(s2);hero2.setFrameSequence(s2);

}}

Page 7: Mobile Gameand Application withJ2ME  - Collision Detection

publicpublic voidvoid start(){ start(){running = running = truetrue;;Thread t = Thread t = newnew Thread( Thread(thisthis););t.start();t.start();

}}publicpublic voidvoid run() { run() {

Graphics g = getGraphics();Graphics g = getGraphics();intint delay = 100; delay = 100;whilewhile(running) {(running) {

moving();moving();getInput();getInput();hero2.nextFrame();hero2.nextFrame();drawScreen(g);drawScreen(g);trytry {Thread. {Thread.sleepsleep(delay);} (delay);} catchcatch (InterruptedException e) {} (InterruptedException e) {}

}}}}

Page 8: Mobile Gameand Application withJ2ME  - Collision Detection

voidvoid moving() { moving() {

ifif (cx2 > getWidth()) { (cx2 > getWidth()) {

cx2 = 0;cx2 = 0;

}}

if if ( ! hero2.collidesWith(hero, ( ! hero2.collidesWith(hero, falsefalse)) )) cx2++;cx2++;

}}

Page 9: Mobile Gameand Application withJ2ME  - Collision Detection

void getInput(){void getInput(){int keyState = getKeyStates();int keyState = getKeyStates();int cDirection = currentDirection; int cDirection = currentDirection; // then we know current direction// then we know current direction

if(keyState== if(keyState== LEFT_PRESSEDLEFT_PRESSED) { ) { if( ! if( ! hero.collidesWith(hero2, false)hero.collidesWith(hero2, false)) {) {

cx = cx - 5; cx = Math.cx = cx - 5; cx = Math.max max (0, cx);(0, cx);currentDirection= currentDirection= LEFT_DIRECTIONLEFT_DIRECTION;;

} hero.nextFrame();} hero.nextFrame();} else if(keyState== } else if(keyState== RIGHT_PRESSEDRIGHT_PRESSED){){

cx = cx + 5; cx = Math.cx = cx + 5; cx = Math.min min (cx, w-32);(cx, w-32);currentDirection= currentDirection= RIGHT_DIRECTIONRIGHT_DIRECTION;;hero.nextFrame();hero.nextFrame();

} else if(keyState== } else if(keyState== UP_PRESSEDUP_PRESSED) {) {cy = cy - 5; cy = Math.cy = cy - 5; cy = Math.max max (0, cy);(0, cy);currentDirection= currentDirection= BACK_DIRECTIONBACK_DIRECTION;;hero.nextFrame();hero.nextFrame();

} else if(keyState== } else if(keyState== DOWN_PRESSEDDOWN_PRESSED){){ cy = cy + 5; cy = Math.cy = cy + 5; cy = Math.minmin(cy, h - 50);(cy, h - 50);

currentDirection= currentDirection= FRONT_DIRECTIONFRONT_DIRECTION;;hero.nextFrame();hero.nextFrame();

}} if(cDirection != currentDirection){if(cDirection != currentDirection){

hero.setFrameSequence(sequence[currentDirection]);hero.setFrameSequence(sequence[currentDirection]);}}

}}

Page 10: Mobile Gameand Application withJ2ME  - Collision Detection

voidvoid drawScreen(Graphics g){ drawScreen(Graphics g){

g.drawImage(img_bg, 0 - cx,0 - cy, Graphics.g.drawImage(img_bg, 0 - cx,0 - cy, Graphics.TOP TOP | | Graphics.Graphics.LEFTLEFT););

hero2.setPosition(cx2, cy2 + 20);hero2.setPosition(cx2, cy2 + 20);

hero2.paint(g);hero2.paint(g);

hero.setPosition(cx, cy);hero.setPosition(cx, cy);

hero.paint(g);hero.paint(g);

flushGraphics();flushGraphics();

}}

}}

Page 11: Mobile Gameand Application withJ2ME  - Collision Detection

LayerManagerLayerManager

Page 12: Mobile Gameand Application withJ2ME  - Collision Detection

LayerManager methodsLayerManager methods

voidvoid append(Layer l)append(Layer l)LayerLayer getLayerAt(int index)getLayerAt(int index)intint getSize()getSize()voidvoid insert(Layer l, int index)insert(Layer l, int index)voidvoid paint(Graphics g, int x, int y)paint(Graphics g, int x, int y)voidvoid remove(Layer l)remove(Layer l)voidvoid setViewWindow(int x, int y, int w, int setViewWindow(int x, int y, int w, int

h)h)

** leyer ** leyer ท�� ท�� append append ก�อน จะอยู่��ด้�านบนสุ�ด้ ก�อน จะอยู่��ด้�านบนสุ�ด้

Page 13: Mobile Gameand Application withJ2ME  - Collision Detection

LayerManager and Scrolling LayerManager and Scrolling backgroundbackground

Idea Idea ของการที่"า ของการที่"า scrolling background scrolling background ค�อค�อใช#ใช# method setViewWindow(sx,sy,w,h)method setViewWindow(sx,sy,w,h)

โด้ยโด้ย sx, sy sx, sy ค�อต"าแหน�งค�อต"าแหน�ง viewpoint viewpoint บนบน background background ที่��ต#องการแสด้งบนที่��ต#องการแสด้งบน screen.screen.

Page 14: Mobile Gameand Application withJ2ME  - Collision Detection

LayerManagerLayerManager

สร#าง สร#าง work space work space ช��อ ช��อ LayerManagerDemoLayerManagerDemo

สร#าง สร#าง J2ME Class J2ME Class ช��อ ช��อ LayerManagerMIDlet.javaLayerManagerMIDlet.java

โด้ย ม่�ต�วิ โด้ย ม่�ต�วิ SpriteSprite เป็�น เป็�น hero 2 hero 2 ต�วิแลื่ะม่� ต�วิแลื่ะม่� SpriteSprite 1 1 ต�วิเป็�น ต�วิเป็�น backgroundbackground

แลื่ะให#ใช#ส�ง แลื่ะให#ใช#ส�ง setViewWindow setViewWindow ในการเลื่�อนหน#าในการเลื่�อนหน#าจอไป็ซ้#ายแลื่ะขาวิจอไป็ซ้#ายแลื่ะขาวิ

Page 15: Mobile Gameand Application withJ2ME  - Collision Detection

LayerManagerMIDletLayerManagerMIDlet.java.javaimportimport java.io.IOException; java.io.IOException;importimport javax.microedition.lcdui.*; javax.microedition.lcdui.*;importimport javax.microedition.lcdui.game.*; javax.microedition.lcdui.game.*;importimport javax.microedition.midlet.*; javax.microedition.midlet.*;

publicpublic classclass LayerManagerMIDlet LayerManagerMIDlet extendsextends MIDlet { MIDlet {CanvasLayerManagerCanvasLayerManager canvas = canvas = newnew CanvasLayerManager ();CanvasLayerManager ();

publicpublic LayerManagerMIDletLayerManagerMIDlet() {}() {}protectedprotected voidvoid destroyApp( destroyApp(booleanboolean arg0) arg0) throwsthrows

MIDletStateChangeException {}MIDletStateChangeException {}protectedprotected voidvoid pauseApp() {} pauseApp() {}protectedprotected voidvoid startApp() startApp() throwsthrows MIDletStateChangeException MIDletStateChangeException

{{Display display = Display.Display display = Display.getDisplaygetDisplay((thisthis););canvas.start();canvas.start();Display.setCurrent(canvas);Display.setCurrent(canvas);

}}}}

Page 16: Mobile Gameand Application withJ2ME  - Collision Detection

classclass CanvasLayerManager CanvasLayerManager extendsextends GameCanvas GameCanvas implementsimplements Runnable { Runnable {

Sprite hero;Sprite hero;

Sprite hero2;Sprite hero2;

Sprite bg;Sprite bg;

booleanboolean running; running;

LayerManager layerManager;LayerManager layerManager;

intint sx,sy; sx,sy;

Page 17: Mobile Gameand Application withJ2ME  - Collision Detection

protectedprotected CanvasLayerManager() { CanvasLayerManager() {supersuper((truetrue););Image img_boy = Image img_boy = nullnull;;Image img_man = Image img_man = nullnull;;Image img_bg = Image img_bg = nullnull;;trytry { {

img_boy = Image.img_boy = Image.createImagecreateImage("/res/boy.png");("/res/boy.png");img_man = Image.img_man = Image.createImagecreateImage("/res/man.png");("/res/man.png");img_bg = Image.img_bg = Image.createImagecreateImage("/res/bg.jpg");("/res/bg.jpg");

} } catchcatch (IOException e) {} (IOException e) {}hero = hero = newnew

Sprite(img_boy,img_boy.getWidth()/4,img_boy.getHeight()/4);Sprite(img_boy,img_boy.getWidth()/4,img_boy.getHeight()/4);hero2 = hero2 = newnew

Sprite(img_man,img_man.getWidth()/4,img_man.getHeight()/4);Sprite(img_man,img_man.getWidth()/4,img_man.getHeight()/4);

bg = bg = newnew Sprite(img_bg); Sprite(img_bg);layerManager = layerManager = newnew LayerManager(); LayerManager();layerManager.append(hero);layerManager.append(hero);layerManager.append(hero2);layerManager.append(hero2);layerManager.append(bg);layerManager.append(bg);

}}

Page 18: Mobile Gameand Application withJ2ME  - Collision Detection

publicpublic voidvoid start() { start() {running = running = truetrue;;Thread t = Thread t = newnew Thread( Thread(thisthis););t.start();t.start();

}}publicpublic voidvoid run() { run() {

Graphics g = getGraphics();Graphics g = getGraphics();intint delay = 50; delay = 50;whilewhile(running){(running){

input();input();drawScreen(g);drawScreen(g);trytry { {

Thread.Thread.sleepsleep(delay);(delay);} } catchcatch (InterruptedException e) { (InterruptedException e) {

e.printStackTrace();e.printStackTrace();}}

}}}}

Page 19: Mobile Gameand Application withJ2ME  - Collision Detection

privateprivate voidvoid input() { input() {intint keyState = getKeyStates(); keyState = getKeyStates();ifif(keyState== (keyState== LEFT_PRESSEDLEFT_PRESSED){){

sx = sx - 5;sx = sx - 5;}}elseelse ifif(keyState== (keyState== RIGHT_PRESSEDRIGHT_PRESSED){){

sx = sx + 5;sx = sx + 5;}}

}}privateprivate voidvoid drawScreen(Graphics g) { drawScreen(Graphics g) {

intint w = getWidth(); w = getWidth();intint h = getHeight(); h = getHeight();hero.setPosition(w/2 + sx, h/2 );hero.setPosition(w/2 + sx, h/2 );hero2.setPosition(w/2 + 50, h/2 - 20);hero2.setPosition(w/2 + 50, h/2 - 20);bg.setPosition(-50, -50);bg.setPosition(-50, -50);layerManager.setViewWindow(sx,sy,w,h);layerManager.setViewWindow(sx,sy,w,h);layerManager.paint(g,0,0);layerManager.paint(g,0,0);flushGraphics();flushGraphics();

}}}}

Page 20: Mobile Gameand Application withJ2ME  - Collision Detection

ใช# ใช# TiledLayerTiledLayer ที่"า ที่"า backgroundbackground

Page 21: Mobile Gameand Application withJ2ME  - Collision Detection

TiledLayerTiledLayer

Page 22: Mobile Gameand Application withJ2ME  - Collision Detection

TiledLayerTiledLayer

map[] ={4 , 5 , 5 , 5 , 5 , 5 , 5 , 4 , 4 , 4 ,4 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 4 ,4 , 5 , 5 , 5 , 2 , 2 , 5 , 5 , 5 , 4 ,4 , 5 , 5 , 5 , 2 , 2 , 5 , 5 , 5 , 4 ,4 , 4 , 5 , 5 , 2 , 2 , 5 , 5 , 5 , 4 ,4 , 4 , 4 , 5 , 5 , 2 , 2 , 2 , 5 , 4 ,1 , 1 , 3 , 3 , 5 , 5 , 2 , 2 , 5 , 4 ,3 , 1 , 3 , 3 , 3 , 5 , 5 , 5 , 5 , 4 ,3 , 1 , 1 , 3 , 3 , 3 , 5 , 5 , 1 , 1 ,3 , 3 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 3 ,3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 }

Page 23: Mobile Gameand Application withJ2ME  - Collision Detection

สร#างแผนที่�� สร#างแผนที่�� (world)(world) เราสาม่ารถสร#าง เราสาม่ารถสร#าง worldworld หร�อ หร�อ mapmap โด้ยใช# โด้ยใช#

Mappy win32Mappy win32

Page 24: Mobile Gameand Application withJ2ME  - Collision Detection

ผลื่จาก ผลื่จาก export text file export text file ในร0ป็แบบ ในร0ป็แบบ arrayarray

const short mymap_map0[10][10] = const short mymap_map0[10][10] = {{

{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },{ 2, 4, 4, 4, 4, 4, 4, 4, 4, 2 },{ 2, 4, 4, 4, 4, 4, 4, 4, 4, 2 },{ 2, 4, 4, 3, 5, 5, 5, 3, 4, 2 },{ 2, 4, 4, 3, 5, 5, 5, 3, 4, 2 },{ 2, 5, 4, 3, 3, 3, 5, 3, 4, 2 },{ 2, 5, 4, 3, 3, 3, 5, 3, 4, 2 },{ 2, 1, 4, 4, 4, 3, 5, 3, 4, 2 },{ 2, 1, 4, 4, 4, 3, 5, 3, 4, 2 },{ 2, 1, 1, 1, 4, 3, 5, 3, 4, 2 },{ 2, 1, 1, 1, 4, 3, 5, 3, 4, 2 },{ 2, 1, 1, 1, 4, 3, 3, 3, 4, 2 },{ 2, 1, 1, 1, 4, 3, 3, 3, 4, 2 },{ 2, 1, 1, 1, 4, 3, 3, 3, 4, 2 },{ 2, 1, 1, 1, 4, 3, 3, 3, 4, 2 },{ 2, 1, 1, 1, 4, 4, 5, 4, 4, 2 },{ 2, 1, 1, 1, 4, 4, 5, 4, 4, 2 },{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }};{ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }};

Page 25: Mobile Gameand Application withJ2ME  - Collision Detection

การใช#งาน การใช#งาน TiledLayerTiledLayer

TiledLayer background, land;TiledLayer background, land;

intint[][] map = {[][] map = {

{ 5, 5, 5, 4, 4, 4, 5, 5, 5, 2, 2, 2, 2, 2, 5, 2, { 5, 5, 5, 4, 4, 4, 5, 5, 5, 2, 2, 2, 2, 2, 5, 2,

2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 },

{ 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 2, 5, 2, { 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 2, 2, 5, 2,

2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2 },2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2 },

{ 5, 5, 5, 5, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}}{ 5, 5, 5, 5, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}}

background = new TiledLayer(30, 30, img_bg, background = new TiledLayer(30, 30, img_bg, img_bg.getWidth()/5,img_bg.getHeight());img_bg.getWidth()/5,img_bg.getHeight());

background.setCell(i, j, map[i][j]);background.setCell(i, j, map[i][j]);

Page 26: Mobile Gameand Application withJ2ME  - Collision Detection

TiledLayerMIDletTiledLayerMIDlet

ให#สร#าง ให#สร#าง work space work space ช��อวิ�า ช��อวิ�า TiledLayerDemoTiledLayerDemo

ให#สร#าง ให#สร#าง J2ME Class J2ME Class ช��อวิ�า ช��อวิ�า TiledLayerMIDletTiledLayerMIDlet.java.java

ใช# ใช# TiledLayerTiledLayer ที่"า ที่"า backgroundbackground

Page 27: Mobile Gameand Application withJ2ME  - Collision Detection

TiledLayerMIDlet.javaTiledLayerMIDlet.javaimportimport java.io.IOException; java.io.IOException;importimport javax.microedition.lcdui.*; javax.microedition.lcdui.*;importimport javax.microedition.lcdui.game.*; javax.microedition.lcdui.game.*;importimport javax.microedition.midlet.*; javax.microedition.midlet.*;

publicpublic classclass TiledLayerMIDlet TiledLayerMIDlet extendsextends MIDlet { MIDlet {CanvasTiledLayer canvas = CanvasTiledLayer canvas = newnew CanvasTiledLayer(); CanvasTiledLayer();publicpublic TiledLayerMIDlet() {} TiledLayerMIDlet() {}protectedprotected voidvoid destroyApp( destroyApp(booleanboolean arg0) arg0) throwsthrows MIDletStateChangeException {}MIDletStateChangeException {}protectedprotected voidvoid pauseApp() {} pauseApp() {}protectedprotected voidvoid startApp() startApp() throwsthrows MIDletStateChangeException {MIDletStateChangeException {

Display display = Display.Display display = Display.getDisplaygetDisplay((thisthis););canvas.start();canvas.start();display.setCurrent(canvas);display.setCurrent(canvas);

}}}}

Page 28: Mobile Gameand Application withJ2ME  - Collision Detection

classclass CanvasTiledLayer CanvasTiledLayer extendsextends GameCanvas GameCanvas implementsimplements Runnable { Runnable {

staticstatic intint FRONT_DIRECTIONFRONT_DIRECTION = 0; = 0;staticstatic intint LEFT_DIRECTIONLEFT_DIRECTION = 1; = 1;staticstatic intint RIGHT_DIRECTIONRIGHT_DIRECTION = 2; = 2;staticstatic intint BACK_DIRECTIONBACK_DIRECTION = 3; = 3;Sprite ship;Sprite ship;TiledLayer waster, land;TiledLayer waster, land;LayerManager layerManager;LayerManager layerManager;intint w, h; w, h;intint cx,cy; cx,cy;intint currentDirection = currentDirection = FRONT_DIRECTIONFRONT_DIRECTION;;booleanboolean running; running;intint[][] sequence = {{0,1,2,3},[][] sequence = {{0,1,2,3}, //front//front

{4,5,6,7},{4,5,6,7}, //left//left{8,9,10,11},{8,9,10,11}, //right//right{12,13,14,15}};{12,13,14,15}}; //back//back

Page 29: Mobile Gameand Application withJ2ME  - Collision Detection

intint[][] obstruction_land = {[][] obstruction_land = {{ 2, 2, 0, 0, 0, 0, 0, 0, 0, 4, 4, 2, 2, 2, 2, 5, 0, 0, 2, 2 },{ 2, 2, 0, 0, 0, 0, 0, 0, 0, 4, 4, 2, 2, 2, 2, 5, 0, 0, 2, 2 },{ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 2, 2, 5, 5, 0, 0, 0, 2 },{ 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 4, 2, 2, 5, 5, 0, 0, 0, 2 },{ 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, 0, 3, 2, 5, 3, 0, 0, 0, 0, 0 },{ 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, 0, 3, 2, 5, 3, 0, 0, 0, 0, 0 },{ 0, 0, 0, 2, 5, 5, 3, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 0, 2, 5, 5, 3, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 4, 2, 2, 2, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },{ 0, 0, 4,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1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,

1 }};1 }};

Page 31: Mobile Gameand Application withJ2ME  - Collision Detection

protectedprotected CanvasTiledLayer() { CanvasTiledLayer() {supersuper((truetrue););Image img_ship = Image img_ship = nullnull; Image img_bg = ; Image img_bg = nullnull;;w = getWidth(); h = getHeight(); cx = w/2; cy = h/2;w = getWidth(); h = getHeight(); cx = w/2; cy = h/2;

trytry { {img_ship = Image.img_ship = Image.createImagecreateImage("/res/Ship.png");("/res/Ship.png");img_bg = Image.img_bg = Image.createImagecreateImage("/res/bgtiles.png");("/res/bgtiles.png");

} } catchcatch (IOException e) {} (IOException e) {}ship = ship = newnew Sprite(img_ship,img_ship.getWidth()/4,img_ship.getHeight()/4);Sprite(img_ship,img_ship.getWidth()/4,img_ship.getHeight()/4);land = land = newnew TiledLayer(20, 20, img_bg, TiledLayer(20, 20, img_bg, img_bg.getWidth()/5,img_bg.getHeight());img_bg.getWidth()/5,img_bg.getHeight());waster = waster = newnew TiledLayer(20, 20, img_bg, TiledLayer(20, 20, img_bg, img_bg.getWidth()/5,img_bg.getHeight());img_bg.getWidth()/5,img_bg.getHeight());forfor ( (intint i = 0; i < 20;i++) { i = 0; i < 20;i++) {

forfor ( (intint j = 0; j <20;j++) { j = 0; j <20;j++) {land.setCell(i, j, obstruction_land[i][j]); }}land.setCell(i, j, obstruction_land[i][j]); }}

forfor ( (intint i = 0; i < 20;i++) { i = 0; i < 20;i++) {forfor ( (intint j = 0; j < 20;j++) { j = 0; j < 20;j++) {

waster.setCell(i, j, waster_map[i][j]); }}waster.setCell(i, j, waster_map[i][j]); }}ship.setFrameSequence(sequence[0]);ship.setFrameSequence(sequence[0]);layerManager = layerManager = newnew LayerManager(); LayerManager();layerManager.append(ship); layerManager.append(land); layerManager.append(ship); layerManager.append(land); layerManager.append(waster);layerManager.append(waster);

}}

Page 32: Mobile Gameand Application withJ2ME  - Collision Detection

publicpublic voidvoid run() { run() {Graphics g = getGraphics();Graphics g = getGraphics();intint delay = 100; delay = 100;whilewhile(running){(running){

getInput();getInput();drawScreen(g);drawScreen(g);trytry {Thread. {Thread.sleepsleep(delay);} (delay);} catchcatch (InterruptedException (InterruptedException

e) {}e) {}}}

}}voidvoid drawScreen(Graphics g) { drawScreen(Graphics g) {

intint w = getWidth(); w = getWidth();intint h = getHeight(); h = getHeight();intint xpoint = 120; xpoint = 120;intint ypoint = 70; ypoint = 70;g.setColor(0);g.setColor(0);g.fillRect(0, 0, w, h);g.fillRect(0, 0, w, h);

ship.setPosition((w/2) + xpoint + (cx /2) - 12, (h/2)+ ypoint + (cy/2) - ship.setPosition((w/2) + xpoint + (cx /2) - 12, (h/2)+ ypoint + (cy/2) - 24 );24 );layerManager.paint(g, -(xpoint) - (cx/2) , -(ypoint) - (cy/2));layerManager.paint(g, -(xpoint) - (cx/2) , -(ypoint) - (cy/2));g.setColor(255,255,255);g.setColor(255,255,255);g.drawString(" ["+cx+","+cy+"]", 0, getHeight()- 20, g.g.drawString(" ["+cx+","+cy+"]", 0, getHeight()- 20, g.TOPTOP | g. | g.LEFTLEFT););flushGraphics();flushGraphics();

}}

Page 33: Mobile Gameand Application withJ2ME  - Collision Detection

voidvoid getInput() { getInput() {intint keyState = getKeyStates(); keyState = getKeyStates();intint cDirection = currentDirection; cDirection = currentDirection;

ifif(keyState== (keyState== LEFT_PRESSEDLEFT_PRESSED){){currentDirection= currentDirection= LEFT_DIRECTIONLEFT_DIRECTION;;ifif(!ship.collidesWith(land, (!ship.collidesWith(land, truetrue)){)){

cx = cx - 10;cx = cx - 10;} } else else {cx = cx + 20;}{cx = cx + 20;}ship.nextFrame();ship.nextFrame();

}}elseelse ifif(keyState== (keyState== RIGHT_PRESSEDRIGHT_PRESSED){){

currentDirection= currentDirection= RIGHT_DIRECTIONRIGHT_DIRECTION;;ifif(!ship.collidesWith(land, (!ship.collidesWith(land, truetrue)){)){cx = cx + 10;cx = cx + 10;} } else else {cx = cx - 20;}{cx = cx - 20;}ship.nextFrame();ship.nextFrame();

}}

Page 34: Mobile Gameand Application withJ2ME  - Collision Detection

elseelse if if (keyState== (keyState== UP_PRESSEDUP_PRESSED) {) {currentDirection= currentDirection= BACK_DIRECTIONBACK_DIRECTION;;

ifif(!ship.collidesWith(land, (!ship.collidesWith(land, truetrue)){)){cy = cy - 10;cy = cy - 10;

}}elseelse{cy = cy + 20;}{cy = cy + 20;}ship.nextFrame();ship.nextFrame();

}}elseelse ifif(keyState== (keyState== DOWN_PRESSEDDOWN_PRESSED){){

currentDirection= currentDirection= FRONT_DIRECTIONFRONT_DIRECTION;;ifif(!ship.collidesWith(land, (!ship.collidesWith(land, truetrue)){)){

cy = cy + 10;cy = cy + 10;}}elseelse{cy = cy - 20;}{cy = cy - 20;}ship.nextFrame();ship.nextFrame();}}

ifif(cDirection != currentDirection){(cDirection != currentDirection){

ship.setFrameSequence(sequence[currentDirection]);ship.setFrameSequence(sequence[currentDirection]);}}

}}

Page 35: Mobile Gameand Application withJ2ME  - Collision Detection

publicpublic voidvoid start() { start() {

running = running = truetrue;;

Thread t = Thread t = newnew Thread(Thread(thisthis););

t.start();t.start();

} }

}}

Page 36: Mobile Gameand Application withJ2ME  - Collision Detection

แนะน"าหน�งส�อ แนะน"าหน�งส�อ J2MEJ2ME สร#างเกม่แลื่ะโป็รแกรม่ด้#วิย สร#างเกม่แลื่ะโป็รแกรม่ด้#วิย J2ME J2ME ค)ณก2ที่"าได้# ค)ณก2ที่"าได้# เข�ยนเกม่แลื่ะโป็รแกรม่แบบม่�อถ�อ เข�ยนเกม่แลื่ะโป็รแกรม่แบบม่�อถ�อ J2ME + CD J2ME + CD J2ME J2ME ค0�ม่�อส"าหร�บเร��ม่ต#น พ�ฒนาจาวิาบนม่�อ ค0�ม่�อส"าหร�บเร��ม่ต#น พ�ฒนาจาวิาบนม่�อ เก�ง เก�ง J2ME J2ME ให#ครบส0ตรให#ครบส0ตร + CD+ CD เข�ยนโป็รแกรม่บนโที่รศั�พที่�เคลื่��อนที่��ด้#วิยเข�ยนโป็รแกรม่บนโที่รศั�พที่�เคลื่��อนที่��ด้#วิย J2ME J2ME