game development for nokia asha devices with java me #2
DESCRIPTION
This presentation is the second one of the Game Development for Nokia Asha Devices with Java ME trainingTRANSCRIPT
![Page 1: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/1.jpg)
Game Development for Nokia Asha Devices with Java ME
Marlon Luz - INdTNokia Developer Evangelist
@marlonluz
![Page 2: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/2.jpg)
Low-Level UI API• GUI to have control over each pixel in the screen
• Portable among MIDP devices when in the same screen resolution
Displayable
Screen Canvas
TextBoxAlert Form List GameCanvas
![Page 3: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/3.jpg)
Canvas• Canvas is a subclass of Displayable class
• Have to extend the Canvas class•public class Tela extends Canvas {•}
• Overwrite the paint() method• protected void paint(Graphics g) {• // do some painting!• }
![Page 4: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/4.jpg)
Drawing on the screen• import javax.microedition.lcdui.*;
• class MyCanvas extends Canvas {• public MyCanvas() {• super();• super.setFullScreenMode(true);• }
• public void paint(Graphics g) {• g.setColor(255, 255, 0);• g.fillRect(0, 0, getWidth(),• getHeight());• g.setColor(0, 0, 0);• g.drawString("Hello There", • getWidth() / 2, 0, • Graphics.TOP | Graphics.HCENTER);• }• }
![Page 5: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/5.jpg)
Fonts• The Font class can be used to control the way text appears on the Canvas• Atributtes:
• Style• Size• Face
• Fonts usam âncoras com as coordenadas (x,y) para decidir ontem serão desenhadas
![Page 6: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/6.jpg)
Fonts• public void paint(Graphics g) {• Font f =
Font.getFont(Font.FACE_PROPORTIONAL, •
Font.STYLE_UNDERLINED, •
Font.SIZE_LARGE);• g.setFont(f);• g.drawString("System Font", • getWidth()/2, getHeight()/2, • Graphics.TOP | Graphics.HCENTER);• }
![Page 7: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/7.jpg)
Events• The developer is resposible to manage all input events
• Input events are throw when:• A key is pressed ( for keyboard enabled devices)• The screen is touched (for touchscreen devices)• A command is chosen
![Page 8: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/8.jpg)
Events• For keyboard enabled devices use the following methods to capture key events:• keyPressed(int keyCode)• keyReleased(int keyCode)• keyRepeated(int keyCode)
• For touchscreen enabled devices use the following methods to capture touch events:• pointerPressed(int x, int y)• pointerReleased(int x, int y)• pointerDragged(int x, int y)
![Page 9: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/9.jpg)
Events•public class MyCanvas2 extends Canvas {
int x,y; boolean firstTime = true; boolean userHasTouched = false;
public void paint(Graphics g) { if (firstTime) { g.setColor(255, 255, 0); g.fillRect(0, 0, getWidth(), getHeight()); firstTime = false; } if (userHasTouched) { g.setColor(0, 0, 0); g.fillArc(x, y, 5, 5, 0, 360); userHasTouched = false; } } protected void pointerPressed(int x, int y) { this.x = x; this.y = y; userHasTouched = true; repaint(); } }
![Page 10: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/10.jpg)
Sample• Checkers Game
![Page 11: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/11.jpg)
Game API• javax.microedition.lcdui.game package
• Help to develop games quickly and help to reduce the size of the JAR file
• Layers usage to create visual elements, for exemple:• Layer 1 – Background• Layer 2 – Enemy • Layer 3 – Player
![Page 12: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/12.jpg)
Game API - SampleGameCanvas
Sprite
Sprite
![Page 13: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/13.jpg)
Game loop•Game loop is responible to:• Handle player events• Update game logics• Repaint the screen scene• It runs inside a separeted thread
Graphics g = getGraphics();
while (isRunning) {
sleep();
update();
checkColisions();
layerManager.paint(g,0,0);
flushGraphics();
}
![Page 14: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/14.jpg)
Thread• Thread
![Page 15: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/15.jpg)
Sprites - Frames
{0,1,2,3,4,5,6,7,8,9,10,11,12}
![Page 16: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/16.jpg)
Sprite• Simplifies the Sprite management
Image image = Image.createImage(“mysprite.png”);
Sprite sprite = new Sprite(image, 5,5);
• Controls the Sprite movementsprite.move(10,10);
• Checks collisionssprite.collidesWith(otherSprite,false);
sprite.collidesWith(tiledLayer,false);
sprite.collidesWith(otherImage,20,20,false);
• Goes to the next frame of the spriteSprite.nextFrame();
• Controls transformations of the spritesprite.setTransform(Sprite.TRANS_ROT90);
![Page 17: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/17.jpg)
Player inputs•We have to get finger position
• The method pointerPressed(int x, int y) is called when the player touch the finger on the screen
• The method pointerDragged(int x, int y) is called when the player drags the finger on the screen
• The method pointerReleased(int x, int y) is called when the player takes the finger from the screen
protected void pointerPressed(int x, int y) { move = true; moveTO_Y = y;}
![Page 18: Game Development for Nokia Asha Devices with Java ME #2](https://reader035.vdocuments.mx/reader035/viewer/2022062303/554f6dbeb4c9058a148b5088/html5/thumbnails/18.jpg)
Let’s code