Wednesday, January 5, 2011

Making a Flash game like Plants Vs Zombies – step 2

Making a Flash game like Plants Vs Zombies – step 2:

In the second part of the series, it’s time to raise money to buy a plant.

Also, we should add a smooth animation to falling suns, fix a bug which made them appear only on certain tiles and make them disappear if they aren’t picked up after a given amount of time.

Fixing the bug on newSun function

As reported by some readers, newSun function had a bug which did not allow to make the sun appear on every tile. It can be fixed assigning the x property this way:

sun.x=52+sunCol*65;

but since we are featuring smooth animation to falling sun, the entire newSun function can be changed this way:

private function newSun(e:TimerEvent):void {
 var sunRow:uint=Math.floor(Math.random()*5);
 var sunCol:uint=Math.floor(Math.random()*9);
 sun = new sunMc();
 sun.buttonMode=true;
 sunContainer.addChild(sun);
 sun.x=52+sunCol*65;
 sun.destinationY=130+sunRow*75;
 sun.y=-20;
 sun.addEventListener(MouseEvent.CLICK,sunClicked);
}

Now y property is set at -20 to make the sun appear outside the visible area, and its final y destination is saved in a variable called destinationY. We’ll see later what to do with it. Also notice the buttonMode property to make the mouse pointer change shape when it’s over the sun.

Collecting suns to raise money

First, we need a new variable to store the amount of player’s money, then we need a text field to display it somewhere. We also need Event and TextField classes to handle frame events and create on the fly text fields.

So these are the libraries we need to import in the package: [...]

No comments:

Post a Comment