[AS3 Code] Play a balloon game

Source: Internet
Author: User
Package
{
Import flash. display. MovieClip;
Import flash. display. Sprite;
Import flash. events. Event;
Import flash. events. MouseEvent;
Import flash. geom. Rectangle;
Import flash. media. Sound;
Import flash. text .*;


Public class Main extends Sprite
{
Public static const STATE_INIT: int = 10;
Public static const STATE_PLAY: int = 20;
Public static const STATE_GAME_OVER: int = 30;
Public var gameState: int = 0; // game status

Public var score: int = 0; // score of the player
Public var chances: int = 0; // number of missing balloons
Public var level: Number = 1; // level

Public var bg: MovieClip;
Public var enemies: Array; // Array for storing enemies (balloons)
Public var player: MovieClip; // reference the player's picture
Public var tempEnemy: MovieClip; // random balloon

Public var scoreLabel: TextField = new TextField (); // score title
Public var levelLabel: TextField = new TextField (); // level title
Public var chancesLabel: TextField = new TextField (); // the title of the missed net.
Public var scoreText: TextField = new TextField (); // minute Value
Public var levelText: TextField = new TextField (); // level value
Public var chancesText: TextField = new TextField (); // network leakage value
Public const SCOREBOARD_Y: Number = 380; // The Score Board is located at the bottom of the game.

Public function Main (): void
{
Init ();
}
Private function init (): void
{
This. addEventListener (Event. ENTER_FRAME, gameLoop );

// Display the background
Bg = new BackImage ();
This. addChild (bg );

// Set the title and default value of the score board
ScoreLabel. text = "score :";
ScoreText. text = "0 ";
LevelLabel. text = "grade :";
LevelText. text = "1 ";
ChancesLabel. text = "collision :";
ChancesText. text = "0 ";

// Place the game at the bottom
ScoreLabel. y = SCOREBOARD_Y;
LevelLabel. y = SCOREBOARD_Y;
ChancesLabel. y = SCOREBOARD_Y;
ScoreText. y = SCOREBOARD_Y;
LevelText. y = SCOREBOARD_Y;

ChancesText. y = SCOREBOARD_Y;

// Set the title and value horizontal coordinates
ScoreLabel. x = 5;
ScoreText. x = 50;
ChancesLabel. x = 105;
ChancesText. x = 155;
LevelLabel. x = 205;
LevelText. x = 260;

// Display the score board
This. addChild (scoreLabel );
This. addChild (levelLabel );
This. addChild (chancesLabel );
This. addChild (scoreText );
This. addChild (levelText );
This. addChild (chancesText );

GameState = STATE_INIT;
}

// Loop function for game status adjustment
Public function gameLoop (e: Event): void
{
Switch (gameState)
{
Case STATE_INIT:
InitGame ();
Break;

Case STATE_PLAY:
PlayGame ();
Break;

Case STATE_GAME_OVER:
EndGame ();
Break;
}
}

// Initialize the game
Public function initGame (): void
{
// Initialize the game
Score = 0; // score
Chances = 0; // Number of missed Networks
// Level = 1;

LevelText. text = level. toString ();

Player = new PlayerImage ();
This. addChild (player );
Player. startDrag (true, new Rectangle (0, 0, 550,400 ));

// Store balloons
Enemies = new Array ();

GameState = STATE_PLAY;
}

// Start the game
Public function playGame (): void
{
// Rotate the dart
Player. rotation + = 20;

// Create a balloon (enemy) Function
MakeEnemies ();

// Update and pray for Y coordinates and remove them when the screen is floated out
MoveEnemies ();

// Checks whether the player has collided with the enemy. If yes, an event is triggered.
TestCollisions ();

// Check whether the level can be increased or the game can end
TestForEnd ();
}

Public function makeEnemies (): void
{
// Obtain a random number between 0 and 99
Var chance: Number = Math. floor (Math. random () * 100 );

// The higher the level, the higher the probability of balloon appearance
If (chance <level + 2)
{
// Create a balloon
TempEnemy = new EnemyImage ();

// The higher the level, the faster the balloon is.
TempEnemy. speed = level + 3;

// Randomly fl the balloon of the corresponding color from 1 to 5 frames
Var xxxx = Math. floor (Math. random () * 5) + 1;
TempEnemy. gotoAndStop (xxxx );

// Coordinates of the Y axis of the balloon, starting from the bottom
TempEnemy. y = 435;

// Coordinates of the X axis of the balloon. The screen is 0-540, so the balloon (which has 30 widths) is randomly flushed from 0.
TempEnemy. x = Math. floor (Math. random () * 515 );

Var tt: TextField = new TextField ();
Var ttf: TextFormat = new TextFormat ();
Ttf. bold = true;
Ttf. align = "center ";
Ttf. size = 30;
Tt. text = xxxx;
Tt. setTextFormat (ttf );
TempEnemy. addChild (tt );

// Display the balloon to the scenario
This. addChild (tempEnemy );

// Add it to the balloon array for tracking
Enemies. push (tempEnemy );
}
}

// Facilitate the balloon array, update the Y coordinates of each balloon, and move the balloon up
Public function moveEnemies (): void
{
Var tempEnemy: MovieClip;
For (var I: int = enemies. length-1; I> = 0; I --)
{
TempEnemy = enemies [I];
TempEnemy. y-= tempEnemy. speed;

// If the balloon goes down to the top of the screen
If (tempEnemy. y <-35)
{
Chances ++; // Number of missed connections + 1
ChancesText. text = chances. toString ();
Enemies. splice (I, 1); // Delete the balloon from the balloon Array
RemoveChild (tempEnemy); // remove the balloon from the display list
}
}
}

// Darts and balloon Collision Detection
Public function testCollisions (): void
{
// Call the sound object
Var sound: Sound = new Pop ();
Var tempEnemy: MovieClip;

// Traverse the remaining balloons from the balloon Array
For (var I: int = enemies. length-1; I> = 0; I --)
{
TempEnemy = enemies [I];

// Check whether the current balloon is in conflict with the dart
If (tempEnemy. hitTestObject (player ))
{
Score ++;
ScoreText. text = score. toString ();
Sound. play ();
Enemies. splice (I, 1); // Delete the balloon from the balloon Array
RemoveChild (tempEnemy); // remove the balloon from the display list
}
}
}

// Check whether the level can be increased or the game can end
Public function testForEnd (): void
{
If (chances> = 5)
{
GameState = STATE_GAME_OVER;
}
Else if (score> = level * 5)
{
Level ++;
LevelText. text = level. toString ();

Var level_up: TextField = new TextField ();
Level_up.text = "Congratulations! Upgraded to "+ level + !!! ";
This. addChild (level_up );
}
}

// End the game
Public function endGame (): void
{
// Remove balloons from the display list one by one
For (var I: int = 0; I <enemies. length; I ++)
{
RemoveChild (enemies [I]);
Enemies = [];
}

Player. stopDrag ();
}
}
}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.