2
2
3
3
package ph .sm .colorbubbles ;
4
4
5
+ import java .util .LinkedList ;
6
+
5
7
import ch .aplu .android .*;
6
8
import android .graphics .Point ;
7
9
@@ -13,6 +15,7 @@ public class ColorBubbles extends GameGrid implements GGFlingListener, GGActorCo
13
15
protected GGPanel p ;
14
16
private int nbBalls = 6 ;
15
17
protected int nbBubbles = 25 ;
18
+ private LinkedList <Bubble > bubbles = new LinkedList <Bubble >();
16
19
private int flingThreshold = 2 ;
17
20
private int hits = 0 ;
18
21
private int shots = 0 ;
@@ -38,6 +41,7 @@ public void main()
38
41
{
39
42
int type = (int )(Math .random () * 6 );
40
43
Bubble b = new Bubble (type );
44
+ bubbles .add (b );
41
45
b .setCollisionCircle (new Point (0 , 0 ), 21 );
42
46
int d = (int )((double )pixelToVirtual (getNbHorzCells ()) / (nbBubbles + 1 ));
43
47
addActorNoRefresh (b , new Location (virtualToPixel ((i + 1 ) * d ), virtualToPixel (30 )));
@@ -48,6 +52,11 @@ public void main()
48
52
Ball ball = new Ball (this , i );
49
53
Location loc = new Location (p .toPixelX (i - 2.5 ), p .toPixelY (0.5 ));
50
54
addActorNoRefresh (ball , loc );
55
+
56
+ for (Bubble b : bubbles ) {
57
+ if (b .getType () == ball .getType ())
58
+ ball .addCollisionActor (b );
59
+ }
51
60
}
52
61
doRun ();
53
62
status .setText ("Fling a ball!" );
@@ -65,9 +74,7 @@ public boolean flingEvent(Point start, Point end, GGVector velocity)
65
74
{
66
75
b .addActorCollisionListener (this );
67
76
b .setCollisionCircle (new Point (0 , 0 ), 21 );
68
- for (Actor bubble : getActors (Bubble .class ))
69
- b .addCollisionActor (bubble );
70
- ((Ball )b ).init (p .toUserX (b .getXStart ()), p .toUserY (b .getYStart ()), vx , vy );
77
+ ((Ball )b ).shoot (vx , vy );
71
78
shots ++;
72
79
return true ;
73
80
}
@@ -81,12 +88,9 @@ public boolean flingEvent(Point start, Point end, GGVector velocity)
81
88
82
89
public int collide (Actor actor1 , Actor actor2 )
83
90
{
84
- if (((Ball )actor1 ).getType () == ((Bubble )actor2 ).getType ())
85
- {
86
- playTone (1200 , 20 );
87
- actor2 .removeSelf ();
88
- hits ++;
89
- }
91
+ playTone (1200 , 20 );
92
+ actor2 .removeSelf ();
93
+ hits ++;
90
94
displayResult ();
91
95
return 0 ;
92
96
}
@@ -111,21 +115,22 @@ public Ball(ColorBubbles app, int type)
111
115
{
112
116
super (type );
113
117
this .app = app ;
114
- setActEnabled (false );
115
118
}
116
119
117
- public void init (double x , double y , double vx , double vy )
120
+ public void reset () {
121
+ setActEnabled (false );
122
+ x = app .p .toUserX (getXStart ());
123
+ y = app .p .toUserY (getYStart ());
124
+ }
125
+ public void shoot (double vx , double vy )
118
126
{
119
- this .x = x ;
120
- this .y = y ;
121
127
this .vx = vx ;
122
128
this .vy = vy ;
123
129
setActEnabled (true );
124
130
}
125
131
126
132
public void act ()
127
133
{
128
- //vy = vy - g * dt;
129
134
x = x + vx * dt ;
130
135
y = y + vy * dt ;
131
136
setLocation (new Location (app .p .toPixelX (x ), app .p .toPixelY (y )));
@@ -141,17 +146,16 @@ public void act()
141
146
// -----------class Bubble -----------------
142
147
class Bubble extends Actor
143
148
{
144
- protected int type ;
145
149
146
150
public Bubble (int type )
147
151
{
148
- super ("peg_" + type );
149
- this . type = type ;
152
+ super ("peg" , 6 );
153
+ show ( type ) ;
150
154
}
151
155
152
156
public int getType ()
153
157
{
154
- return type ;
158
+ return getIdVisible () ;
155
159
}
156
160
157
161
}
0 commit comments