forked from buckyroberts/Source-Code-from-Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyGdxGame.java
More file actions
103 lines (82 loc) · 2.64 KB
/
Copy pathMyGdxGame.java
File metadata and controls
103 lines (82 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.thenewboston.buckyblaster;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class MyGdxGame extends ApplicationAdapter implements InputProcessor {
private SpriteBatch batch;
//Screen dimensions will be used to center text
private BitmapFont font;
private int screenWidth, screenHeight;
private String message = "Touch me";
//Set screen dimensions, font, and use this class for input processing
@Override
public void create () {
batch = new SpriteBatch();
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getHeight();
font = new BitmapFont();
font.setColor(Color.BLUE);
font.scale(5);
Gdx.input.setInputProcessor(this);
}
//Don't forget to free font
@Override
public void dispose() {
batch.dispose();
font.dispose();
}
//Get middle of screen and adjust for message size
@Override
public void render () {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
BitmapFont.TextBounds textSize = font.getBounds(message);
float x = screenWidth/2 - textSize.width/2;
float y = screenHeight/2 + textSize.height/2;
batch.begin();
font.draw(batch, message, x, y);
batch.end();
}
//Return true to indicate that the event was handled
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
message = "Touched down at " + screenX + ", " + screenY;
return true;
}
//When finger is lifted up
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
message = "Touch up at " + screenX + ", " + screenY;
return true;
}
//While dragging finger across screen
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
message = "Dragging at " + screenX + ", " + screenY;
return true;
}
@Override
public boolean keyDown(int keycode) {
return false;
}
@Override
public boolean keyUp(int keycode) {
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
}