【flash】 水果忍者

    技术2022-07-10  148

    界面展示 源代码链接 链接:https://pan.baidu.com/s/1Fm4lnxKlMNYWg8JFngHeTg 提取码:4u6p

    主要内容 需要几个类 1.刀刃类 2.水果类 3.水果残骸类

    游戏主循环

    类的功能 Blade 刀刃类 进行刀刃绘制 和 水果的碰撞检测 Fruits 水果类 里面包含了水果和炸弹 FruitsWaste 水果残骸 水果死亡后产生的东西 GlobalInput 全局输入类 输入的东西会储存在这里 Sound_module 声音模块 一开始的那个调整声音大小用的 Tool 工具类 进行各种计算,或者常用的函数

    主函数 除去界面的主函数

    1.水果生成 2.升级检测 3.Tick方法运行 4.刀刃 和 水果进行碰撞检测 5.死亡处理 6.改变界面的文字 7.死亡处理

    //重置数量 if (list1.length==0) furistSpawner(level) //检测升级 levelUp() for (i=0;i<list1.length;i++) list1[i].Tick() for (i=0;i<list2_garbage.length;i++) list2_garbage[i].Tick() blade.Tick() //=======================碰撞检测==========================//水果 和 采样数组 for (i=0;i<list1.length;i++){ list1[i].hitCheck(blade.ExtendList) } //===================死亡处理========= clearAll() updateText() if (isLost){ gotoAndPlay(12) }

    刀刃类 tick函数 每帧执行函数 SampleGet 获得采样数组函数 onDraw 绘制函数

    tick函数相当于 游戏的主函数 1.进行位置采样 2.绘制直线 3.播放声音

    public function Tick(){//每帧执行函数 SampleGet() //采样数据 和 扩展采样 onDraw() //绘制直线 this.num++ //声音播放 if (num % 25 == 0 && this.SampleList.length >2 ){ num = Tool.randint(0,20) sound.play() } }

    刀刃 刀刃是一个 点的稀疏的数组。 鼠标移动的时候对其经过的点进行记录采样,用采样后的点进行各种操作。 采样点 用队列保存,每时间片采样 就会剔除最先进来的那个,然后对其绘制 造成了一个延迟效果。 和水果碰撞检测的时候,会吧稀疏的点数组 扩展成一个 密集的数组。 1.进行位置采样 判断鼠标按下 如果按下 { 如果采样数组 点的数量大于9 队列出列,以及在队列的末尾添加新的鼠标点 否则 就直接直接添加} 否则 采样数组变为空

    接下来是将采样数组进行扩充。 根据 第i个点 和 第i+1 个点求得一个单位向量。用这个单位向量 进行扩展。

    function SampleGet(){//获得采样数组 if (GlobalInput.mouseDown){ if (this.SampleList.length <9) this.SampleList.push(GlobalInput.mousePos.slice(0)) //将globalinput里面的数组的第0个元素塞到samplelist的最尾端 else{ this.SampleList.splice(0,1) //从第0位删除一个元素 this.SampleList.push(GlobalInput.mousePos.slice(0))// 插入第0个位置 } } else{ this.SampleList=[] } //扩展采样点 对采样点进行 检测 var i,j,x1,y1,x0,y0,nowX,nowY; this.ExtendList=[] for (i=1;i<this.SampleList.length;i++){ x1=this.SampleList[i][0] y1=this.SampleList[i][1] x0=this.SampleList[i-1][0] y0=this.SampleList[i-1][1] var vector = [x1-x0,y1-y0] var vector_length = Tool.getVectorLenght(vector) //模长 var unitary_vector = Tool.UnitaryVector(vector) //单位化 for (j=0;j<vector_length;j++){ nowX = x0 + unitary_vector[0]*j nowY = y0 + unitary_vector[1]*j this.ExtendList.push([nowX,nowY]) } } }

    2.绘制刀刃

    function onDraw(){//绘制刀刃 var i,list1 = this.SampleList this.contain.graphics.clear() if (list1.length>=1){ for (i=1;i<list1.length;i++){ this.contain.graphics.lineStyle(10,0xff00ff) this.contain.graphics.moveTo(list1[i-1][0],list1[i-1][1]) this.contain.graphics.lineTo(list1[i][0],list1[i][1]) } } }

    3 碰撞检测 在第10帧 吧刀刃的扩展后点数组 和所有水果进行碰撞检测。

    //=======================碰撞检测==========================//水果 和 采样数组 for (i=0;i<list1.length;i++){ list1[i].hitCheck(blade.ExtendList) }

    水果类 和 水果死亡类 Fruits FruitsWaste 根据传入来的 type生成对应的图片。 tick函数 没帧执行函数 。 move 函数 处理移动 和旋转 getImage 函数 根据type或者图片 hitCheck 碰撞检测。用当前的水果对象 和传进来的刀刃点数组进行碰撞 destroy 调用 destroySpawn 用来产生new出来 FruitsWaste 产生水果死亡特效

    FruitsWaste 根据传入的type 产生对应的特效 和让他下落

    一些全局类 GlobalInput Tool GlobalInput

    public static var mouseDown:Boolean=false; // 判断鼠标按下 public static var mousePos:Array=[0,0]; // 储存鼠标位置

    Tool

    static public function randint(min,max):int{ //取得一定范围内的随机数 return Math.round(Math.random()*(max-min))+min } static public function randfloat(min,max):Number{ return Math.random()*(max-min)+min } //向量模 static public function getVectorLenght(v){ return Math.sqrt(v[0]*v[0]+v[1]*v[1]) //如果是二维数组定义,怎么可以直接乘 } //向量单位化 static public function UnitaryVector(v){ var len = Tool.getVectorLenght(v) return [v[0]/len,v[1]/len] }
    Processed: 0.010, SQL: 9