1 package
2 {
3 import flash.display.MovieClip;
4 import flash.events.MouseEvent;
5 import flash.utils.Timer;
6 import flash.events.TimerEvent;
7 import flash.text.TextField;
8 import flash.events.Event;
9 import flash.utils.getTimer;
10 import flash.display.IBitmapDrawable;
11 import flash.media.SoundChannel;
12 import flash.net.SharedObject;
13
14
15 public class MyGame extends MovieClip
16 {
17
18 private const boardWidth:uint = 6;
19 private const boardHeight:uint = 6;
20 private const cardWidth:Number = 52;
21 private const cardWHeight:Number = 52;
22 private const offsetX:Number = 120;
23 private const offsetY:Number = 45;
24 private const pointForMatch:int = 100;
25 private const pointForMiss:int = -5;
26
27
28 private var cardArr:Array;
29 private var firstCard:Card10;
30 private var secondCard:Card10;
31 private var cardNumbers:uint = 0;
32 private var flipBackTimer:Timer;
33
34 private var startTime:uint;
35 private var gameTime:uint;
36 private var timeText:TextField;
37 private var score:int;
38 private var scoreText:TextField;
39
40 private var firstSound:FirstCardSound=new FirstCardSound();
41 private var matchSound:MatchSound=new MatchSound();
42 private var missSound:MissSound=new MissSound();
43
44
45
46 public function MyGame():void
47 {
48 //var highScore:SharedObject = SharedObject.getLocal("userHighScore");
49 //trace(highScore.data.HS);
50 //highScore.data.HS = 0;
51 //highScore.flush();
52 //so.data.highScore = new Number(1234567890);
53 //so.flush();
54
55 startTime = getTimer();
56 gameTime = 0;
57
58 timeText=new TextField();
59 timeText.x = 400;
60 addChild(timeText);
61
62 scoreText=new TextField();
63 scoreText.x = 50;
64 score = 0;
65 addChild(scoreText);
66
67
68 addEventListener(Event.ENTER_FRAME,showTimeAndScore);
69
70
71
72 var Arr:Array=new Array();
73 cardArr=new Array();
74 for (var i:uint=0; i<boardWidth*boardHeight/2; i++)
75 {
76 Arr.push(i);
77 Arr.push(i);
78 }
79
80 for (i=0; i<boardHeight; i++)
81 {
82 for (var j:uint=0; j<boardWidth; j++)
83 {
84 var thiscard:Card10=new Card10();
85 //thiscard.stop();
86 cardArr.push(thiscard);
87 thiscard.x = i * cardWidth + offsetX;
88 thiscard.y = j * cardWHeight + offsetY;
89 var no:uint = Math.floor(Math.random() * Arr.length);
90 thiscard.cardFace = Arr[no];
91 Arr.splice(no,1);
92 thiscard.gotoAndStop(thiscard.cardFace+2);
93 //thiscard.gotoAndStop(1);
94 thiscard.addEventListener(MouseEvent.CLICK,cardClik);
95 addChild(thiscard);
96 cardNumbers++;
97 }
98 }
99
100 flipBackTimer = new Timer(2000,1);
101 flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE,allFlipBack);
102 flipBackTimer.start();
103
104 }
105
106 private function showTimeAndScore(event:Event)
107 {
108 gameTime = getTimer() - startTime;
109 timeText.text = "time:" + clockTime(gameTime);
110 scoreText.text = "score:" + String(score);
111
112 }
113
114 private function clockTime(time:uint):String
115 {
116 var seconds:int = Math.floor(time / 1000);
117 var minutes:int = Math.floor(seconds / 60);
118 seconds -= minutes * 60;
119 var timeString:String = minutes + ":" + String(seconds + 100).substr(1,2);
120 return timeString;
121 }
122
123 private function allFlipBack(event:TimerEvent)
124 {
125 for (var i:uint=0; i<cardArr.length; i++)
126 {
127 //if(!this.getChildAt(i) is MovieClip)
128 //trace("yes");
129 //(Card10)(this.getChildAt(i)).startFlip(1);
130 cardArr[i].startFlip(1);
131
132 }
133
134 //trace("t");
135 flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,allFlipBack);
136
137 }
138
139 private function cardClik(event:MouseEvent)
140 {
141 //var highScore:SharedObject = SharedObject.getLocal("userHighScore");
142 //trace(so.data.highScore);
143 //var file:FileReference=new FileReference();
144 //file.save(clockTime(gameTime),"t.txt");
145 //MovieClip(root).gotoAndStop("GameOver");
146 var thiscard:Card10=(event.currentTarget as Card10);
147 //trace(thiscard.cardFace);
148 if (firstCard == null)
149 {
150 firstCard = thiscard;
151 //thiscard.gotoAndStop(thiscard.cardFace+2);
152 thiscard.startFlip(thiscard.cardFace+2);
153 score += pointForMiss;
154 PlaySound(firstSound);
155 }
156 else if (firstCard==thiscard)
157 {
158 thiscard.startFlip(1);
159 firstCard = null;
160 PlaySound(firstSound);
161
162
163 }
164 else if (secondCard==null)
165 {
166 secondCard = thiscard;
167 //thiscard.gotoAndStop(thiscard.cardFace+2);
168 thiscard.startFlip(thiscard.cardFace+2);
169 if (firstCard.cardFace == secondCard.cardFace)
170 {
171 PlaySound(matchSound);
172 removeChild(firstCard);
173 removeChild(secondCard);
174 firstCard = null;
175 secondCard = null;
176 cardNumbers -= 2;
177 if (cardNumbers == 0)
178 {
179 //trace(cardNumbers);
180
181 MovieClip(root).Time = clockTime(gameTime);
182 MovieClip(root).Score = score;
183
184 var highScore:SharedObject = SharedObject.getLocal("userHighScore");
185
186 if (score > highScore.data.HS)
187 {
188 highScore.data.HS=score;
189 highScore.flush();
190 }
191 MovieClip(root).HighScore=highScore.data.HS;
192 MovieClip(root).gotoAndStop("GameOver");
193 }
194 //trace(cardNumbers);
195 score += pointForMatch;
196
197
198 }
199 else
200 {
201 PlaySound(missSound);
202 //flipBackTimer=new Timer(2000,1);
203 flipBackTimer.reset();
204 flipBackTimer.addEventListener(TimerEvent.TIMER_COMPLETE,FlipBack);
205 flipBackTimer.start();
206 score += pointForMiss;
207
208 }
209
210 }
211 else
212 {
213 PlaySound(firstSound);
214 //firstCard.gotoAndStop(1);
215 //secondCard.gotoAndStop(1);
216 firstCard.startFlip(1);
217 secondCard.startFlip(1);
218 firstCard = thiscard;
219 //thiscard.gotoAndStop(thiscard.cardFace+2);
220 thiscard.startFlip(thiscard.cardFace+2);
221 secondCard = null;
222 flipBackTimer.stop();
223 score += pointForMiss;
224 }
225
226 }
227 private function FlipBack(event:TimerEvent)
228 {
229 if (firstCard != null && secondCard != null)
230 {
231
232 firstCard.startFlip(1);
233 secondCard.startFlip(1);
234 firstCard = null;
235 secondCard = null;
236
237 }
238 flipBackTimer.removeEventListener(TimerEvent.TIMER_COMPLETE,FlipBack);
239 }
240
241 private function PlaySound(soundObj:Object)
242 {
243 var channel:SoundChannel = soundObj.play();
244 }
245
246
247 }
248
249 }