Salut à tous !
Je suis nouveau dans le monde d'androïd, et dans le cadre d'un projet pour ma licence, Je doit développer un petit jeu pour smartphone et tablette.
Le sujet est imposé. Il s'agit de réaliser un jeu de puzzle. En gros il faut reconstituer une image qui à été préalablement découper en plusieurs morceaux.
Rien de plus.
Je vous demande pas de me donner de réponse toute faite hein , mais j'aurais juste besoin d'aide pour la gestion du toucher sur l'écran. (les évènement quoi !)
Niveau algorithmique, c'est plutôt simple, même très mais android , un véritable casse tête !!
Pour commencer, j'ai décidé de créer une classe Piece qui étend Drawable (pour la méthode draw) et qui implémentes View.OnTouchListener.
L'idée été qu'il aurait suffit de modifier les valeurs des deux attributs x et y pour déplacer l'image dans le canvas.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 package info.ups.fr.puzzlegame_template; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; /** * Created by leocances on 24/03/15. */ public class Piece extends Drawable implements View.OnTouchListener{ private static final String TAG_LOG = "TouchActivity"; private static final boolean LOG = true; private int id, idG; private int x, y; private Bitmap image; public Piece(int id, int x, int y, Bitmap image) { this.id = id; this.x = x; this.y = y; this.image = image; } public Piece(int id, int idG, Bitmap image) { this.id = id; this.idG = idG; this.image = image; } public int getX() { return this.x;} public int getY() { return this.y;} public int getId() { return this.id;} public int getIdG() {return this.idG;} public Bitmap getImage() {return this.image;} @Override public void draw(Canvas canvas) { canvas.drawBitmap(image, x, y, null); } @Override public boolean onTouch(View v, MotionEvent event) { final int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: x += 10; v.invalidate(); log("x : " + x + "\ty : " + y); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: break; } return true; } @Override public void setAlpha(int alpha) {} @Override public void setColorFilter(ColorFilter cf) {} @Override public int getOpacity() {return 0;} private String stringValue(MotionEvent event) { final int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: return "ACTION_DOWN"; case MotionEvent.ACTION_MOVE: return "ACTION_MOVE"; case MotionEvent.ACTION_UP: return "ACTION_UP"; case MotionEvent.ACTION_CANCEL: return "ACTION_CANCEL"; } return ""; } private static void log(String message){ if (LOG) { Log.d(TAG_LOG, message); } } }
Cependant même après avoir déclarer mes SetOnTouchListener(piece) dans ma vue, les évènement ne sont pas gérer.
Pouvez-vous m'expliquer comment déclarer correctement mes évenements pour effectuer des action lord d'event sur l'écran ?
Partager