SlideShare une entreprise Scribd logo
1  sur  41
Télécharger pour lire hors ligne
Crea%ng	
  Games	
  for	
  	
  
Asha	
  -­‐	
  Pla3orm	
  
Jussi	
  Pohjolainen	
  	
  
TAMK	
  University	
  of	
  Applied	
  Sciences	
  
GRAPHICS	
  IN	
  MIDP	
  
Class	
  Hierarchy	
  
javax.microedi9on.lcdui	
  

javax.microedi9on.lcdui.game	
  

Displayable	
  

Canvas	
  

Screen	
  

Alert	
  

List	
  

Form	
  

TextBox	
  

GameCanvas	
  
Using	
  Graphics	
  
•  Class:	
  javax.microedition.lcdui.Canvas
•  Create	
  a	
  subclass:	
  
–  class MyCanvas extends Canvas

•  Canvas-­‐class	
  has	
  only	
  one	
  abstract	
  method:	
  
–  paint(graphics g)

•  It	
  is	
  possible	
  to	
  override	
  methods	
  that	
  deal	
  
with	
  events	
  
Simple	
  Example	
  
class MyCanvas extends Canvas {
public void paint(Graphics g){
// draw
}
}
class MyMidlet extends MIDlet{
public void startApp(){
MyCanvas mycanvas = new MyCanvas();
Display.getDisplay(this).setCurrent(mycanvas);
}
}
Repain%ng	
  
•  You	
  never	
  call	
  the	
  paint()	
  method.	
  
•  Instead	
  of	
  you	
  use	
  repaint():
–  By	
  using	
  this	
  method,	
  you	
  ask	
  the	
  framework	
  to	
  
repaint	
  the	
  canvas	
  
–  Framework	
  decides	
  when	
  is	
  the	
  best	
  %me	
  to	
  repaint	
  
the	
  canvas	
  

•  There	
  is	
  a	
  also:	
  
–  repaint(int x, int y, int width, int
height)
Coordinates	
  
•  Upper-­‐LeS	
  (0,0)	
  
•  Translate	
  the	
  origon	
  
–  translate()	
  –	
  metodi	
  

•  Origo's	
  posi%on:	
  
–  getTranslateX()
–  getTranslateY()

x"

y"
Graphics-­‐classes	
  drawing	
  methods	
  
•  See	
  the	
  API!	
  
–  drawLine(..)
–  drawRect(...)
–  drawRoundRect(...)
–  drawArc(...)
–  fillTriangle(...)
–  fillRect(...)
–  fillRoundRect(...)
–  fillArc(...)
Key	
  Handling	
  
class MyCanvas extends Canvas{
public void paint(Graphics g) { }
protected void keyReleased(int keyCode) { }
protected void keyRepeated(int keyCode) { }
protected void keyPressed(int keyCode) { }

}
class MyMidlet extends MIDlet{
public void startApp(){
MyCanvas mycanvas = new MyCanvas();
Display.getDisplay(this).setCurrent(mycanvas);
}
}
GAME	
  API	
  
Game	
  API	
  
•  It	
  is	
  easy	
  to	
  handle	
  anima%on	
  and	
  graphics	
  
with	
  the	
  Game	
  API	
  
•  All	
  the	
  classes	
  can	
  be	
  found	
  from	
  
javax.microedition.lcdui.game.*;
GameCanvas	
  
•  Using	
  the	
  tradi%onal	
  Canvas-­‐class	
  
–  You	
  inherit	
  the	
  Canvas	
  and	
  override	
  paint-­‐method.	
  
–  repaint()
–  Event	
  handling	
  is	
  done	
  by	
  using	
  methods	
  like	
  keypressed,	
  
keyreleased	
  

•  Using	
  the	
  GameCanvas-­‐class	
  
–  You	
  inherit	
  the	
  GameCanvas-­‐class	
  
–  There	
  is	
  no	
  need	
  for	
  paint-­‐method,	
  you	
  can	
  draw	
  anywhere!	
  	
  
–  flushGraphics()
–  Two	
  ways	
  of	
  doing	
  event	
  handling!
Example	
  of	
  GameCanvas	
  Usage	
  
class MyCanvas extends GameCanvas {
public void anymethod(){
Graphics g = getGraphics();
// some drawing
flushGraphics()
}
}
Handling	
  Events	
  
•  Constructor	
  of	
  GameCanvas	
  
–  protected GameCanvas(boolean
suppressKeyEvents)

•  You	
  have	
  to	
  call	
  this	
  constructor	
  in	
  you	
  own	
  
GameCanvas	
  class..	
  
–  =>	
  You	
  have	
  to	
  give	
  a	
  boolean	
  value..	
  
–  true:	
  use	
  only	
  GameCanvases	
  own	
  event	
  handling	
  
