flash 植物大战僵尸制作2
3: 阳光收集够了,可以摆放植物了。addPlants()
private function addPlants():void { // 添加植物对象容器 addChild(plantContainer); plant=new plantMc(); plantContainer.addChild(plant); plant.buttonMode=true; plant.x=90; plant.y=40; plant.addEventListener(MouseEvent.CLICK,onPlantClicked); } private function onPlantClicked(e:MouseEvent):void { if (money>=10&&! playerMoving) { money-=10; updateMoney(); selector=new selectorMc(); selector.visible=false; plantContainer.addChild(selector); movingPlant=new plantMc(); movingPlant.addEventListener(MouseEvent.CLICK,placePlant); plantContainer.addChild(movingPlant); playerMoving=true; } }
在这里我们通过从游戏上方选择相应的植物后,通过逐帧事件判断鼠标按下的位置来摆放所选择的植物
//------ 逐帧函数 -------- private function onEnterFrm(e:Event):void {// 条件成立后可以岁鼠标移动并摆放 if (playerMoving) { movingPlant.x=mouseX; movingPlant.y=mouseY; var plantRow:int=Math.floor((mouseY-80)/75); var plantCol:int=Math.floor((mouseX-25)/65); if (plantRow>=0&&plantCol>=0&&plantRow<5&&plantCol<9) { selector.visible=true; selector.x=25+plantCol*65; selector.y=80+plantRow*75; } else { selector.visible=false; } } }
4:植物摆放成功后,现在僵尸就要出场喽 addZombies()
private function addZombies():void { //添加僵尸容器 addChild(zombieContainer); //开始僵尸计数器对象 zombieTimer.start(); zombieTimer.addEventListener(TimerEvent.TIMER,newZombie); } private function newZombie(e:TimerEvent):void { var zombie:zombieMc=new zombieMc(); zombieContainer.addChild(zombie); var row:uint=Math.floor(Math.random()*5); zombie.x=660; zombie.y=row*75+115; }
为了让僵尸移动起来,我们还的在逐帧事件中增加移动代码
private function onEnterFrm(e:Event):void { // 循环移动所有的僵尸 for (var i:uint=0; i<zombieContainer.numChildren; i++) { var movingZombie:zombieMc=zombieContainer.getChildAt(i) as zombieMc; movingZombie.x-=0.5; }
到此为止元素都齐了,阳光也有了,植物也种了,僵尸也来了那剩下的就是开火射击消灭僵尸喽