Submit Search
Upload
Mobile Gameand Application withJ2ME - Collision Detection
•
Download as PPT, PDF
•
1 like
•
810 views
J
Jenchoke Tachagomain
Follow
Collision Detection
Read less
Read more
Report
Share
Report
Share
1 of 36
Download now
Recommended
The Ring programming language version 1.7 book - Part 63 of 196
The Ring programming language version 1.7 book - Part 63 of 196
Mahmoud Samir Fayed
Graphics programs
Graphics programs
NAVYA RAO
Ocr code
Ocr code
wi7sonjoseph
Making Games in JavaScript
Making Games in JavaScript
Sam Cartwright
Gems of GameplayKit. UA Mobile 2017.
Gems of GameplayKit. UA Mobile 2017.
UA Mobile
Exploring Canvas
Exploring Canvas
Kevin Hoyt
computer graphics practicals
computer graphics practicals
Manoj Chauhan
Computer graphics lab assignment
Computer graphics lab assignment
Abdullah Al Shiam
Recommended
The Ring programming language version 1.7 book - Part 63 of 196
The Ring programming language version 1.7 book - Part 63 of 196
Mahmoud Samir Fayed
Graphics programs
Graphics programs
NAVYA RAO
Ocr code
Ocr code
wi7sonjoseph
Making Games in JavaScript
Making Games in JavaScript
Sam Cartwright
Gems of GameplayKit. UA Mobile 2017.
Gems of GameplayKit. UA Mobile 2017.
UA Mobile
Exploring Canvas
Exploring Canvas
Kevin Hoyt
computer graphics practicals
computer graphics practicals
Manoj Chauhan
Computer graphics lab assignment
Computer graphics lab assignment
Abdullah Al Shiam
Introduction to Game Programming Tutorial
Introduction to Game Programming Tutorial
Richard Jones
Cg my own programs
Cg my own programs
Amit Kapoor
Computer graphics programs in c++
Computer graphics programs in c++
Ankit Kumar
OOXX
OOXX
Weihong Lee
Computer graphics File for Engineers
Computer graphics File for Engineers
varun arora
Computer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
Kandarp Tiwari
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithm
Kapil Pandit
ماترێکس به کوردی ئارام
ماترێکس به کوردی ئارام
Aram Jamal
ES6(ES2015) is beautiful
ES6(ES2015) is beautiful
monikagupta18jan
Intro to Game Programming
Intro to Game Programming
Richard Jones
The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202
Mahmoud Samir Fayed
SE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of Pune
Bhavesh Shah
Computer graphics
Computer graphics
Prianka Padmanaban
662305 LAB12
662305 LAB12
Nitigan Nakjuatong
Graphics point clipping c program
Graphics point clipping c program
Dr.M.Karthika parthasarathy
Hypercritical C++ Code Review
Hypercritical C++ Code Review
Andrey Karpov
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
tutorialsruby
Mini-curso JavaFX Aula1
Mini-curso JavaFX Aula1
Raphael Marques
My favorite slides
My favorite slides
Mitchell Wand
Android architecture
Android architecture
Jenchoke Tachagomain
J2 Me Mobile Application
J2 Me Mobile Application
Imranahmed_19
Session10 J2ME Record Management System
Session10 J2ME Record Management System
muthusvm
More Related Content
What's hot
Introduction to Game Programming Tutorial
Introduction to Game Programming Tutorial
Richard Jones
Cg my own programs
Cg my own programs
Amit Kapoor
Computer graphics programs in c++
Computer graphics programs in c++
Ankit Kumar
OOXX
OOXX
Weihong Lee
Computer graphics File for Engineers
Computer graphics File for Engineers
varun arora
Computer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
Kandarp Tiwari
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithm
Kapil Pandit
ماترێکس به کوردی ئارام
ماترێکس به کوردی ئارام
Aram Jamal
ES6(ES2015) is beautiful
ES6(ES2015) is beautiful
monikagupta18jan
Intro to Game Programming
Intro to Game Programming
Richard Jones
The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202
Mahmoud Samir Fayed
SE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of Pune
Bhavesh Shah
Computer graphics
Computer graphics
Prianka Padmanaban
662305 LAB12
662305 LAB12
Nitigan Nakjuatong
Graphics point clipping c program
Graphics point clipping c program
Dr.M.Karthika parthasarathy
Hypercritical C++ Code Review
Hypercritical C++ Code Review
Andrey Karpov
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
tutorialsruby
Mini-curso JavaFX Aula1
Mini-curso JavaFX Aula1
Raphael Marques
My favorite slides
My favorite slides
Mitchell Wand
What's hot
(19)
Introduction to Game Programming Tutorial
Introduction to Game Programming Tutorial
Cg my own programs
Cg my own programs
Computer graphics programs in c++
Computer graphics programs in c++
OOXX
OOXX
Computer graphics File for Engineers
Computer graphics File for Engineers
Computer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
Wap in c to draw a line using DDA algorithm
Wap in c to draw a line using DDA algorithm
ماترێکس به کوردی ئارام
ماترێکس به کوردی ئارام
ES6(ES2015) is beautiful
ES6(ES2015) is beautiful
Intro to Game Programming
Intro to Game Programming
The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202
SE Computer, Programming Laboratory(210251) University of Pune
SE Computer, Programming Laboratory(210251) University of Pune
Computer graphics
Computer graphics
662305 LAB12
662305 LAB12
Graphics point clipping c program
Graphics point clipping c program
Hypercritical C++ Code Review
Hypercritical C++ Code Review
building_games_with_ruby_rubyconf
building_games_with_ruby_rubyconf
Mini-curso JavaFX Aula1
Mini-curso JavaFX Aula1
My favorite slides
My favorite slides
Viewers also liked
Android architecture
Android architecture
Jenchoke Tachagomain
J2 Me Mobile Application
J2 Me Mobile Application
Imranahmed_19
Session10 J2ME Record Management System
Session10 J2ME Record Management System
muthusvm
Session11 J2ME Record Management System
Session11 J2ME Record Management System
muthusvm
Session11 J2ME Record Management System Database
Session11 J2ME Record Management System Database
muthusvm
Day 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through Activities
Ahsanul Karim
Session11 J2ME MID-Low Level User Interface(LLUI)-graphics
Session11 J2ME MID-Low Level User Interface(LLUI)-graphics
muthusvm
Session8 J2ME Low Level User Interface
Session8 J2ME Low Level User Interface
muthusvm
Session6 J2ME High Level User Interface(HLUI) part1
Session6 J2ME High Level User Interface(HLUI) part1
muthusvm
Java 7 Language Enhancement
Java 7 Language Enhancement
muthusvm
Session5 J2ME High Level User Interface(HLUI) part1
Session5 J2ME High Level User Interface(HLUI) part1
muthusvm
Session12 J2ME Generic Connection Framework
Session12 J2ME Generic Connection Framework
muthusvm
Session13 J2ME Timer
Session13 J2ME Timer
muthusvm
Session9 J2ME Record Management System
Session9 J2ME Record Management System
muthusvm
Lecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting Started
Ahsanul Karim
Lecture 3 getting active through activities
Lecture 3 getting active through activities
Ahsanul Karim
Session7 J2ME High Level User Interface(HLUI) part1-2
Session7 J2ME High Level User Interface(HLUI) part1-2
muthusvm
Lecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick Overview
Ahsanul Karim
Android programming
Android programming
Jenchoke Tachagomain
Day 1 Android: Before Getting Started
Day 1 Android: Before Getting Started
Ahsanul Karim
Viewers also liked
(20)
Android architecture
Android architecture
J2 Me Mobile Application
J2 Me Mobile Application
Session10 J2ME Record Management System
Session10 J2ME Record Management System
Session11 J2ME Record Management System
Session11 J2ME Record Management System
Session11 J2ME Record Management System Database
Session11 J2ME Record Management System Database
Day 4: Android: Getting Active through Activities
Day 4: Android: Getting Active through Activities
Session11 J2ME MID-Low Level User Interface(LLUI)-graphics
Session11 J2ME MID-Low Level User Interface(LLUI)-graphics
Session8 J2ME Low Level User Interface
Session8 J2ME Low Level User Interface
Session6 J2ME High Level User Interface(HLUI) part1
Session6 J2ME High Level User Interface(HLUI) part1
Java 7 Language Enhancement
Java 7 Language Enhancement
Session5 J2ME High Level User Interface(HLUI) part1
Session5 J2ME High Level User Interface(HLUI) part1
Session12 J2ME Generic Connection Framework
Session12 J2ME Generic Connection Framework
Session13 J2ME Timer
Session13 J2ME Timer
Session9 J2ME Record Management System
Session9 J2ME Record Management System
Lecture 1 Session 1 Before Getting Started
Lecture 1 Session 1 Before Getting Started
Lecture 3 getting active through activities
Lecture 3 getting active through activities
Session7 J2ME High Level User Interface(HLUI) part1-2
Session7 J2ME High Level User Interface(HLUI) part1-2
Lecture 2(b) Android Internals A Quick Overview
Lecture 2(b) Android Internals A Quick Overview
Android programming
Android programming
Day 1 Android: Before Getting Started
Day 1 Android: Before Getting Started
Similar to Mobile Gameand Application withJ2ME - Collision Detection
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
apexcomputer54
662305 LAB13
662305 LAB13
Nitigan Nakjuatong
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
anjandavid
import java.awt.;import java.awt.event.MouseAdaptor;import java.pdf
import java.awt.;import java.awt.event.MouseAdaptor;import java.pdf
anyacarpets
import java.util.Scanner;public class Main { public static in.pdf
import java.util.Scanner;public class Main { public static in.pdf
anwarsadath111
Aditazz 01-ul
Aditazz 01-ul
michaelsbergin
Creating an Uber Clone - Part VIII.pdf
Creating an Uber Clone - Part VIII.pdf
ShaiAlmog1
Mashup caravan android-talks
Mashup caravan android-talks
honjo2
MIDP: Game API
MIDP: Game API
Jussi Pohjolainen
I wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdf
feelinggifts
Clock For My
Clock For My
shamalanamnam
The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212
Mahmoud Samir Fayed
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
TashiBhutia12
Lec 7 28_aug [compatibility mode]
Lec 7 28_aug [compatibility mode]
Palak Sanghani
Computer graphics lab manual
Computer graphics lab manual
Uma mohan
need help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdf
arcotstarsports
Creating an Uber Clone - Part IV.pdf
Creating an Uber Clone - Part IV.pdf
ShaiAlmog1
Following are the changes mentioned in bold in order to obtain the r.pdf
Following are the changes mentioned in bold in order to obtain the r.pdf
anithareadymade
Leaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGL
gerbille
Html5 game programming overview
Html5 game programming overview
민태 김
Similar to Mobile Gameand Application withJ2ME - Collision Detection
(20)
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
662305 LAB13
662305 LAB13
This is Java,I am currently stumped on how to add a scoreboard for.pdf
This is Java,I am currently stumped on how to add a scoreboard for.pdf
import java.awt.;import java.awt.event.MouseAdaptor;import java.pdf
import java.awt.;import java.awt.event.MouseAdaptor;import java.pdf
import java.util.Scanner;public class Main { public static in.pdf
import java.util.Scanner;public class Main { public static in.pdf
Aditazz 01-ul
Aditazz 01-ul
Creating an Uber Clone - Part VIII.pdf
Creating an Uber Clone - Part VIII.pdf
Mashup caravan android-talks
Mashup caravan android-talks
MIDP: Game API
MIDP: Game API
I wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdf
Clock For My
Clock For My
The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.10 book - Part 70 of 212
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
COMPAPPABCA49085rFunrAP__Practical Number 9 & 10.docx
Lec 7 28_aug [compatibility mode]
Lec 7 28_aug [compatibility mode]
Computer graphics lab manual
Computer graphics lab manual
need help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdf
Creating an Uber Clone - Part IV.pdf
Creating an Uber Clone - Part IV.pdf
Following are the changes mentioned in bold in order to obtain the r.pdf
Following are the changes mentioned in bold in order to obtain the r.pdf
Leaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGL
Html5 game programming overview
Html5 game programming overview
More from Jenchoke Tachagomain
Digital Transformation
Digital Transformation
Jenchoke Tachagomain
Lect 08 Css
Lect 08 Css
Jenchoke Tachagomain
Lect07 Page Design
Lect07 Page Design
Jenchoke Tachagomain
Lect06 Web Design
Lect06 Web Design
Jenchoke Tachagomain
Introduction to On-line Documemt Lect05 Web Process
Introduction to On-line Documemt Lect05 Web Process
Jenchoke Tachagomain
Introduction to On-line Documemt Lect03 E Commerce
Introduction to On-line Documemt Lect03 E Commerce
Jenchoke Tachagomain
Introduction to On-line Documemt Lec02
Introduction to On-line Documemt Lec02
Jenchoke Tachagomain
Introduction to On-line Documemt Lab 4
Introduction to On-line Documemt Lab 4
Jenchoke Tachagomain
Introduction to On-line Documemt Lab 3
Introduction to On-line Documemt Lab 3
Jenchoke Tachagomain
Lab 2 For Css
Lab 2 For Css
Jenchoke Tachagomain
Rss
Rss
Jenchoke Tachagomain
Digital Content Business
Digital Content Business
Jenchoke Tachagomain
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 09
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 09
Jenchoke Tachagomain
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 08
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 08
Jenchoke Tachagomain
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 07
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 07
Jenchoke Tachagomain
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 06
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 06
Jenchoke Tachagomain
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 05
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 05
Jenchoke Tachagomain
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 04
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 04
Jenchoke Tachagomain
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 03
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 03
Jenchoke Tachagomain
Communication Concept
Communication Concept
Jenchoke Tachagomain
More from Jenchoke Tachagomain
(20)
Digital Transformation
Digital Transformation
Lect 08 Css
Lect 08 Css
Lect07 Page Design
Lect07 Page Design
Lect06 Web Design
Lect06 Web Design
Introduction to On-line Documemt Lect05 Web Process
Introduction to On-line Documemt Lect05 Web Process
Introduction to On-line Documemt Lect03 E Commerce
Introduction to On-line Documemt Lect03 E Commerce
Introduction to On-line Documemt Lec02
Introduction to On-line Documemt Lec02
Introduction to On-line Documemt Lab 4
Introduction to On-line Documemt Lab 4
Introduction to On-line Documemt Lab 3
Introduction to On-line Documemt Lab 3
Lab 2 For Css
Lab 2 For Css
Rss
Rss
Digital Content Business
Digital Content Business
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 09
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 09
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 08
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 08
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 07
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 07
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 06
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 06
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 05
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 05
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 04
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 04
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 03
การพัฒนาเอกสารออนไลน์ขั้นสูง Lect 03
Communication Concept
Communication Concept
Mobile Gameand Application withJ2ME - Collision Detection
1.
J2ME Mobile games
2.
การตรวจพบการกระทบกัน
(Collision Detection) เป็นวิธีการตรวจสอบว่า สไปรต์ (Sprite) เคลื่อนที่ กระทบกับ สไปรต์ (Sprite) ตัวอื่นหรือไม่ มีประโยชน์มากในการนำาไปใช้กับภาพเคลื่อนไหว ของเกมส์ แต่ละสไปรต์ (Sprite) จะมีขอบเขตเป็น สีเหลี่ยมมุมฉาก บางส่วนของภาพจะโปร่งแสง ่ เมื่อเคลื่อนที่กระทบกันจะทำาให้มุมของสีเหลี่ยม ่ มุมฉากเหลื่อมลำำากัน ทำาให้ตรวจพบได้ทันทีว่ามี การกระทบกัน
3.
Collision Detection
( ง Work space ชื่อ Sprite) สร้าSprite Vs Collision สร้าง J2ME Class ชือ ่ SpriteCollideMIDlet.java สร้าง GameCanvas โดยให้แสดง sprite สอง ตัว หาก sprite ชน อีกตัว ให้หยุดเดิน
4.
SpriteCollideMIDlet.java import java.io.IOException; import javax.microedition.lcdui.*; import
javax.microedition.lcdui.game.*; import javax.microedition.midlet.*; public class SpriteCollideMIDlet extends MIDlet { SpriteCollideCanvas canvas = new SpriteCollideCanvas(); public SpriteCollideMIDlet() {} protected void destroyApp(boolean arg0) throws MIDletStateChangeException {} protected void pauseApp() {} protected void startApp() throws MIDletStateChangeException { Display display = Display.getDisplay(this); canvas.start(); display.setCurrent(canvas); } }
5.
class SpriteCollideCanvas extends
GameCanvas implements Runnable { static int FRONT_DIRECTION = 0; static int LEFT_DIRECTION = 1; static int RIGHT_DIRECTION = 2; static int BACK_DIRECTION = 3; Sprite hero,hero2; boolean running; int[][] sequence = {{0,1,2,3}, //front {4,5,6,7}, //left {8,9,10,11}, //right {12,13,14,15}}; //back int s2[] ={8,9,10,11}; int w, h; Image img_bg = null; int cx,cy,cx2,cy2; int currentDirection = FRONT_DIRECTION;
6.
protected SpriteCollideCanvas() {
super(true); Image im = null, hm2 = null; try { im = Image.createImage("/sprites/c1.png"); hm2 = Image.createImage("/sprites/c4.png"); img_bg = Image.createImage("/world/roundedbg.png"); } catch (IOException e) {} hero = new Sprite(im,32,48); hero2 = new Sprite(hm2,32,48); w = getWidth(); h = getHeight(); cx = w/2; cy = h/2; hero.setFrameSequence(sequence[0]); hero2.setFrameSequence(s2); }
7.
public void start(){
running = true; Thread t = new Thread(this); t.start(); } public void run() { Graphics g = getGraphics(); int delay = 100; while(running) { moving(); getInput(); hero2.nextFrame(); drawScreen(g); try {Thread.sleep(delay);} catch (InterruptedException e) {} } }
8.
void moving() {
if (cx2 > getWidth()) { cx2 = 0; } if ( ! hero2.collidesWith(hero, false)) cx2++; }
9.
void getInput(){
int keyState = getKeyStates(); int cDirection = currentDirection; // then we know current direction if(keyState== LEFT_PRESSED) { if( ! hero.collidesWith(hero2, false)) { cx = cx - 5; cx = Math.max (0, cx); currentDirection= LEFT_DIRECTION; } hero.nextFrame(); } else if(keyState== RIGHT_PRESSED){ cx = cx + 5; cx = Math.min (cx, w-32); currentDirection= RIGHT_DIRECTION; hero.nextFrame(); } else if(keyState== UP_PRESSED) { cy = cy - 5; cy = Math.max (0, cy); currentDirection= BACK_DIRECTION; hero.nextFrame(); } else if(keyState== DOWN_PRESSED){ cy = cy + 5; cy = Math.min(cy, h - 50); currentDirection= FRONT_DIRECTION; hero.nextFrame(); } if(cDirection != currentDirection){ hero.setFrameSequence(sequence[currentDirection]); }
10.
void drawScreen(Graphics g){
g.drawImage(img_bg, 0 - cx,0 - cy, Graphics.TOP | Graphics.LEFT); hero2.setPosition(cx2, cy2 + 20); hero2.paint(g); hero.setPosition(cx, cy); hero.paint(g); flushGraphics(); } }
11.
LayerManager
12.
LayerManager methods void append(Layer
l) Layer getLayerAt(int index) int getSize() void insert(Layer l, int index) void paint(Graphics g, int x, int y) void remove(Layer l) void setViewWindow(int x, int y, int w, int h) ** leyer ท่ี append ก่อน จะอย่่ด้านบนสุด
13.
LayerManager and Scrolling
background Idea ของการทำา scrolling background คือใช้ method setViewWindow(sx,sy,w,h) โดย sx, sy คือตำาแหน่ ง viewpoint บน background ท่ต้องการแสดงบน screen. ี
14.
LayerManager
สร้าง work space ชือ LayerManagerDemo ่ สร้างJ2ME Class ชื่อ LayerManagerMIDlet.java โดย มีตัว Sprite เป็น hero 2 ตัวและมี Sprite 1 ตัวเป็น background และให้ใช้สง setViewWindow ในการเลือนหน้า ั จอไปซ้ายและขาว
15.
LayerManagerMIDlet.java import java.io.IOException; import javax.microedition.lcdui.*; import
javax.microedition.lcdui.game.*; import javax.microedition.midlet.*; public class LayerManagerMIDlet extends MIDlet { CanvasLayerManager canvas = new CanvasLayerManager (); public LayerManagerMIDlet() {} protected void destroyApp(boolean arg0) throws MIDletStateChangeException {} protected void pauseApp() {} protected void startApp() throws MIDletStateChangeException { Display display = Display.getDisplay(this); canvas.start(); Display.setCurrent(canvas); } }
16.
class CanvasLayerManager extends
GameCanvas implements Runnable { Sprite hero; Sprite hero2; Sprite bg; boolean running; LayerManager layerManager; int sx,sy;
17.
protected CanvasLayerManager() {
super(true); Image img_boy = null; Image img_man = null; Image img_bg = null; try { img_boy = Image.createImage("/res/boy.png"); img_man = Image.createImage("/res/man.png"); img_bg = Image.createImage("/res/bg.jpg"); } catch (IOException e) {} hero = new Sprite(img_boy,img_boy.getWidth()/4,img_boy.getHeight()/4); hero2 = new Sprite(img_man,img_man.getWidth()/4,img_man.getHeight()/4); bg = new Sprite(img_bg); layerManager = new LayerManager(); layerManager.append(hero); layerManager.append(hero2); layerManager.append(bg); }
18.
public void start()
{ running = true; Thread t = new Thread(this); t.start(); } public void run() { Graphics g = getGraphics(); int delay = 50; while(running){ input(); drawScreen(g); try { Thread.sleep(delay); } catch (InterruptedException e) { e.printStackTrace(); } } }
19.
private void input()
{ int keyState = getKeyStates(); if(keyState== LEFT_PRESSED){ sx = sx - 5; }else if(keyState== RIGHT_PRESSED){ sx = sx + 5; } } private void drawScreen(Graphics g) { int w = getWidth(); int h = getHeight(); hero.setPosition(w/2 + sx, h/2 ); hero2.setPosition(w/2 + 50, h/2 - 20); bg.setPosition(-50, -50); layerManager.setViewWindow(sx,sy,w,h); layerManager.paint(g,0,0); flushGraphics(); } }
20.
ใช้ TiledLayer ทำา
background
21.
TiledLayer
22.
TiledLayer 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}
23.
สร้างแผนที่ (world)
เราสามารถสร้าง world หรือ map โดยใช้ Mappy win32
24.
ผลจาก export text
file ในรูปแบบ array const short mymap_map0[10][10] = { { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, { 2, 4, 4, 4, 4, 4, 4, 4, 4, 2 }, { 2, 4, 4, 3, 5, 5, 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, 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, 4, 5, 4, 4, 2 }, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }};
25.
การใช้งาน TiledLayer TiledLayer background,
land; int[][] map = { { 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, 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 }, { 5, 5, 5, 5, 2, 5, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}} background = new TiledLayer(30, 30, img_bg, img_bg.getWidth()/5,img_bg.getHeight()); background.setCell(i, j, map[i][j]);
26.
TiledLayerMIDlet ให้สร้าง work
space ชือว่า TiledLayerDemo ่ ให้สร้าง J2ME Class ชือว่า ่ TiledLayerMIDlet.java ใช้ TiledLayer ทำา background
27.
TiledLayerMIDlet.java import java.io.IOException; import javax.microedition.lcdui.*; import
javax.microedition.lcdui.game.*; import javax.microedition.midlet.*; public class TiledLayerMIDlet extends MIDlet { CanvasTiledLayer canvas = new CanvasTiledLayer(); public TiledLayerMIDlet() {} protected void destroyApp(boolean arg0) throws MIDletStateChangeException {} protected void pauseApp() {} protected void startApp() throws MIDletStateChangeException { Display display = Display.getDisplay(this); canvas.start(); display.setCurrent(canvas); } }
28.
class CanvasTiledLayer extends
GameCanvas implements Runnable { static int FRONT_DIRECTION = 0; static int LEFT_DIRECTION = 1; static int RIGHT_DIRECTION = 2; static int BACK_DIRECTION = 3; Sprite ship; TiledLayer waster, land; LayerManager layerManager; int w, h; int cx,cy; int currentDirection = FRONT_DIRECTION; boolean running; int[][] sequence = {{0,1,2,3}, //front {4,5,6,7}, //left {8,9,10,11}, //right {12,13,14,15}}; //back
29.
int[][] obstruction_land =
{ { 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 }, { 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, 4, 2, 2, 2, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 4, 5, 2, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 4, 4, 4, 0, 0, 0, 0, 5, 2, 2, 2, 2, 5, 5, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 2, 2, 5, 3, 0, 0, 0, 2 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2 }, { 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 2, 4, 5 }, { 2, 2, 0, 0, 0, 0, 0, 0, 2, 2, 4, 4, 3, 0, 0, 0, 0, 2, 2, 4 }, { 5, 2, 2, 0, 0, 0, 5, 4, 5, 2, 2, 5, 5, 3, 0, 0, 0, 0, 2, 4 }, { 5, 4, 2, 0, 0, 3, 4, 4, 2, 2, 2, 2, 5, 3, 0, 0, 0, 0, 0, 2 }, { 5, 4, 2, 0, 0, 0, 0, 4, 4, 5, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0 }, { 5, 3, 0, 0, 0, 0, 0, 0, 0, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 5, 2, 2, 2, 2, 2, 2, 2, 0, 0 }, { 0, 0, 0, 0, 2, 2, 2, 5, 4, 4, 5, 4, 4, 5, 4, 5, 4, 2, 2, 2 }};
30.
int[][] waster_map =
{ { 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, 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, 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, 1 }};
31.
protected CanvasTiledLayer() { super(true); Image
img_ship = null; Image img_bg = null; w = getWidth(); h = getHeight(); cx = w/2; cy = h/2; try { img_ship = Image.createImage("/res/Ship.png"); img_bg = Image.createImage("/res/bgtiles.png"); } catch (IOException e) {} ship = new Sprite(img_ship,img_ship.getWidth()/4,img_ship.getHeight()/4); land = new TiledLayer(20, 20, img_bg, img_bg.getWidth()/5,img_bg.getHeight()); waster = new TiledLayer(20, 20, img_bg, img_bg.getWidth()/5,img_bg.getHeight()); for (int i = 0; i < 20;i++) { for (int j = 0; j <20;j++) { land.setCell(i, j, obstruction_land[i][j]); }} for (int i = 0; i < 20;i++) { for (int j = 0; j < 20;j++) { waster.setCell(i, j, waster_map[i][j]); }} ship.setFrameSequence(sequence[0]); layerManager = new LayerManager(); layerManager.append(ship); layerManager.append(land); layerManager.append(waster); }
32.
public void run()
{ Graphics g = getGraphics(); int delay = 100; while(running){ getInput(); drawScreen(g); try {Thread.sleep(delay);} catch (InterruptedException e) {} } } void drawScreen(Graphics g) { int w = getWidth(); int h = getHeight(); int xpoint = 120; int ypoint = 70; g.setColor(0); g.fillRect(0, 0, w, h); ship.setPosition((w/2) + xpoint + (cx /2) - 12, (h/2)+ ypoint + (cy/2) - 24 ); layerManager.paint(g, -(xpoint) - (cx/2) , -(ypoint) - (cy/2)); g.setColor(255,255,255); g.drawString(" ["+cx+","+cy+"]", 0, getHeight()- 20, g.TOP | g.LEFT); flushGraphics(); }
33.
void getInput() { int
keyState = getKeyStates(); int cDirection = currentDirection; if(keyState== LEFT_PRESSED){ currentDirection= LEFT_DIRECTION; if(!ship.collidesWith(land, true)){ cx = cx - 10; } else {cx = cx + 20;} ship.nextFrame(); } else if(keyState== RIGHT_PRESSED){ currentDirection= RIGHT_DIRECTION; if(!ship.collidesWith(land, true)){ cx = cx + 10; } else {cx = cx - 20;} ship.nextFrame(); }
34.
else if (keyState==
UP_PRESSED) { currentDirection= BACK_DIRECTION; if(!ship.collidesWith(land, true)){ cy = cy - 10; }else{cy = cy + 20;} ship.nextFrame(); } else if(keyState== DOWN_PRESSED){ currentDirection= FRONT_DIRECTION; if(!ship.collidesWith(land, true)){ cy = cy + 10; }else{cy = cy - 20;} ship.nextFrame(); } if(cDirection != currentDirection){ ship.setFrameSequence(sequence[currentDirection]); } }
35.
public void start()
{ running = true; Thread t = new Thread(this); t.start(); } }
36.
แนะนำาหนังสือ J2ME สร้างเกมและโปรแกรมด้วย
J2ME คุณก็ทำาได้ เขียนเกมและโปรแกรมแบบมือถือ J2ME + CD J2ME ค่่มือสำาหรับเร่มต้น พัฒนาจาวาบนมือ ิ เก่ง J2ME ให้ครบส่ตร + CD เขียนโปรแกรมบนโทรศัพท์เคล่ ือนท่ด้วย J2ME ี
Download now