在手机游戏的开发中,要做多机型的适配,但是越来越多的机器开始支持触摸屏操作。
那么我们也要很好的去实现其触摸屏的输入,以让玩家有类似甚至超过键盘控制的舒适感。
下面是一个触摸屏游戏按钮管理思想,对游戏中人物的触摸屏控制会在整理后放出。
该思路并不一定是最佳的,只是我是这么实现的。
-0-
屏幕上所有可以被点击的区域(RECT)按钮都是一个对象,他们有自己被点击的todo()方法,还有一个触点管理器,该管理器控制添加触摸按钮以及清理所有触摸按钮和每个按钮是否被点击的判定。
具体实现如下:
1
import
javax.microedition.lcdui.Graphics;
2
3
import
vectors.CVector;
4
5
import
base.CTools;
6
import
base.CType;
7
8
/**
9
* 触点区域
10
*
11
* @example 重写todo方法<br>
12
* pointAreaManager.addPionterArea(pointAreaManager.new
13
* PointArea(10, 10, 50, 50){
14
* <br>public void todo(){ <br>
15
* // 写要被点击后要做的逻辑<br>
16
*
17
* // ------------------------------------------------------------<br>
18
* }<br>
19
* });
20
*
21
*
22
*
@author
Colonleado
23
*
24
*/
25
public
class
PointAreaManager
{
26
27
public
CVector a
=
new
CVector();
28
29
public
void
addPionterArea(PointArea area)
{
30
31
a.addElement(area);
32
33
}
34
35
public
void
update()
{
36
37
for
(
int
i
=
0
; i
<
a.size();
++
i)
{
38
39
PointArea b
=
(PointArea) a.elementAt(i);
40
41
b.update();
42
43
}
44
45
}
46
47
public
void
debug(Graphics g)
{
48
for
(
int
i
=
0
; i
<
a.size();
++
i)
{
49
50
PointArea b
=
(PointArea) a.elementAt(i);
51
52
b.debug(g);
53
54
}
55
}
56
57
public
void
clear()
{
58
59
a.removeAllElements();
60
61
}
62
63
public
abstract
class
PointArea
{
64
65
private
int
x, y, width, height;
66
67
public
PointArea(
int
_x,
int
_y,
int
_width,
int
_height)
{
68
69
x
=
_x;
70
71
y
=
_y;
72
73
width
=
_width;
74
75
height
=
_height;
76
77
}
78
79
private
boolean
isPointerClick()
{
80
81
//
是否发生了触摸事件
82
if
(CType.havePointerEvent)
{
83
84
//
如果发生了触摸事件 检测下是否触点在该区域矩形内
85
if
(CTools.isPointInRect(CType.getPointerX(),
86
CType.getPointerY(), x, y, width, height))
{
87
88
CType.havePointerEvent
=
false
;
89
90
return
true
;
91
92
}
93
94
}
95
96
return
false
;
97
98
}
99
100
public
void
update()
{
101
102
//
如果被点击了 那么执行自己的todo
103
if
(isPointerClick())
{
104
105
todo();
106
107
}
108
109
}
110
111
//
抽象方法todo 供不同矩形按钮去实现
112
protected
abstract
void
todo();
113
114
public
void
debug(Graphics g)
{
115
g.setColor(
0x00ffff
);
116
g.drawRect(x, y, width, height);
117
}
118
119
}
120
121
}

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

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

我们在主类(一般是我们的Canvas)中实例一个PointAreaManager的对象,以此来完成对触摸屏输入的所有管理。
1
//
实例一个触点管理器
2
psm
=
new
PointAreaManager();
3
//
添加一个按钮
4
psm.addPionterArea(psm.
new
PointArea(
0
, CType.ScreenHeight
-
30
,
5
40
,
30
)
{
6
7
//
实现todo方法
8
protected
void
todo()
{
9
10
//
如果被点击了 就打开音乐
11
pointerAskMusicOk();
12
13
}
14
15
}
);

2

3

4

5

6

7

8

9

10

11

12

13

14

15

这样当进入一个新的界面时,我们只需要向管理器中添加我们需要的矩形区域按钮们,他们各自实现了自己的todo。而在游戏的逻辑更新中会执行管理器的update,管理器会去检查每一个按钮是否被点击,是就执行该按钮的todo。这样就做到了按钮自己管理自己。
当切换界面的时候只需要清理掉管理器中的所有按钮,再添加新按钮们即可。