–  false:	
  in	
  addi4on	
  to	
  GameCanvases	
  own	
  event	
  handling	
  
use	
  Canvases	
  event	
  handling	
  
GameCanvas	
  Usage	
  
class MyCanvas extends GameCanvas {
public MyCanvas() {
// Let's use Game Canvas event handling!
super(true);
}
public void anymethod() {
// drawing..
}
}
Event	
  Handling	
  
•  You	
  can	
  ask	
  which	
  bu]on	
  is	
  pressed	
  
(GameCanvas	
  Event	
  Handling)	
  
–  public int getKeyStates()

•  Bit-­‐finals	
  of	
  GameCanvas	
  
–  UP_PRESSED, DOWN_PRESSED, LEFT_PRESSED,
RIGHT_PRESSED, FIRE_PRESSED,
GAME_A_PRESSED, GAME_B_PRESSED,
GAME_C_PRESSED, GAME_D_PRESSED
GameCanvas	
  Example	
  3	
  
class MyCanvas extends GameCanvas implements Runnable {
public MyCanvas() {
super(true);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
while(true) {
int ks = getKeyStates();
if ((ks & UP_PRESSED) != 0)
moveUp();
else if((ks & DOWN_PRESSED) != 0)
moveDown();
// Drawing...
}
}
}
Touch	
  
class MyCanvas extends GameCanvas implements Runnable {
public MyCanvas() {
super(true);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
while(true) {
doSomething();
}
}
public void pointerPressed(int x, int y) { .. }
public void pointerReleased(int x, int y) { .. }
public void pointerDragged(int x, int y) { .. }
}
GameLoop	
  and	
  FPS	
  
class MyCanvas extends GameCanvas implements Runnable {
public MyCanvas() {
super(true);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
// THIS WILL LOOP AS FAST AS IT CAN!
while(true) {
doSomething();
}
}
}
GameLoop	
  and	
  FPS	
  
class MyCanvas extends GameCanvas implements Runnable {
private int fps = 1;
public MyCanvas() {
super(true);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
// Now iteration is 1 frames per sec
while(true) {
doSomething();
try {
Thread.sleep(1000 / fps);
} catch (InterruptedException e) { .. }
}
}
}
GameLoop	
  and	
  FPS	
  
class MyCanvas extends GameCanvas implements Runnable {
private int fps = 30;
public MyCanvas() {
super(true);
Thread thread = new Thread(this);
thread.start();
}
public void run() {

// Problem, what if the mobile phone can keep up with the pace?
while(true) {

// This takes second…
doSomethingHeavyAndMagical3DStuff();
try {
Thread.sleep(1000 / fps); // and after the second, we will wait more!
} catch (InterruptedException e) { .. }
}
}
}
class MyCanvas extends GameCanvas implements Runnable {
private int fps = 30;
public
long
long
long
long

void run() {
time = 0;
elapsedTime = 0;
interval = 0;
sleepTime = 0;

while(true) {
// Get current time
time = System.currentTimeMillis();
doSomethingHeavyAndMagical3DStuff();

// Elapsed time
elapsedTime = System.currentTimeMillis() - time;
// Do we need to sleep?
sleepTime = 0;
// If elapsed time was 100 millisecs, then
//
1000 / 30 - 100 = -66,66. Sleep is not needed (= 0)!
// If elapsed time was 1 millisecs, then sleepTime:
//
1000 / 30 - 1 = 32,333.
// If elapsed time was 20 millisecs, then sleepTime:
//
1000 / 30 - 20 = 13,333..
interval = (int) ((1000.0 / fps) - elapsedTime);
if(interval > 0) {
sleepTime = interval;
}
try {

}

}

}

Thread.sleep(sleepTime);
} catch (InterruptedException e) { }
Layers	
  
•  You	
  can	
  use	
  layers	
  with	
  the	
  Game	
  canvas.	
  
•  For	
  example:	
  
–  background-­‐layer	
  (bo]om)	
  
–  car-­‐layer	
  (front	
  of	
  the	
  background)	
  
•  javax.microedition.lcdui.game.Layer
Layer-­‐class	
  
•  Layer	
  class	
  is	
  abstract	
  and	
  it	
  has	
  two	
  concrete	
  
subclasses:	
  1)	
  TiledLayer,	
  2)	
  Sprite
•  Layer's	
  methods	
  
–  int getX()
–  int getY()
–  int getWidth()
–  int getHeight()
–  void setPosition(..)
–  move(..)
Class	
  Diagram	
  
{abstract}	
  Layer	
  
int	
  getX()	
  
int	
  getY()	
  
int	
  getWidth()	
  
int	
  getHeight()	
  
void	
  setPosi%on(..)	
  
move(..)	
  
Sprite	
  

TiledLayer	
  
Mastering	
  the	
  layers	
  
