第一个flash游戏--配对游戏
看了《AS3.0游戏编程大学》用里面的素材模仿做了第一个flash游戏。。。
game
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 }
卡片类代码:
1 package 2 { 3 import flash.events.Event; 4 5 public class Card10 extends Card 6 { 7 8 private var cardface:uint; 9 private var flipToFrame:uint; 10 private var Step:uint; 11 public function Card10():void 12 { 13 cardface=0; 14 } 15 16 public function get cardFace():uint 17 { 18 return cardface; 19 } 20 public function set cardFace(cf:uint):void 21 { 22 cardface=cf; 23 } 24 25 public function startFlip(toWhichFrame:uint) 26 { 27 flipToFrame=toWhichFrame; 28 Step=10; 29 this.addEventListener(Event.ENTER_FRAME,flip); 30 } 31 32 public function flip(event:Event) 33 { 34 Step--; 35 if (Step > 5) 36 { // first half of flip 37 this.scaleX = .20*(Step-6); 38 } 39 else 40 { // second half of flip 41 this.scaleX = .20*(5-Step); 42 } 43 44 if(Step==5) 45 { 46 gotoAndStop(flipToFrame); 47 } 48 49 if(Step==0) 50 this.removeEventListener(Event.ENTER_FRAME,flip); 51 } 52 53 } 54 55 }
把flash嵌入html代码:
<title>DEMO</title> <head> <script type="text/javascript" src="http://files.cnblogs.com/wonderKK/swfobject.js"></script> </head> <body><form id="Form1"><div id="flashcontent"><a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" border="0" /> </a> </div></form><script type="text/javascript"> var so = new SWFObject("http://files.cnblogs.com/wonderKK/game.swf", "mymovie", "800", "600", "7", "#FFFFFF");so.write("flashcontent");</script> </body>
第一个flash游戏--配对游戏相关推荐
- 做一个FLASH游戏你需要掌握的东西【实用】
做一个FLASH游戏你需要掌握的东西 作者:jianzhong 一直想着什么时间好好做一个像样点的游戏,于是刻意的开始去了解FLASHGAME的相关资料,在这里把自己在整个制作和收集过程中的一些感觉使 ...
- 第一个Flash小游戏制作(1)
目标:是制作一个类似连连看的游戏(如下图). 素材可以在附件中下载,包括: 1.图片的MovieClip 2.两个自定义按钮(也可以使用自己的按钮) 3.一些声音文件 第一步: 我们先新建一个(AS3 ...
- [air for ios] 三小时开发一个iOS飞行射击游戏
[air for ios] 三小时开发一个iOS飞行射击游戏 http://www.badyoo.com/index.php/2012/07/04/158/index.html 2012-07-04 ...
- GemCraft Labyrinth:超耐玩的在线flash塔防游戏
GemCraft Labyrinth是一个在线flash塔防游戏,中文名字可以理解为宝石塔防.这个游戏拥有众多的关卡(169关),所以和植物大战僵尸一样十分的耐玩,除此之外,独特的宝石合成模式.技能点 ...
- flash游戏代码html5,Flash贪吃蛇游戏AS代码翻译
Flash贪吃蛇游戏AS代码翻译 互联网 发布时间:2008-10-06 01:25:13 作者:佚名 我要评论 今天翻译了一段经典的贪吃蛇代码,译后感觉还有很多地方不太妥当,很多不妥的地 ...
- 学习Flash制作高射炮游戏
主要是利用Flash Actionscript一步一步学习Flash高射炮简单游戏的制作过程,最终效果只是一个简单的演示,如果你有兴趣可以继续深入学习!开篇前,先把所有的演示动画的源程序提供给大家: ...
- HTML5 canvas 游戏设计:创建一个经典的魔塔游戏
整个项目都已经开源,项目地址:https://github.com/m8705/MAGIC-TOWER-JS 注:这是我高中时候的作品,BUG 很多,已经不再更新了.下载项目到本地就能玩. 前言 魔塔 ...
- 图片配对游戏Demo的学习和改进
学习几个小游戏来打发时间,这个图片配对游戏其实非常简单,就是一个鼠标移动点击游戏,运行图如下: 就是将图片移到相应的位置然后位置就会变成绿色,意味着成功.但是看完代码,我个人觉得还是有些地方的逻辑并不 ...
- Flash在移动游戏中渲染动画模型
游戏程序 平台类型: 程序设计: 编程语言: 引擎/SDK: 源:Adobe 动画模型是在游戏中移动的对象,它们能够表示你的角色.车辆.怪物和任何其它互动对象,它们不是明确地在背景中画 ...
最新文章
- 这应该是脑结构、脑工作原理最详细的图解了
- PMcaff活动 | 汪培公:农村电商模式的重新思考
- Delphi 中自定义异常及异常处理的一般方法
- SpringMVC环境简单搭建
- android下获取无线wif信号、ssid、MAC等操作类
- SQL事务控制语言(TCL)
- MongoDB 教程 | 菜鸟教程
- 实训汇编语言设计——内存多字节10进制数相加
- js读取文件的内置方法之 FileReader
- 计算机端口详细介绍(整理版)
- (7) PyQt 设计并实现【工厂扫码装箱系统】- Python代码实现BarTender自动化打印条码标签
- OHSAS18001与ISO14001体系的一体化及其审核(转载)
- 演唱会网上订票系统(SSM,JSP,MYSQL)
- 详解Unity的几种移动方式实现
- QQ被盗如何找回好友
- java opencv 实现换脸
- 计算机基础及photoshop应用试题,计算机基础及Photoshop应用选择题(计算机一级B考试卷).doc...
- NBA GLOSSARY
- java xmap_转:使用XMAP完成JavaBean和XML之间转换
- 预约挂号医院管理项目----Service-cmn模块—数据字典
热门文章
- sis最新ip地址2020_2020年12月版 最新IP数据库 号段归属地 省市区镇村行政区划
- linux网络方面命令大全,Linux下必须知道的11个网络命令
- Ubuntu 安装 python-opencv
- 大数据学习笔记44:Hive架构
- Python学习笔记:pandas初体验
- iphone退款申请教程_【揭秘】朋友圈卖的iOS退款、王者荣耀0元撸点券教程
- bzoj1047 [HAOI2007]理想的正方形 单调队列
- 2017.2.27自测
- 四.3D-2D:PnP问题求解 线性法
- 搭建LNMP基础框架