Flash game development practices (1)

Source: Internet
Author: User
Tags addchild event listener

I have to admit that I am not a zombie fan of Plants vs. Zombies, So if you find anything wrong with this series of tutorials, please let me know.

Define the main structure of a game

The plant war gives us good visual and sensory enjoyment. You have to protect your house from zombie intrusion, which is quite attractive. In general, killing botnets is very interesting. But this visual thing has nothing to do with the gameplay. We can replace it with the butchers, the pigeons, and the camels.

In this series of tutorials, we use circles to replace plants and blocks to replace botnets. Great circles will prevent evil squares from entering our base. The game region can be simplified to a block game.

Imagine this situation:

Then try to think like this:

All we have is a (0th) plant that will try its best to stop the botnets that walk on the second line (starting from line 2, the same below, and another zombie is approaching our base along the third line, and there is a sunlight that is falling from the fourth column. There cannot be another plant located in (2, 2), and it is impossible for a zombie to walk between the second row and the third row.

Create a game Region

As you can see, the game region is a matrix of five rows and nine columns. Therefore, the first thing to do is to define an array. I suppose your FLA file has a document class named main. The content of Main. As is as follows:

 
package {        import flash.display.Sprite;        public class Main extends Sprite {                private var gameField:Array;                public function Main():void {                        setupField();                }                private function setupField():void {                        gameField=new Array();                        for (var i:uint=0; i<5; i++) {                                gameField[i]=new Array();                                for (var j:uint=0; j<9; j++) {                                        gameField[i][j]=0;                                }                        }                }        }}

 

 

In this way, the gamefield array is a matrix of 5*9.

Draw game regions

The following code is sufficient for testing. At least we will draw a game area.

 

All the operations done by the drawfield function can be implemented in the setupfield function, but I want them to be separated. I have to let a function only process one thing. The only interesting line of code is that 25th rows generate random colors between #007d00 and #00ae00.

This is our game region. If you want to give area block points. Please... Self-help.

Capture sunlight

Sunshine is the currency in Plants vs. Zombies. They fell down from the sky and fell onto a block. You can buy plants only when you pick up the sunlight.

I used the timer event to bring down a ray of sunshine every five seconds. If you are not familiar with timer, please search.

Now, the sun does not fall from the sky, but directly appears in a random block. At this moment, I still don't know if two sets of sunlight can be in the same block at the same time. In this example, they can, but if this is not allowed in the original version of Plants vs. Zombies, please let me know.

When a ray of sunshine falls onto the ground, it can be picked up by players. A mouse event listener processes the entire task. The following code is used:

 
package {        import flash.display.Sprite;        import flash.utils.Timer;        import flash.events.TimerEvent;        import flash.events.MouseEvent;        public class Main extends Sprite {                private var gameField:Array;                private var flowersTimer:Timer=new Timer(5000);                private var sun:sunMc;                private var sunContainer:Sprite=new Sprite();                public function Main():void {                        setupField();                        drawField();                        fallingSuns();                }                private function fallingSuns():void {                        addChild(sunContainer);                        flowersTimer.start();                        flowersTimer.addEventListener(TimerEvent.TIMER, newSun);                }                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();                        sunContainer.addChild(sun);                        sun.x=52+sunRow*65;                        sun.y=130+sunRow*75;                        sun.addEventListener(MouseEvent.CLICK,sunClicked);                }                private function sunClicked(e:MouseEvent):void {                        e.currentTarget.removeEventListener(MouseEvent.CLICK,sunClicked);                        var sunToRemove:sunMc=e.currentTarget as sunMc;                        sunContainer.removeChild(sunToRemove);                }                private function setupField():void {                        gameField=new Array();                        for (var i:uint=0; i<5; i++) {                                gameField[i]=new Array();                                for (var j:uint=0; j<9; j++) {                                        gameField[i][j]=0;                                }                        }                }                private function drawField():void {                        var fieldSprite:Sprite=new Sprite();                        var randomGreen:Number;                        addChild(fieldSprite);                        fieldSprite.graphics.lineStyle(1,0xFFFFFF);                        for (var i:uint=0; i<5; i++) {                                for (var j:uint=0; j<9; j++) {                                        randomGreen=(125+Math.floor(Math.random()*50))*256;                                        fieldSprite.graphics.beginFill(randomGreen);                                        fieldSprite.graphics.drawRect(25+65*j,80+75*i,65,75);                                }                        }                }        }}

The following describes the functions used:
Drawfield: Draw the game Area
Fallingsuns: start to make sunshine and let them fall
Newsun: Create a New sunshine
Setupfield: Generate block array
Sunclicked: called when a player clicks the sun
The following figure shows the effect:



Use the mouse to pick up the sun.

Flash game development practices (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.