•  Every	
  layer	
  (Sprite	
  or	
  TiledLayer)	
  is	
  put	
  into	
  a	
  
LayerManager.	
  The	
  LayerManager is	
  
eventually	
  drawn	
  to	
  the	
  screen.	
  
•  LayerManager's	
  methods	
  
–  append(Layer l)
–  insert(Layer l, int i)
–  Layer getLayer(int i)
–  paint(..)
Class	
  Diagram	
  
LayerManager	
  

{abstract}	
  Layer	
  

append(Layer	
  l)	
  
insert(Layer	
  l,	
  int	
  i)	
  
Layer	
  getLayer(int	
  i)	
  
paint(..)	
  

int	
  getX()	
  
*	
   int	
  getY()	
  
int	
  getWidth()	
  
int	
  getHeight()	
  
void	
  setPosi%on(..)	
  
move(..)	
  
Sprite	
  

TiledLayer	
  
LayerManager:	
  setViewWindow!
•  public void setViewWindow(int x, int y, int width,
int height)
•  What	
  part	
  of	
  a	
  big	
  picture	
  is	
  shown	
  on	
  the	
  screen:	
  
Sprite	
  -­‐	
  class	
  
•  Sprite	
  classes	
  constructors:	
  
–  public Sprite(Image i)
–  public Sprite(Image i, int framewidth,
int frameheight)
Example	
  of	
  Using	
  Sprite	
  and	
  
LayoutManager	
  
LayerManager l = new LayerManager();
Sprite s = new Sprite(myimage);
s.setPosition(50,50);
l.append(s);
Graphics g = getGraphics();
l.paint(g,0,0);
flushGraphics();
Sprite	
  anima%on	
  
•  Make	
  one	
  image-­‐file,	
  which	
  contains	
  all	
  the	
  
frames	
  
•  In	
  the	
  Sprite's	
  constructor	
  you	
  define	
  one	
  
frame's	
  height	
  and	
  width	
  
•  ASer	
  that	
  you	
  can	
  use	
  Sprite's	
  nextFrame()	
  
method	
  
Example	
  

Sprite x = new Sprite(image, 540/18, 30);
layermanager.append(x);
.
.
x.nextFrame();
With	
  Threads..	
  
public void run() {!
while(true){!
int ks = getKeyStates();!
if((ks & RIGHT_PRESSED) != 0){!
mysprite.move(3,0);!
mysprite.nextFrame();!
}!
}!
}!
Influencing	
  frames	
  
•  Changing	
  sequence	
  
–  int sequence [] = {0, 15, 17};
–  mysprite.setFrameSequence(sequence)

•  Jumping	
  to	
  another	
  frame	
  
–  mysprite.setFrame(10);
Transforma%on	
  
•  It	
  is	
  possible	
  to	
  transform	
  the	
  sprite	
  
–  public void setTransform(int transform)

•  Finals	
  
–  TRANS_NONE
–  TRANS_ROT90
–  TRANS_MIRROR
–  ..	
  see	
  the	
  api	
  

•  In	
  prac%ce	
  
–  mysprite.setTransform(Sprite.TRANS_MIRROR)
Reference	
  Pixel	
  

Reference pixel"
Reference	
  Pixel	
  
mysprite.defineReferencePixel(15,15);

Reference pixel"
Collisions	
  
•  Sprite	
  
–  collidesWith(Sprite s, boolean pixelLevel)
–  collidesWith(TiledLayer s, boolean pixelLevel)
–  collidesWith(Image s, int x, int y, boolean pixelLevel)

•  PixelLevel	
  
–  The	
  rect	
  of	
  the	
  sprite	
  or	
  the	
  "real	
  pixels"	
  
Example	
  
Sprite x = new Sprite(...);
Sprite y = new Sprite(...);
if(x.collidesWith(y, true)){
// CRASH!

}
TiledLayer	
  
•  A	
  TiledLayer	
  is	
  a	
  visual	
  element	
  composed	
  of	
  a	
  
grid	
  of	
  cells	
  that	
  can	
  be	
  filled	
  with	
  a	
  set	
  of	
  %le	
  
images.	
  
•  Rows	
  and	
  Columns	
  
–  TiledLayer(int columns, int rows, Image
i, int tileWidth, int tileHeight);
Example	
  
TiledLayer a = new TiledLayer(4,2, picture, 32, 32);
a.setCell(0,1,1); a.setCell(1,1,1), a.setCell(2,1,1);
a.setCell(3,1,1);
a.setCell(1,0,2);
a.setCell(2,0,3);

1"

2"

3"

0" 1" 2" 3"
0"
1"

Contenu connexe

Tendances

14multithreaded Graphics
14multithreaded Graphics14multithreaded Graphics
14multithreaded GraphicsAdil Jafri
 
Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲吳錫修 (ShyiShiou Wu)
 
Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019Unity Technologies
 
