@@ -28,13 +28,18 @@ var timer=0;
28
28
29
29
var score = 0 ;
30
30
31
+ var roll = 0 ;
32
+
31
33
var gameover = false ;
32
34
33
35
var formulas = [
34
- "X+d" ,
35
- "X-d" ,
36
- "X*a+d" , "X*a-d" ,
37
- "X/a+d" , "X/a-d"
36
+ "X+D" ,
37
+ "X-D" ,
38
+ "X*A&+D" , "X*A&-D" ,
39
+ "X/A&+D" , "X/A&-D" ,
40
+ "Math.sqrt(X)" ,
41
+ "X*X-D" ,
42
+ "D*Math.sin(X)"
38
43
] ;
39
44
40
45
var formula = "" ;
@@ -47,23 +52,26 @@ var Ball = cc.Class({
47
52
y :0 ,
48
53
orx :0 ,
49
54
ory :0 ,
50
- ortimer :0 ,
55
+ drx :0 ,
56
+ dry :0 ,
51
57
ballnode : null ,
52
58
labelnode : null ,
53
59
pnode : null , psf :null ,
54
- tow :false , toa :false , tod :false , tos :false ,
55
60
getSelfPosInBalls : function ( ) {
56
61
let id = 0 ;
57
62
for ( id = 0 ; id < balls . length ; id ++ ) {
58
63
if ( balls [ id ] . x == this . x && balls [ id ] . y == this . y && balls [ id ] . num == this . num ) break ;
59
64
}
60
65
return id ;
61
66
} ,
62
-
67
+ getR : function ( ) {
68
+ return this . ballnode . width * calcSize ( this . num ) / 2 ;
69
+ } ,
63
70
ctor : function ( node , sf ) {
64
71
this . pnode = node ;
65
72
this . psf = sf ;
66
73
this . num = Math . round ( Math . random ( ) * 19 ) + 1 ;
74
+ this . targetnum = this . num ;
67
75
68
76
//Set position
69
77
var size = cc . winSize ;
@@ -89,51 +97,23 @@ var Ball = cc.Class({
89
97
this . ballnode . on ( cc . Node . EventType . TOUCH_START , function ( event ) {
90
98
this . orx = this . x ;
91
99
this . ory = this . y ;
92
- this . ortimer = timer ;
93
- this . tow = false ;
94
- this . toa = false ;
95
- this . tod = false ;
96
- this . tos = false ;
100
+ this . drx = event . getLocationX ( ) - size . width / 2 - this . x ;
101
+ this . dry = event . getLocationY ( ) - size . height / 2 - this . y ;
97
102
} . bind ( this ) ) ;
98
103
99
104
//Move
100
105
this . ballnode . on ( cc . Node . EventType . TOUCH_MOVE , function ( event ) {
101
106
var size = cc . winSize ;
102
- this . setPos ( event . getLocationX ( ) - size . width / 2 , event . getLocationY ( ) - size . height / 2 ) ;
103
- if ( this . x - this . orx > 50 ) this . tow = true ;
104
- if ( this . x - this . orx < - 50 ) this . tos = true ;
105
- if ( this . y - this . ory > 50 ) this . tod = true ;
106
- if ( this . y - this . ory < - 50 ) this . toa = true ;
107
- //Throw
108
- if ( this . num >= 5 && ( this . tow && this . tos ) || ( this . tod && this . toa ) ) {
109
- let ballNum = Math . round ( Math . random ( ) * 8 ) + 2 ;
110
- let allNum = this . num ;
111
- for ( let i = 0 ; i < ballNum ; i ++ ) {
112
- let xd = Math . round ( Math . random ( ) * 38 ) + 12 ;
113
- let yd = Math . round ( Math . random ( ) * 38 ) + 12 ;
114
- if ( Math . random ( ) > 0.5 ) xd = - xd ;
115
- if ( Math . random ( ) > 0.5 ) yd = - yd ;
116
- let takeNum = i != ballNum - 1 ?Math . floor ( Math . random ( ) * allNum ) :allNum ;
117
- if ( takeNum === 0 ) break ;
118
- let ball = new Ball ( this . pnode , this . psf ) ;
119
- ball . setPos ( this . x + xd , this . y + yd ) ;
120
- ball . setNum ( takeNum ) ;
121
- allNum -= takeNum ;
122
- balls . push ( ball ) ;
123
- }
124
- this . rm ( ) ;
125
- removeFromBalls ( this . getSelfPosInBalls ( ) ) ;
126
- }
107
+ this . setPos ( event . getLocationX ( ) - size . width / 2 - this . drx , event . getLocationY ( ) - size . height / 2 - this . dry ) ;
127
108
} . bind ( this ) ) ;
128
-
129
109
130
110
this . ballnode . on ( cc . Node . EventType . TOUCH_END , function ( event ) {
131
111
//碰撞检测,合并
132
112
for ( let i = 0 ; i < balls . length ; i ++ ) {
133
113
let balli = balls [ i ] ;
134
114
let ballj = this ;
135
115
if ( balli . x == ballj . x && balli . y == ballj . y && balli . num == ballj . num ) continue ;
136
- if ( posDiff ( balli . x , balli . y , ballj . x , ballj . y ) < 20 ) {
116
+ if ( posDiff ( balli . x , balli . y , ballj . x , ballj . y ) < ballj . getR ( ) ) {
137
117
let ball = new Ball ( this . pnode , this . psf ) ;
138
118
ball . setPos ( ballj . x , ballj . y ) ;
139
119
ball . targetnum = balli . num + ballj . num ;
@@ -146,20 +126,6 @@ var Ball = cc.Class({
146
126
return ;
147
127
}
148
128
}
149
-
150
- //平分
151
- if ( this . num >= 2 && timer - this . ortimer < 1 && posDiff ( this . x , this . y , this . orx , this . ory ) < 10 ) {
152
- let ball1 = new Ball ( this . pnode , this . psf ) ;
153
- let ball2 = new Ball ( this . pnode , this . psf ) ;
154
- ball1 . setPos ( this . x - 15 , this . y ) ;
155
- ball1 . setNum ( Math . floor ( this . num / 2 ) ) ;
156
- ball2 . setPos ( this . x + 15 , this . y ) ;
157
- ball2 . setNum ( this . num - Math . floor ( this . num / 2 ) ) ;
158
- this . rm ( ) ;
159
- removeFromBalls ( this . getSelfPosInBalls ( ) ) ;
160
- balls . push ( ball1 ) ;
161
- balls . push ( ball2 ) ;
162
- }
163
129
} . bind ( this ) ) ;
164
130
} ,
165
131
setPos : function ( x , y ) {
@@ -205,10 +171,14 @@ cc.Class({
205
171
206
172
numsum :0 ,
207
173
174
+ outstart :false ,
175
+
208
176
genFormula : function ( ) {
209
- formula = formulas [ Math . floor ( Math . random ( ) * formulas . length ) ]
210
- . replace ( "a" , random ( 1 , 5 ) ) . replace ( "a!" , random ( 2 , 5 ) ) . replace ( "b" , random ( 1 , 100 ) ) . replace ( "c" , Math . floor ( randomfloat ( 0 , 0.1 ) * this . numsum ) )
211
- . replace ( "d" , Math . floor ( randomfloat ( 0.5 , 0.8 ) * this . numsum ) ) . replace ( "e" , randomfloat ( 0 , 1 ) ) ;
177
+ let id = Math . floor ( Math . random ( ) * formulas . length ) ;
178
+ if ( id != formulas . length - 1 && random ( 0 , 100 ) < roll ) id ++ ;
179
+ formula = formulas [ id ]
180
+ . replace ( "A&" , random ( 2 , 5 ) ) . replace ( "A" , random ( 1 , 5 ) ) . replace ( "B" , random ( 1 , 100 ) ) . replace ( "C" , Math . floor ( randomfloat ( 0 , 0.1 ) * this . numsum ) )
181
+ . replace ( "D" , Math . floor ( randomfloat ( 0.5 , 0.8 ) * this . numsum ) ) . replace ( "E" , randomfloat ( 0 , 1 ) ) ;
212
182
this . formulaNode . string = formula ;
213
183
} ,
214
184
@@ -217,13 +187,58 @@ cc.Class({
217
187
balls . push ( new Ball ( this . node , this . ballsprite ) ) ;
218
188
219
189
score = 0 ;
190
+ roll = 0 ;
220
191
formula = "" ;
221
192
this . numsum = 0 ;
222
193
for ( let i = 0 ; i < balls . length ; i ++ ) {
223
194
this . numsum += balls [ i ] . num ;
224
195
}
225
196
this . scoreNode . string = "Score: " + this . numsum + "/" + score ;
226
197
this . genFormula ( ) ;
198
+
199
+ this . node . on ( cc . Node . EventType . TOUCH_START , function ( event ) {
200
+ let size = cc . winSize ;
201
+ let x = event . getLocationX ( ) - size . width / 2 ;
202
+ let y = event . getLocationY ( ) - size . height / 2 ;
203
+ this . outstart = true ;
204
+ for ( let i = 0 ; i < balls . length ; i ++ ) {
205
+ if ( posDiff ( x , y , balls [ i ] . x , balls [ i ] . y ) < balls [ i ] . getR ( ) ) {
206
+ this . outstart = false ;
207
+ break ;
208
+ }
209
+ }
210
+ } . bind ( this ) ) ;
211
+
212
+ this . node . on ( cc . Node . EventType . TOUCH_END , function ( event ) {
213
+ if ( ! this . outstart ) return ;
214
+
215
+ let size = cc . winSize ;
216
+ let x = event . getLocationX ( ) - size . width / 2 ;
217
+ let y = event . getLocationY ( ) - size . height / 2 ;
218
+ for ( let i = 0 ; i < balls . length ; i ++ ) {
219
+ if ( balls [ i ] . num >= 2 && posDiff ( x , y , balls [ i ] . x , balls [ i ] . y ) < balls [ i ] . getR ( ) ) {
220
+ //平分
221
+ let ball1 = new Ball ( this . node , this . ballsprite ) ;
222
+ let ball2 = new Ball ( this . node , this . ballsprite ) ;
223
+ let ball1Num = Math . floor ( balls [ i ] . num / 2 ) ;
224
+ let ball2Num = balls [ i ] . num - Math . floor ( balls [ i ] . num / 2 ) ;
225
+ let d = balls [ i ] . ballnode . width * calcSize ( balls [ i ] . num / 2 ) * 2 ;
226
+ ball1 . setPos ( balls [ i ] . x - 30 , balls [ i ] . y ) ;
227
+ ball1 . setNum ( ball1Num ) ;
228
+ ball1 . targetnum = ball1Num ;
229
+ ball2 . setPos ( balls [ i ] . x + 30 , balls [ i ] . y ) ;
230
+ ball2 . setNum ( ball2Num ) ;
231
+ ball2 . targetnum = ball2Num ;
232
+ balls [ i ] . rm ( ) ;
233
+ removeFromBalls ( balls [ i ] . getSelfPosInBalls ( ) ) ;
234
+ balls . push ( ball1 ) ;
235
+ balls . push ( ball2 ) ;
236
+ break ;
237
+ }
238
+ }
239
+
240
+ } . bind ( this ) ) ;
241
+
227
242
} ,
228
243
229
244
// use this for initialization
@@ -247,8 +262,8 @@ cc.Class({
247
262
248
263
let test = false ;
249
264
for ( let i = 0 ; i < balls . length ; i ++ ) {
250
- //Test Apply formula
251
- if ( parseInt ( eval ( formula . replace ( "X" , balls [ i ] . num ) ) ) > 0 ) {
265
+ //Test apply formula
266
+ if ( parseInt ( eval ( formula . replace ( "X" , balls [ i ] . targetnum ) . replace ( "X" , balls [ i ] . targetnum ) ) ) > 0 ) {
252
267
test = true ;
253
268
break ;
254
269
}
@@ -258,10 +273,11 @@ cc.Class({
258
273
259
274
if ( timer >= TimeEachRound ) {
260
275
timer = 0 ;
276
+ roll ++ ;
261
277
262
278
for ( let i = 0 ; i < balls . length ; ) {
263
279
//Apply formula
264
- balls [ i ] . targetnum = parseInt ( eval ( formula . replace ( "X" , balls [ i ] . num ) ) ) ;
280
+ balls [ i ] . targetnum = parseInt ( eval ( formula . replace ( "X" , balls [ i ] . targetnum ) . replace ( "X" , balls [ i ] . targetnum ) ) ) ;
265
281
if ( balls [ i ] . targetnum <= 0 ) {
266
282
balls [ i ] . rm ( ) ;
267
283
removeFromBalls ( i ) ;
@@ -272,7 +288,7 @@ cc.Class({
272
288
273
289
this . numsum = 0 ;
274
290
for ( let i = 0 ; i < balls . length ; i ++ ) {
275
- this . numsum += balls [ i ] . num ;
291
+ this . numsum += balls [ i ] . targetnum ;
276
292
}
277
293
if ( this . numsum > score ) score = this . numsum ;
278
294
this . scoreNode . string = "Score: " + this . numsum + "/" + score ;
0 commit comments