[As3 Code] A complete game framework

Source: Internet
Author: User
Package
{
Import Flash. display. Sprite;
Import Flash. Events. event;
Import Flash. Events. mouseevent;

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 Clicks: Int = 0; // Game clicks

Public Function Main (): void
{
Init ();
}
Private Function Init (): void
{
This. addeventlistener (event. enter_frame, gameloop );
Gamestate = state_init;
}

Public Function Gameloop (E: Event): void
{
Switch (Gamestate)
{
Case State_init:
Initgame ();
Break ;
Case State_play:
Playgame ();
Break ;
Case State_game_over:
Gameover ();
Break ;
}
}

Public Function Initgame (): void
{
// Initialize the game. When the button is clicked, the number of clicks returns to zero and the status changes to Game start.
Stage. addeventlistener (mouseevent. Click, onmouseclickevent );
Clicks = 0;
Gamestate = state_play;
}
Public Function Playgame (): void
{
// If the game is clicked 10 times, the status changes to the game end.
If (Clicks> = 10)
{
Gamestate = state_game_over;
}
}
Public Function Onmouseclickevent (E: mouseevent): void
{
Clicks ++; // Game clicks + 1
Trace ("click" + clicks + "Times game! ");
}
Public Function Gameover (): void
{
// Cancel Game listener
Stage. removeeventlistener (mouseevent. Click, onmouseclickevent );
Gamestate = state_init;
Trace ("the game is over! ");
}
}
}

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.