The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184Mahmoud Samir Fayed
 
The Enchant.js Animation Engine in 5 Minutes
The Enchant.js Animation Engine in 5 MinutesThe Enchant.js Animation Engine in 5 Minutes
The Enchant.js Animation Engine in 5 MinutesBrandon McInnis
 
Breathing the life into the canvas
Breathing the life into the canvasBreathing the life into the canvas
Breathing the life into the canvasTomislav Homan
 
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.3 book - Part 38 of 88The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.3 book - Part 38 of 88Mahmoud Samir Fayed
 
Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲吳錫修 (ShyiShiou Wu)
 
Chapter ii(coding)
Chapter ii(coding)Chapter ii(coding)
Chapter ii(coding)Chhom Karath
 
Getting Started with 3D Game Development on Nokia Series 40 Asha Phones
Getting Started with 3D Game Development on Nokia Series 40 Asha PhonesGetting Started with 3D Game Development on Nokia Series 40 Asha Phones
Getting Started with 3D Game Development on Nokia Series 40 Asha PhonesMicrosoft Mobile Developer
 
[C++ GUI Programming with Qt4] chap8
[C++ GUI Programming with Qt4] chap8[C++ GUI Programming with Qt4] chap8
[C++ GUI Programming with Qt4] chap8Picker Weng
 

Tendances (19)

14multithreaded Graphics
14multithreaded Graphics14multithreaded Graphics
14multithreaded Graphics
 
Real life XNA
Real life XNAReal life XNA
Real life XNA
 
libGDX: Scene2D
libGDX: Scene2DlibGDX: Scene2D
libGDX: Scene2D
 
Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲Unity遊戲程式設計(15) 實作Space shooter遊戲
Unity遊戲程式設計(15) 實作Space shooter遊戲
 
Developing games for Series 40 full-touch UI
Developing games for Series 40 full-touch UIDeveloping games for Series 40 full-touch UI
Developing games for Series 40 full-touch UI
 
Unity programming 1
Unity programming 1Unity programming 1
Unity programming 1
 
Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019
 
The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184The Ring programming language version 1.5.3 book - Part 48 of 184
The Ring programming language version 1.5.3 book - Part 48 of 184
 
Python book
Python bookPython book
Python book
 
The Enchant.js Animation Engine in 5 Minutes
The Enchant.js Animation Engine in 5 MinutesThe Enchant.js Animation Engine in 5 Minutes
The Enchant.js Animation Engine in 5 Minutes
 
Breathing the life into the canvas
Breathing the life into the canvasBreathing the life into the canvas
Breathing the life into the canvas
 
The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84The Ring programming language version 1.2 book - Part 36 of 84
The Ring programming language version 1.2 book - Part 36 of 84
 
The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.3 book - Part 38 of 88The Ring programming language version 1.3 book - Part 38 of 88
The Ring programming language version 1.3 book - Part 38 of 88
 
Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲Unity遊戲程式設計 - 2D Platformer遊戲
Unity遊戲程式設計 - 2D Platformer遊戲
 
Chapter ii(coding)
Chapter ii(coding)Chapter ii(coding)
Chapter ii(coding)
 
Getting Started with 3D Game Development on Nokia Series 40 Asha Phones
Getting Started with 3D Game Development on Nokia Series 40 Asha PhonesGetting Started with 3D Game Development on Nokia Series 40 Asha Phones
Getting Started with 3D Game Development on Nokia Series 40 Asha Phones
 
7java Events
7java Events7java Events
7java Events
 
[C++ GUI Programming with Qt4] chap8
[C++ GUI Programming with Qt4] chap8[C++ GUI Programming with Qt4] chap8
[C++ GUI Programming with Qt4] chap8
 
OpenGL L05-Texturing
OpenGL L05-TexturingOpenGL L05-Texturing
OpenGL L05-Texturing
 

En vedette

ADT02 - Java 8 Lambdas and the Streaming API
ADT02 - Java 8 Lambdas and the Streaming APIADT02 - Java 8 Lambdas and the Streaming API
ADT02 - Java 8 Lambdas and the Streaming APIMichael Remijan
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesJussi Pohjolainen
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame AnimationJussi Pohjolainen
 
iOS Selectors Blocks and Delegation
iOS Selectors Blocks and DelegationiOS Selectors Blocks and Delegation
iOS Selectors Blocks and DelegationJussi Pohjolainen
 

En vedette (7)

ADT02 - Java 8 Lambdas and the Streaming API
ADT02 - Java 8 Lambdas and the Streaming APIADT02 - Java 8 Lambdas and the Streaming API
ADT02 - Java 8 Lambdas and the Streaming API
 
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and GesturesCreating Asha Games: Game Pausing, Orientation, Sensors and Gestures
Creating Asha Games: Game Pausing, Orientation, Sensors and Gestures
 
