flash 植物大战僵尸制作3

5:开火,消灭敌人。游戏的核心代码

 

private function onEnterFrm(e:Event):void {
            var i:int;
            var j:int;
            for (i=0; i<plantContainer.numChildren; i++) {
                var currentPlant:plantMc=plantContainer.getChildAt(i) as plantMc;
                // 获取容器中植物的索引
                if (currentPlant.recharge==currentPlant.fireRate&&! currentPlant.isFiring) {
                    // 与植物同一行的僵尸
                    if (zombiesArray[currentPlant.plantRow].length>0) {
                        // 遍历所有僵尸
                        for (j=0; j<zombiesArray[currentPlant.plantRow].length>0; j++) {
                            var targetZombie:zombieMc=zombieContainer.getChildByName(zombiesArray[currentPlant.plantRow][j]) as zombieMc;
                            // 判断如果僵尸在植物的右侧
                            if (targetZombie.x>currentPlant.x) {
                                var bullet:bulletMc=new bulletMc();//产生一个新子弹,并加入子弹容器中
                                bulletContainer.addChild(bullet);
                                bullet.x=currentPlant.x;
                                bullet.y=currentPlant.y;
                                bullet.sonOf=currentPlant;
                                currentPlant.recharge=0;
                                currentPlant.isFiring=true;
                                break;
                            }
                        }
                    }
                }
                // 检测植物是否需要填装子弹
                if (currentPlant.recharge<currentPlant.fireRate) {
                    currentPlant.recharge++;/
                }
            }
            //
            // 子弹的管理
            //
            for (i=0; i<bulletContainer.numChildren; i++) {
                var movingBullet:bulletMc=bulletContainer.getChildAt(i) as bulletMc;
                movingBullet.x+=3;//向右移动子弹3个像素
                var firingPlant:plantMc=movingBullet.sonOf as plantMc;// 找到射出子弹的植物
                // 判断子弹是否飞出屏幕
                if (movingBullet.x>650) {
                    firingPlant.isFiring=false;//植物不再开火
                    bulletContainer.removeChild(movingBullet);//
                } else {
                    for (j=0; j<zombieContainer.numChildren; j++) {
                        var movingZombie:zombieMc=zombieContainer.getChildAt(j) as zombieMc;
                        // 判断子弹是否碰闯到僵尸
                        if (movingZombie.hitTestPoint(movingBullet.x,movingBullet.y,true)) {
                            movingZombie.alpha-=0.3;//减少透明度
                            firingPlant.isFiring=false;
                            bulletContainer.removeChild(movingBullet);//移除子弹
                            //判断如果僵尸的透明度为0了,则僵尸死翘翘
                            if (movingZombie.alpha<0) {
                                zombiesArray[movingZombie.zombieRow].splice(zombiesArray[movingZombie.zombieRow].indexOf(movingZombie.name),1);//从行中移除僵尸
                                zombieContainer.removeChild(movingZombie);
                            }
                            break;
                        }
                    }
                }
            }
            //
            // 僵尸管理
            //
            var zombieColumn:int;
            for (i=0; i<zombieContainer.numChildren; i++) {
                movingZombie=zombieContainer.getChildAt(i) as zombieMc;
                zombieColumn = Math.floor((movingZombie.x-25)/65);// gets zombie column
                // 查看是否某个植物与僵尸在同一方格中
                if (zombieColumn<0||zombieColumn>8||plantsArray[movingZombie.zombieRow][zombieColumn]==0) {
                    movingZombie.x-=0.5;//移动僵尸0.5个像素点
                } else {
                    //  植物遭到攻击,并获取到当前方格中植物的对象
                    var attackedPlant:plantMc=plantContainer.getChildByName("plant_"+movingZombie.zombieRow+"_"+zombieColumn) as plantMc;
                    attackedPlant.alpha-=0.01;//削弱植物的透明度
                    //植物死亡
                    if (attackedPlant.alpha<0) {
                        plantsArray[movingZombie.zombieRow][zombieColumn]=0;
                        plantContainer.removeChild(attackedPlant);
                    }
                }
            }
}

 

简陋的植物大战僵尸

 

posted @ 2013-11-25 16:08  无名盗闪  阅读(299)  评论(0编辑  收藏  举报