Android Games: Exemplo 03
De Aulas
Links relacionados: Jogos Digitais
Recursos
Classe Main
1package com.mygdx.game;
2
3import com.badlogic.gdx.ApplicationAdapter;
4import com.badlogic.gdx.Gdx;
5import com.badlogic.gdx.graphics.GL20;
6import com.badlogic.gdx.graphics.Texture;
7import com.badlogic.gdx.graphics.g2d.SpriteBatch;
8import com.badlogic.gdx.audio.Sound;
9import com.badlogic.gdx.math.Vector3;
10
11public class MyGdxGame extends ApplicationAdapter {
12 private SpriteBatch batch;
13 private Texture background;
14 private Texture sphere;
15 private Sound sound;
16 private int w, h, x, y;
17 private boolean touched;
18
19 @Override
20 public void create() {
21 w = Gdx.graphics.getWidth();
22 h = Gdx.graphics.getHeight();
23 batch = new SpriteBatch();
24 background = new Texture("background.png");
25 sphere = new Texture("sphere.png");
26 sound = Gdx.audio.newSound(Gdx.files.internal("basso.wav"));
27 }
28
29 @Override
30 public void render() {
31 run();
32 Gdx.gl.glClearColor(1, 0, 0, 1);
33 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
34 batch.begin();
35 batch.draw(background, 0, 0, w, h);
36 batch.draw(sphere, x, y);
37 batch.end();
38 }
39
40 public void run() {
41 if (Gdx.input.isTouched()) {
42 Vector3 touchPos = new Vector3();
43 touchPos.set(Gdx.input.getX(), h - Gdx.input.getY(), 0);
44 x = (int) (touchPos.x - sphere.getWidth() / 2);
45 y = (int) (touchPos.y - sphere.getHeight() / 2);
46 if (!touched) {
47 sound.play();
48 touched = true;
49 }
50 } else {
51 touched = false;
52 }
53 }
54
55 @Override
56 public void dispose() {
57 batch.dispose();
58 background.dispose();
59 sphere.dispose();
60 sound.dispose();
61 }
62}