Android Threading
Android ThreadingAndroid Threading
Android Threading
 
Intro to Asha UI
Intro to Asha UIIntro to Asha UI
Intro to Asha UI
 
libGDX: Simple Frame Animation
libGDX: Simple Frame AnimationlibGDX: Simple Frame Animation
libGDX: Simple Frame Animation
 
iOS Selectors Blocks and Delegation
iOS Selectors Blocks and DelegationiOS Selectors Blocks and Delegation
iOS Selectors Blocks and Delegation
 
Compiling Qt Apps
Compiling Qt AppsCompiling Qt Apps
Compiling Qt Apps
 

Similaire à Creating Games for Asha - platform

Graphics, Threads and HTTPConnections in MIDLets
Graphics, Threads and HTTPConnections in MIDLetsGraphics, Threads and HTTPConnections in MIDLets
Graphics, Threads and HTTPConnections in MIDLetsJussi 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.pdfI wanted to change the cloudsrectangles into an actuall image it do.pdf
I wanted to change the cloudsrectangles into an actuall image it do.pdffeelinggifts
 
Game Development for Nokia Asha Devices with Java ME #2
Game Development for Nokia Asha Devices with Java ME #2Game Development for Nokia Asha Devices with Java ME #2
Game Development for Nokia Asha Devices with Java ME #2Marlon Luz
 
Java ME - 03 - Low Level Graphics E
Java ME - 03 - Low Level Graphics EJava ME - 03 - Low Level Graphics E
Java ME - 03 - Low Level Graphics EAndreas Jakl
 
Mapping the world with Twitter
Mapping the world with TwitterMapping the world with Twitter
Mapping the world with Twittercarlo zapponi
 
Google I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics PerformanceGoogle I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics PerformanceDouO
 
Google I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics PerformanceGoogle I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics PerformanceDouO
 
Stop running from animations droidcon London
Stop running from animations droidcon LondonStop running from animations droidcon London
Stop running from animations droidcon Londonmaric_iv
 
Advanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics APIAdvanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics APITomi Aarnio
 
Criando meu 1º game com android
Criando meu 1º game com androidCriando meu 1º game com android
Criando meu 1º game com androidDaniel Baccin
 
HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?Ankara JUG
 
Java ME - 05 - Game API
Java ME - 05 - Game APIJava ME - 05 - Game API
Java ME - 05 - Game APIAndreas Jakl
 
Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Korhan Bircan
 
Google Fit, Android Wear & Xamarin
Google Fit, Android Wear & XamarinGoogle Fit, Android Wear & Xamarin
Google Fit, Android Wear & XamarinPeter Friese
 
Task Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfTask Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfcronkwurphyb44502
 

Similaire à Creating Games for Asha - platform (20)

MIDP: Game API
MIDP: Game APIMIDP: Game API
MIDP: Game API
 
Scmad Chapter07
Scmad Chapter07Scmad Chapter07
Scmad Chapter07
 
Graphics, Threads and HTTPConnections in MIDLets
Graphics, Threads and HTTPConnections in MIDLetsGraphics, Threads and HTTPConnections in MIDLets
Graphics, Threads and HTTPConnections in MIDLets
 
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.pdfI 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
 
Game Development for Nokia Asha Devices with Java ME #2
Game Development for Nokia Asha Devices with Java ME #2Game Development for Nokia Asha Devices with Java ME #2
Game Development for Nokia Asha Devices with Java ME #2
 
Java ME - 03 - Low Level Graphics E
Java ME - 03 - Low Level Graphics EJava ME - 03 - Low Level Graphics E
Java ME - 03 - Low Level Graphics E
 
Mapping the world with Twitter
Mapping the world with TwitterMapping the world with Twitter
Mapping the world with Twitter
 
Google I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics PerformanceGoogle I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics Performance
 
Google I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics PerformanceGoogle I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics Performance
 
Stop running from animations droidcon London
Stop running from animations droidcon LondonStop running from animations droidcon London
Stop running from animations droidcon London
 
Advanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics APIAdvanced Game Development with the Mobile 3D Graphics API
Advanced Game Development with the Mobile 3D Graphics API
 
Criando meu 1º game com android
Criando meu 1º game com androidCriando meu 1º game com android
Criando meu 1º game com android
 
HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?HTML5 - Daha Flash bir web?
HTML5 - Daha Flash bir web?
 
Java ME - 05 - Game API
Java ME - 05 - Game APIJava ME - 05 - Game API
Java ME - 05 - Game API
 
Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)Useful Tools for Making Video Games - XNA (2008)
Useful Tools for Making Video Games - XNA (2008)
 
Peint talk
Peint talkPeint talk
Peint talk
 
A More Flash Like Web?
A More Flash Like Web?A More Flash Like Web?
A More Flash Like Web?
 
Google Fit, Android Wear & Xamarin
Google Fit, Android Wear & XamarinGoogle Fit, Android Wear & Xamarin
Google Fit, Android Wear & Xamarin
 
Task Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdfTask Write a Java program to implement a simple graphics editor tha.pdf
Task Write a Java program to implement a simple graphics editor tha.pdf
 
Scmad Chapter06
Scmad Chapter06Scmad Chapter06
Scmad Chapter06
 

Plus de Jussi Pohjolainen

Plus de Jussi Pohjolainen (19)

Moved to Speakerdeck
Moved to SpeakerdeckMoved to Speakerdeck
Moved to Speakerdeck
 
Java Web Services
Java Web ServicesJava Web Services
Java Web Services
 
libGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and PreferenceslibGDX: Screens, Fonts and Preferences
libGDX: Screens, Fonts and Preferences
 
Intro to Building Android Games using libGDX
Intro to Building Android Games using libGDXIntro to Building Android Games using libGDX
Intro to Building Android Games using libGDX
 
Advanced JavaScript Development
Advanced JavaScript DevelopmentAdvanced JavaScript Development
Advanced JavaScript Development
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
Introduction to AngularJS
Introduction to AngularJSIntroduction to AngularJS
Introduction to AngularJS
 
Building Android games using LibGDX
Building Android games using LibGDXBuilding Android games using LibGDX
Building Android games using LibGDX
 
Intro to Java ME and Asha Platform
Intro to Java ME and Asha PlatformIntro to Java ME and Asha Platform
Intro to Java ME and Asha Platform
 
Intro to PhoneGap
Intro to PhoneGapIntro to PhoneGap
Intro to PhoneGap
 
Quick Intro to JQuery and JQuery Mobile
Quick Intro to JQuery and JQuery MobileQuick Intro to JQuery and JQuery Mobile
Quick Intro to JQuery and JQuery Mobile
 
JavaScript Inheritance
JavaScript InheritanceJavaScript Inheritance
JavaScript Inheritance
 
JS OO and Closures
JS OO and ClosuresJS OO and Closures
JS OO and Closures
 
Short intro to ECMAScript
Short intro to ECMAScriptShort intro to ECMAScript
Short intro to ECMAScript
 
XAMPP
XAMPPXAMPP
XAMPP
 
Building Web Services
Building Web ServicesBuilding Web Services
Building Web Services
 
CSS
CSSCSS
CSS
 
Extensible Stylesheet Language
Extensible Stylesheet LanguageExtensible Stylesheet Language
Extensible Stylesheet Language
 
About Http Connection
About Http ConnectionAbout Http Connection
About Http Connection
 

Dernier

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGSujit Pal
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Dernier (20)

The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Google AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAGGoogle AI Hackathon: LLM based Evaluator for RAG
Google AI Hackathon: LLM based Evaluator for RAG
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Creating Games for Asha - platform

  • 1. Crea%ng  Games  for     Asha  -­‐  Pla3orm   Jussi  Pohjolainen     TAMK  University  of  Applied  Sciences  
  • 3. Class  Hierarchy   javax.microedi9on.lcdui   javax.microedi9on.lcdui.game   Displayable   Canvas   Screen   Alert   List   Form   TextBox   GameCanvas  
  • 4. Using  Graphics   •  Class:  javax.microedition.lcdui.Canvas •  Create  a  subclass:   –  class MyCanvas extends Canvas •  Canvas-­‐class  has  only  one  abstract  method:   –  paint(graphics g) •  It  is  possible  to  override  methods  that  deal   with  events  
  • 5. Simple  Example   class MyCanvas extends Canvas { public void paint(Graphics g){ // draw } } class MyMidlet extends MIDlet{ public void startApp(){ MyCanvas mycanvas = new MyCanvas(); Display.getDisplay(this).setCurrent(mycanvas); } }
  • 6. Repain%ng   •  You  never  call  the  paint()  method.   •  Instead  of  you  use  repaint(): –  By  using  this  method,  you  ask  the  framework  to   repaint  the  canvas   –  Framework  decides  when  is  the  best  %me  to  repaint   the  canvas   •  There  is  a  also:   –  repaint(int x, int y, int width, int height)
  • 7. Coordinates   •  Upper-­‐LeS  (0,0)   •  Translate  the  origon   –  translate()  –  metodi   •  Origo's  posi%on:   –  getTranslateX() –  getTranslateY() x" y"
  • 8. Graphics-­‐classes  drawing  methods   •  See  the  API!   –  drawLine(..) –  drawRect(...) –  drawRoundRect(...) –  drawArc(...) –  fillTriangle(...) –  fillRect(...) –  fillRoundRect(...) –  fillArc(...)
  • 9. Key  Handling   class MyCanvas extends Canvas{ public void paint(Graphics g) { } protected void keyReleased(int keyCode) { } protected void keyRepeated(int keyCode) { } protected void keyPressed(int keyCode) { } } class MyMidlet extends MIDlet{ public void startApp(){ MyCanvas mycanvas = new MyCanvas(); Display.getDisplay(this).setCurrent(mycanvas); } }
  • 11. Game  API   •  It  is  easy  to  handle  anima%on  and  graphics   with  the  Game  API   •  All  the  classes  can  be  found  from   javax.microedition.lcdui.game.*;
  • 12. GameCanvas   •  Using  the  tradi%onal  Canvas-­‐class   –  You  inherit  the  Canvas  and  override  paint-­‐method.   –  repaint() –  Event  handling  is  done  by  using  methods  like  keypressed,   keyreleased   •  Using  the  GameCanvas-­‐class   –  You  inherit  the  GameCanvas-­‐class   –  There  is  no  need  for  paint-­‐method,  you  can  draw  anywhere!     –  flushGraphics() –  Two  ways  of  doing  event  handling!
  • 13. Example  of  GameCanvas  Usage   class MyCanvas extends GameCanvas { public void anymethod(){ Graphics g = getGraphics(); // some drawing flushGraphics() } }
  • 14. Handling  Events   •  Constructor  of  GameCanvas   –  protected GameCanvas(boolean suppressKeyEvents) •  You  have  to  call  this  constructor  in  you  own   GameCanvas  class..   –  =>  You  have  to  give  a  boolean  value..   –  true:  use  only  GameCanvases  own  event  handling   –  false:  in  addi4on  to  GameCanvases  own  event  handling   use  Canvases  event  handling  
  • 15. GameCanvas  Usage   class MyCanvas extends GameCanvas { public MyCanvas() { // Let's use Game Canvas event handling! super(true); } public void anymethod() { // drawing.. } }
  • 16. Event  Handling   •  You  can  ask  which  bu]on  is  pressed   (GameCanvas  Event  Handling)   –  public int getKeyStates() •  Bit-­‐finals  of  GameCanvas   –  UP_PRESSED, DOWN_PRESSED, LEFT_PRESSED, RIGHT_PRESSED, FIRE_PRESSED, GAME_A_PRESSED, GAME_B_PRESSED, GAME_C_PRESSED, GAME_D_PRESSED
  • 17. GameCanvas  Example  3   class MyCanvas extends GameCanvas implements Runnable { public MyCanvas() { super(true); Thread thread = new Thread(this); thread.start(); } public void run() { while(true) { int ks = getKeyStates(); if ((ks & UP_PRESSED) != 0) moveUp(); else if((ks & DOWN_PRESSED) != 0) moveDown(); // Drawing... } } }
  • 18. Touch   class MyCanvas extends GameCanvas implements Runnable { public MyCanvas() { super(true); Thread thread = new Thread(this); thread.start(); } public void run() { while(true) { doSomething(); } } public void pointerPressed(int x, int y) { .. } public void pointerReleased(int x, int y) { .. } public void pointerDragged(int x, int y) { .. } }
  • 19. GameLoop  and  FPS   class MyCanvas extends GameCanvas implements Runnable { public MyCanvas() { super(true); Thread thread = new Thread(this); thread.start(); } public void run() { // THIS WILL LOOP AS FAST AS IT CAN! while(true) { doSomething(); } } }
  • 20. GameLoop  and  FPS   class MyCanvas extends GameCanvas implements Runnable { private int fps = 1; public MyCanvas() { super(true); Thread thread = new Thread(this); thread.start(); } public void run() { // Now iteration is 1 frames per sec while(true) { doSomething(); try { Thread.sleep(1000 / fps); } catch (InterruptedException e) { .. } } } }
  • 21. GameLoop  and  FPS   class MyCanvas extends GameCanvas implements Runnable { private int fps = 30; public MyCanvas() { super(true); Thread thread = new Thread(this); thread.start(); } public void run() { // Problem, what if the mobile phone can keep up with the pace? while(true) { // This takes second… doSomethingHeavyAndMagical3DStuff(); try { Thread.sleep(1000 / fps); // and after the second, we will wait more! } catch (InterruptedException e) { .. } } } }
  • 22. class MyCanvas extends GameCanvas implements Runnable { private int fps = 30; public long long long long void run() { time = 0; elapsedTime = 0; interval = 0; sleepTime = 0; while(true) { // Get current time time = System.currentTimeMillis(); doSomethingHeavyAndMagical3DStuff(); // Elapsed time elapsedTime = System.currentTimeMillis() - time; // Do we need to sleep? sleepTime = 0; // If elapsed time was 100 millisecs, then // 1000 / 30 - 100 = -66,66. Sleep is not needed (= 0)! // If elapsed time was 1 millisecs, then sleepTime: // 1000 / 30 - 1 = 32,333. // If elapsed time was 20 millisecs, then sleepTime: // 1000 / 30 - 20 = 13,333.. interval = (int) ((1000.0 / fps) - elapsedTime); if(interval > 0) { sleepTime = interval; } try { } } } Thread.sleep(sleepTime); } catch (InterruptedException e) { }
  • 23. Layers   •  You  can  use  layers  with  the  Game  canvas.   •  For  example:   –  background-­‐layer  (bo]om)   –  car-­‐layer  (front  of  the  background)   •  javax.microedition.lcdui.game.Layer
  • 24. Layer-­‐class   •  Layer  class  is  abstract  and  it  has  two  concrete   subclasses:  1)  TiledLayer,  2)  Sprite •  Layer's  methods   –  int getX() –  int getY() –  int getWidth() –  int getHeight() –  void setPosition(..) –  move(..)
  • 25. Class  Diagram   {abstract}  Layer   int  getX()   int  getY()   int  getWidth()   int  getHeight()   void  setPosi%on(..)   move(..)   Sprite   TiledLayer  
  • 26. Mastering  the  layers   •  Every  layer  (Sprite  or  TiledLayer)  is  put  into  a   LayerManager.  The  LayerManager is   eventually  drawn  to  the  screen.   •  LayerManager's  methods   –  append(Layer l) –  insert(Layer l, int i) –  Layer getLayer(int i) –  paint(..)
  • 27. Class  Diagram   LayerManager   {abstract}  Layer   append(Layer  l)   insert(Layer  l,  int  i)   Layer  getLayer(int  i)   paint(..)   int  getX()   *   int  getY()   int  getWidth()   int  getHeight()   void  setPosi%on(..)   move(..)   Sprite   TiledLayer  
  • 28. LayerManager:  setViewWindow! •  public void setViewWindow(int x, int y, int width, int height) •  What  part  of  a  big  picture  is  shown  on  the  screen:  
  • 29. Sprite  -­‐  class   •  Sprite  classes  constructors:   –  public Sprite(Image i) –  public Sprite(Image i, int framewidth, int frameheight)
  • 30. Example  of  Using  Sprite  and   LayoutManager   LayerManager l = new LayerManager(); Sprite s = new Sprite(myimage); s.setPosition(50,50); l.append(s); Graphics g = getGraphics(); l.paint(g,0,0); flushGraphics();
  • 31. Sprite  anima%on   •  Make  one  image-­‐file,  which  contains  all  the   frames   •  In  the  Sprite's  constructor  you  define  one   frame's  height  and  width   •  ASer  that  you  can  use  Sprite's  nextFrame()   method  
  • 32. Example   Sprite x = new Sprite(image, 540/18, 30); layermanager.append(x); . . x.nextFrame();
  • 33. With  Threads..   public void run() {! while(true){! int ks = getKeyStates();! if((ks & RIGHT_PRESSED) != 0){! mysprite.move(3,0);! mysprite.nextFrame();! }! }! }!
  • 34. Influencing  frames   •  Changing  sequence   –  int sequence [] = {0, 15, 17}; –  mysprite.setFrameSequence(sequence) •  Jumping  to  another  frame   –  mysprite.setFrame(10);
  • 35. Transforma%on   •  It  is  possible  to  transform  the  sprite   –  public void setTransform(int transform) •  Finals   –  TRANS_NONE –  TRANS_ROT90 –  TRANS_MIRROR –  ..  see  the  api   •  In  prac%ce   –  mysprite.setTransform(Sprite.TRANS_MIRROR)
  • 38. Collisions   •  Sprite   –  collidesWith(Sprite s, boolean pixelLevel) –  collidesWith(TiledLayer s, boolean pixelLevel) –  collidesWith(Image s, int x, int y, boolean pixelLevel) •  PixelLevel   –  The  rect  of  the  sprite  or  the  "real  pixels"  
  • 39. Example   Sprite x = new Sprite(...); Sprite y = new Sprite(...); if(x.collidesWith(y, true)){ // CRASH! }
  • 40. TiledLayer   •  A  TiledLayer  is  a  visual  element  composed  of  a   grid  of  cells  that  can  be  filled  with  a  set  of  %le   images.   •  Rows  and  Columns   –  TiledLayer(int columns, int rows, Image i, int tileWidth, int tileHeight);
  • 41. Example   TiledLayer a = new TiledLayer(4,2, picture, 32, 32); a.setCell(0,1,1); a.setCell(1,1,1), a.setCell(2,1,1); a.setCell(3,1,1); a.setCell(1,0,2); a.setCell(2,0,3); 1" 2" 3" 0" 1" 2" 3" 0" 1"