Beruflich Dokumente
Kultur Dokumente
WWW.MYANMARTUTORIALS.COM ..
WWW.WYTUMYANMAR.COM
PROFESSIONAL FLASH GAME DEVELOPMENT Professional Flash Game Development Tutorials Tutorial Series Total Game Play Myanmar Tutorials Tutorials Myanmar Tutorials Myanmar Online Game Portal
Chapter 1
Flash Flash Macromedia Photoshop web multi-media content Adobe software company Flash web site web application Flash
Game Flash Flash game Flash code Flash Flash code Flash game developer Language Flash game development concept Flash game PC game Flash game PC game download run component player () Flash Player () Flash authoring tool Flash platform Flash platform Component () Internet C++ game game computer language game intuitive Built-in animation system Beginner Flash code
animation
Flash
reuse C++ game Flash Flash game ActionScript Flash authoring tool authoring tool artist PC game game
install
Flash Player Flash Player file web browser Flash Player plug-in Flash Player plug-in Web browser Flash file SWF Flash web browser Flash version update Internet 90% Flash Player Flash developers Flash internet platform plug-in
Flash platform
Flash Authoring Tools Flash Flash authoring tool Flash authoring tool library object vector graphic animation objects scene Flash game Flash authoring tool Adobe Adobe Flash CS 4 Flash authoring tool Adobe companies Flash authoring tool coding Adobe Flash CS 4 authoring tool SWF file
Flash
concept
ActionScript Flash authoring tool Flash languages ActionScript object logic language javascript javascript animation ActionScript support Object scripting language Flash open scripting javascript ActionScript
text editor Adobe Flash CS4 ActionScript text syntax highlight, auto-complete features IDE (integrated development ActionScript IDE VisualStudio FlashDevelop
Start-up page options Flash File (ActionScript 3.0) 2.0 ActionScript language support Flash file 2.0 object oriented flash file ActionScript version 3.0
files
interface ESSENTIALS
save
Flash CS
interface working area Game Workspace flash game workspace flash file asset animation asset Asset workspace Workspace flash file flash file export assets
Workspace: workspace game area Library Panel: Library Library Library Library
select
Toolbox: Adobe Photoshop Flash CS Toolbox Photoshop shortcut Artist toolbox workspace programmer toolbox tool Timeline: Timeline animation frame manage frame Timeline animation frame
Flash CS game
interface
graphic
File graphic
graphic Bitmap Flash coding control Symbol Symbol graphic Symbol Symbol workspace graphic select Convert to Symbol menu Convert to Symbol Dialog Box Name Enemy Type Movie Clip
Graphic
Modify ->
Convert to Symbol Dialog Box graphic Panel symbol Bitmap Instance name Symbol Option coding Instance name Workspace Instance name enemy1 Properties Instance name reference enemy1
ActionScript Panel ActionScript panel code enemy1.x = 100; enemy1.y = 100; enemy1 object properties properties graphic coordinate 100 Window run code graphic copy Instance name dot . OOP language x y enemy1 x y coordinate Graphic object default properties x y coordinate game Ctrl + ENTER game run workspace graphic coordinate (100, 100) code Frame run execute
Run
Animation
code
addEventListener( Event.ENTER_FRAME, DoGameLoop ); var nXDir : int = 1; var nYDir : int = 1; var nSpeed : int = 10; function DoGameLoop(e : Event) {
enemy1.x += nXDir * nSpeed; enemy1.y += nYDir * nSpeed; if ( enemy1.x > nXDir = -1; if ( enemy1.x < nXDir = 1; if ( enemy1.y > nYDir = -1; if ( enemy1.y < nYDir = 1; 610 ) 30 ) 450 ) 30 )
enemy1.scaleX = nXDir; }
default frame
function event listen function execute DoGameLoop DoGameLoop function DoGameLoop function enemy1 x y nSpeed code run graphic screen
graphic
code
addEventListener(Event.ENTER_FRAME, DoGameLoop); var nXDir : int = 1; var nYDir : int = 1; var nSpeed : int = 10; function DoGameLoop(e : Event) { enemy1.x += nXDir * nSpeed; enemy1.y += nYDir * nSpeed; if ( enemy1.x > nXDir = -1; if ( enemy1.x < nXDir = 1; if ( enemy1.y > nYDir = -1; if ( enemy1.y < nYDir = 1; 610 ) 30 ) 450 ) 30 )
enemy1.scaleX = nXDir; } addEventListener(MouseEvent.CLICK, DoClick); function DoClick(e : MouseEvent) { if ( enemy1.hitTestPoint( e.stageX, e.stageY ) ) removeEventListener(Event.ENTER_FRAME, DoGameLoop); }
removeEventListener(Event.ENTER_FRAME, DoGameLoop); } enemy1.hitTestPoint() function point hit-test MouseEvent properties stageX stageY DoGameLoop function event listener remove Point mouse coordinate hit-test graphic
game code
Ctrl+ENTER
Chapter 2
Flash game Macromedia version 3 ActionScript ActionScript scripting language Adobe upgrade ActionScript ActionScript 3 Object Oriented Programming OOP language C++/C#/Java tutorial ActionScript language programming concept language programming language
programming
ActionScript
variable
var i : Number = 0;
var
keyword
colon :
= 0 variable type
Variable Type int Number
function dot .
function
public function MyFunction( var parameter : int = 0 ) : int { return 0; }
Function public function function function parameter list Parameter function return variable type class
package { class MyClass extends MovieClip { // constructor public function MyClass() { super(); } } }
public variable
private
keyword
package
Java
package
C++
Constructor class function public function constructor Constructor function return value parent class constructor ActionScript parent class super keyword parent class member function foo
super()
super.foo();
ActionScript ActionScript
Java function
parent class
function
Parent class
class
import flash.utils.Timer;
flash.utils
class
import
import flash.utils.*;
FlashDevelop import
IDE
ActionScript Game Adobe Flash Flash programming ActionScript ActionScript graphic Adobe Flash CS Symbol Insert -> New Symbol New Symbol dialog box
ActionScript
bitmap Symbol
Create New Symbol Dialog Box Symbol symbol Edit Mode double-click (Right-click -> Edit )
Editing Symbol (Right Click -> Edit) Scene 1 Symbol scene Scene 1 Symbol edit graphic main scene Symbol text graphic Symbol animation frame animation frame ActionScript Symbol logic symbol symbol symbol symbol symbol level symbol character symbol Flash ActionScript Coding Manager window symbol class nested level level ActionScript link symbol class Flash symbol symbol graphic control symbol Animation Symbol nested complex character symbol symbol game screen main graphic Library edit symbol ( )
symbol animation
Frame game
code
context menu
New
New Class Dialog Box Filename TestSymbol Flash CS symbol link symbol Library panel Right click -> Properties Test Symbol class Adobe Flash CS4 select (Symbol symbol Symbol Properties dialog box class link )
Symbol Properties Dialog Box Linkage classes Export for ActionScript Class flash.display.MovieClip class file class link TestSymbol class MovieClip FlashDevelop extends MovieClip FlashDevelop import Linkage class Advance button TestSymbol Base icon
Symbol MovieClip
package { import flash.display.MovieClip; /** * ... * @author mkt */ public class Character extends MovieClip { public function Character() { } } }
New symbol
Graphic
Button
type
Type Movie Clip Extends Class MovieClip
MovieClip class
extend
move, rotate, scale functions MovieClip Graphic DisplayObject MovieClip control Symbol extend
function light-weighted
Button
SimpleButton
Flash
button
symbol screen
MovieClip Properties
Name x : Number y : Number scaleX : Number Description X Y Width scaleX = 0.5; scaleX = 2.0; scaleX = -1.0; scaleY : Number width : Number Height Graphic process .width height: Number alpha : Number Graphic Graphic width return graphic query height return transparency 0.5 visible : Boolean Symbol screen transparent 1.0 0.0 opaque width mirror scaleX TestSymbol.x = 10 X scaleX object Mirror x coordinate 10
visible = false;
symbol
visible = true; currentFrame : int parent : DisplayObject Play head Animation symbol symbol symbol root : DisplayObject numChildren : int parent object symbol symbols symbol
Properties
MovieClip class
Methods
MovieClip Methods
Name addChild(child : DisplayObject) Description Symbol Flash time addEventListener(type:String, listener:Function) getChildAt(index:int) getChildByName(name:String) Event mouse-click listen Symbol Symbol function symbol frame animation play play frame listen user input child symbol retrieve child symbol child symbol frame remove frame retrieve symbol dynamically run time programming symbol mouse-click event symbol design symbol edit
removeChild(child:DisplayObject) Symbol nextFrame() prevFrame() stop() play() gotoAndPlay(frame:Object) Play-head Play-head Animation Animation
function
methods MovieClip
Pong
intro screen Create New -> Flash File (ActionScript 3.0) Filename Pong FlashDevelop (Project -> New Project) ActionScript 3 Flash IDE Project
file New
Project
Pong
Main class Main class C++ main() function program run entry point Flash Main class main scene control MovieClip object game Movie Clip symbol Main class main scene select Main scene select select select Escape key select main scene Properties Panel Class Pong
Pong
class game
graphic
ball
paddle Flash CS
Insert -> New Symbol ActionScript on Class Ball OK GfxBall symbol Edit Mode Oval tool Horizontal Center Vertical Center
Paddle New Symbol GfxPaddleLeft Class name PaddleLeft Base class Paddle Base class MovieClip extend class Paddle Left Right Paddle Paddle Paddle parent class extend OOP OK FlashDevelop Paddle class Paddle class MovieClip extends OK GfxPaddleLeft Edit Mode rectangle Tool box Rectangle Tool Rectangle scene ( Alignment commands )
paddle PaddleRight
Ball extends
class code
FlashDevelop
add
public function OnFrameMove(e : Event ) { x += m_nDirX * m_nSpeed; y += m_nDirY * m_nSpeed; if ( y < 15 || y > 465 ) m_nDirY *= -1; if ( x < 15 || x > 625 ) { x = 320; y = 240; m_nDirX *= -1; } } } }
addEventListener(Event.ENTER_FRAME, function frame ( ) code ball direction direction Direction -1 opposite direction x Paddle class class Paddle.as
package { import flash.display.MovieClip; import flash.events.Event;
/** * ... * @author mkt */ public class Paddle extends MovieClip { public var m_lpBall : Ball; public var m_nMoveY : int; public var m_nSpeed : int = 6; public function Paddle() { addEventListener(Event.ENTER_FRAME, OnFrameMove); } public function OnFrameMove(e : Event) { if (hitTestObject(m_lpBall)) m_lpBall.m_nDirX *= -1; y += m_nMoveY * m_nSpeed; if ( y > 480 ) y = 480;
if ( y < 0 ) y = 0; } } }
ball
hitTestObject() function paddle Y direction player key value child class value User input PaddleRight class child classes
x direction
package { import flash.events.KeyboardEvent; /** * ... * @author mkt */ public class PaddleLeft extends Paddle { public function PaddleLeft() { stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown); stage.addEventListener(KeyboardEvent.KEY_UP, OnKeyUp); } public function OnKeyDown(e : KeyboardEvent) { if (e.keyCode == 87) // W key m_nMoveY = -1; if (e.keyCode == 83) // S key m_nMoveY = 1; } public function OnKeyUp(e : KeyboardEvent) { m_nMoveY = 0; } } }
PaddleRight.as
package { import flash.events.KeyboardEvent; /** * ... * @author mkt */ public class PaddleRight extends Paddle {
public function PaddleRight() { stage.addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown); stage.addEventListener(KeyboardEvent.KEY_UP, OnKeyUp); } public function OnKeyDown(e : KeyboardEvent) { if (e.keyCode == 38) // Up key m_nMoveY = -1; if (e.keyCode == 40) // Down key m_nMoveY = 1; } public function OnKeyUp(e : KeyboardEvent) { m_nMoveY = 0; } } }
addEventListener(KeyboardEvent.KEY_DOWN, OnKeyDown) key input Listen function listener stage add key input Key input stage listen key input listen stage. addEventListener() listen code key press key code 87: W key 83 : S key 38 : Up arrow key 40 : Down arrow key left paddle W S right paddle up down arrow
select
ball paddle classes instance name Instance name ball paddleRight Pong class
Screen
ball paddleLeft
Pong.as
public class Pong extends MovieClip { public function Pong() { var lpPaddleLeft : PaddleLeft = PaddleLeft(getChildByName("paddleLeft")); var lpPaddleRight : PaddleRight = PaddleRight(getChildByName("paddleRight")); var lpBall : Ball = Ball(getChildByName("ball")); lpPaddleLeft.m_lpBall = lpBall; lpPaddleRight.m_lpBall = lpBall; } }
game paddle
run Ball
run
coding score
Chapter 3
Using Timeline and Frame Chapter Timeline Symbol Frame Edit Mode (symbol double-click ) Timeline Symbol chapter Flash game symbol
feature
Timeline in Symbol Edit Mode Timeline 1 Frame frame frames Frame Play-head frame Flash Frame Game run Play-head frame frame Play-head frames rate Timeline Flash game ActionScript ActionScript code ActionScript code run
rate
Adobe Toolbox
Flash
CS Symbol
Flash
frame
Oval Tool frame animation Right-click -> Insert Keyframe frame select Selection Tool ) Frame frame frame
Timeline Timeline
Using Frames to animate ball bouncing frame area Key frame frame Keyframe animation rotation Animation Right-click -> Actions frame play-head run play-head
key frame position Keyframe position run run ActionScript panel frame
Using Tweening
frame
frame stop()
run
frame
frame Right-Click -> Remove Frames Frame 10 select Frame Keyframe Frame 1 select Right-click -> Create Classic Tween
frame Frame
Shift key
select
smooth
frame 20 40 Right-click -> Insert normal frames Frame 10 normal frame select
Creating a Tween
frame
Right-click -> Insert Keyframe frame frame select Right-click -> Create Classic Tween keyframe Example Game Using Frames
game
frame graphic
game
graphic
game
Project save
Flash CS FlashDevelop
Memory class Flash CS Stage class Memory (skeleton chapter 2 ) symbol Symbol frame GfxCard graphic frame Symbol Library Panel Frame 5
New Document Memory Memory New Project MovieClip extend game skeleton game game
Insert -> Keyframe 5% Transformation panel Ctrl-T Transformation panel frame 10 Key frame Frame 10
Tween
Animation
Enter
code
5%
code
frame
frame close Keyframe frame Keyframe Right-click -> Convert to Keyframe open animation frame 10 frame 10 stop() ActionScript code Frame 11 close0 frame 16 Convert to Keyframe open0 open0 clode0 code integer Frame 20 stop() code frame 5 frame ActionScript panel complex code maintain MovieClip extend open0 animation class frame ActionScript panel FlashDevelop Card class GfxCard simple Card link code class
code
Card class
public class Card extends MovieClip { public var m_nFace : int;
public function Card() { m_nFace = 0; } public function set Face(f : int) { m_nFace = f; } // open the card to current face public function OpenCard() { // move to the "face" animation frame gotoAndPlay( "open" + m_nFace ); } }
gotoAndPlay()
m_nFace 0 open1, open2 Flash CS frame 5 select OpenCard() frame 5 Card class OpenCard() OpenCard() function frame Card class frame code member function member variables card click even Card class
animation frame open0 code ActionScript panel function GfxCard symbol link class
constructor
public function Card() { addEventListener( MouseEvent.CLICK, OnClick, false, 0, true ); mouseEnabled = true; buttonMode = true; useHandCursor = true; }
addEventListener() function
card click OnClick() function symbol (Button symbol ) mouse event mouseEnabled = true; mouse input event buttonMode useHandCursor properties mouse cursor card cursor OnClick() function
// on click card public function OnClick(e : MouseEvent) { Memory(root).OpenCard( this ); }
delegate
public var m_CurCard2 : Card; public var m_nCardIndex; // Open the card when the card is clicked public function OpenCard(c : Card) { m_nCardIndex++; if ( m_nCardIndex == 1 ) { m_CurCard1 = c; // open up the card m_CurCard1.gotoAndPlay("close"); } else if ( m_nCardIndex == 2 ) { m_CurCard2 = c; // open up the card m_CurCard2.gotoAndPlay("close"); } } ..... ..... }
Card
Memory class
// Close the card public function CloseCard(c : Card) { if ( m_nCardIndex > 1 ) { // check if card faces r shown if ( m_CurCard1.m_nFace == m_CurCard2.m_nFace ) { m_CurCard1.SetShown(); m_CurCard2.SetShown(); } else { // close both card back m_CurCard1.gotoAndPlay("close" + m_CurCard1.m_nFace); m_CurCard2.gotoAndPlay("close" + m_CurCard2.m_nFace); } // reset card count m_nCardIndex = 0; } }
card
SetShown() function
} // set the card as shown public function SetShown() { m_bShown = true; mouseEnabled = false; buttonMode = false; useHandCursor = false; } ................ }
public class Card extends MovieClip { .............. .............. // close the card face public function CloseCard() { stop(); Memory(root).CloseCard( this ); } }
CloseCard()
function
ActionScript
Workspace
Drop a few GfxCards into Workspace is all you need to build a game level
Flash level
Add more graphic in the similar way Tween close1, close2, close3 frame open1, open2, open3, ActionScript
card
m_nFace value
randomize
Randomize
Memory class
public class Memory extends MovieClip { public static const CARD_COUNT = 6; public var m_ItemList : Array; // start a new level public function StartLevel() { m_nCardIndex = 0; m_ItemList = new Array(); // retrieve items from the scene var i : int; var c : Card; var nFace : int = 0; var nSet : int = 0; for ( i = 0; i < numChildren; i++ ) { if ( getChildAt(i) is Card ) m_ItemList.push( getChildAt( i ) ); } // randomize var index1 : int; var index2 : int; for ( i = 0; i < 1000; i++ ) { index1 = Math.random() * m_ItemList.length; index2 = Math.random() * m_ItemList.length; // shuffle
c = m_ItemList[index1]; m_ItemList[index1] = m_ItemList[index2]; m_ItemList[index2] = c; } for ( i = 0; i < m_ItemList.length; i++ ) { c = Card( m_ItemList[i] ); // randomize the card face if ( nSet == 0 ) { nFace++; if ( nFace >= CARD_COUNT ) nFace = 0; } nSet++; if ( nSet == 2 ) nSet = 0; c. m_nFace = nFace; // set the close face c.gotoAndPlay("open"); } } .................... .................... }
StartLevel() function game run Workspace Actions ActionScript panel Memory game Memory.as
package { import flash.display.MovieClip;
Workspace timeline workspace play-head frame 1 (Edit mode ) frame 1 Right-click StartLevel() game run code
/** * ... * @author mkt */ public class Memory extends MovieClip { public static const CARD_COUNT = 6; public public public public var var var var m_ItemList : Array; m_CurCard1 : Card; m_CurCard2 : Card; m_nCardIndex;
public function Memory() { } // start a new level public function StartLevel() { m_nCardIndex = 0;
m_ItemList = new Array(); // retrieve items from the scene var i : int; var c : Card; var nFace : int = 0; var nSet : int = 0; for ( i = 0; i < numChildren; i++ ) { if ( getChildAt(i) is Card ) m_ItemList.push( getChildAt( i ) ); } // randomize var index1 : int; var index2 : int; for ( i = 0; i < 1000; i++ ) { index1 = Math.random() * m_ItemList.length; index2 = Math.random() * m_ItemList.length; // shuffle c = m_ItemList[index1]; m_ItemList[index1] = m_ItemList[index2]; m_ItemList[index2] = c; } for ( i = 0; i < m_ItemList.length; i++ ) { c = Card( m_ItemList[i] ); // randomize the card face if ( nSet == 0 ) { nFace++; if ( nFace >= CARD_COUNT ) nFace = 0; } nSet++; if ( nSet == 2 ) nSet = 0; c.m_nFace = nFace; // set the close face c.gotoAndPlay("open"); } } // Open the card when the card is clicked public function OpenCard(c : Card) { m_nCardIndex++; if ( m_nCardIndex == 1 ) { m_CurCard1 = c; // open up the card m_CurCard1.gotoAndPlay("close"); } else if ( m_nCardIndex == 2 ) { m_CurCard2 = c; // open up the card m_CurCard2.gotoAndPlay("close"); } }
// Close the card public function CloseCard(c : Card) { if ( m_nCardIndex > 1 ) { // check if card faces r shown if ( m_CurCard1.m_nFace == m_CurCard2.m_nFace ) { m_CurCard1.SetShown(); m_CurCard2.SetShown(); } else { // close both card back m_CurCard1.gotoAndPlay("close" + m_CurCard1.m_nFace); m_CurCard2.gotoAndPlay("close" + m_CurCard2.m_nFace); } // reset card count m_nCardIndex = 0; } } } }
Card.as
package { import flash.display.MovieClip; import flash.events.MouseEvent; /** * ... * @author mkt */ public class Card extends MovieClip { public var m_nFace : int; public var m_bShown : Boolean; public function Card() { mouseEnabled = true; buttonMode = true; useHandCursor = true; m_bShown = false; addEventListener( MouseEvent.CLICK, OnClick, false, 0, true ); } // set the card as shown public function SetShown() { m_bShown = true; mouseEnabled = false; buttonMode = false; useHandCursor = false; } // open the card to current face public function OpenCard()
{ // move to the "face" animation frame gotoAndPlay( "open" + m_nFace ); } // close the card face public function CloseCard() { stop(); Memory(root).CloseCard( this ); } // on click card public function OnClick(e : MouseEvent) { if ( !m_bShown ) Memory(root).OpenCard( this ); } } }
Flash
code
level
animation
code
frame
Chapter 4
Building a Game UI Game Interface) Symbol game game navigate character graphic game Flash UI game UI (User
Button Symbol Flash Insert -> New Symbol Create New Symbol Dialogbox Type MovieClip Button Symbol ( ) Symbol Button
Frame Set for Button Symbol Button Symbol Frame Timeline Up, Over, Down Hit frame Up frame graphic button up ( ) Over mouse button graphic Down mouse button Graphic button graphic bitmap Flash Tool Text object MovielClip button Flash
Windows Bitmap
button save
Windows Flash
ActionScript code
function
button
ClickMe
// attach event listener to button var button : SimpleButton = getChildByName("ClickMe"); button.addEventListener(MouseEvent.CLICK, OnClick); // Button click event function public function OnClick(e : MouseEvent) { trace("You click on a button"); }
button
Text Game object UI button Text Button Symbol Flash Toolbar game Text tool Text
Text Tool
Workspace text object Text object Text Tool select text object select Properties Panel text object properties property text Static Text Dynamic Text Input Text Static Text value game version game Static Text
game Highscores Static Text Dynamic Text Static Text Input Text
Font Family text bold, italic run system configuration font generic Properties Panel Font Family drop down list font _sans _serif _typewrite generic font
Sans
font
font
generic Font
font Flash embedded computer context menu button context menu Font Symbol Properties dialog box
Font Properties dialog box Dialog box font Linkage Class ActionScript Embedded font ActionScript checkbox on font family drop down list text OK embedded font font Class Export for FntArial
text
Text object Dynamic Text ( ) Input Text run-time ActionScript text text object instance name HighScores HighScores code
// set new highscores var scores : int = 1000; var txtHighscores : TextField = getChildByName("HighScores"); txtHighscores.text = "Player Highscores: " + scores;
Components
Button menu
text
Component Panel
Component Panels Components Panel Windows controls program language (VB, C#) Flash Components component control Components Panel Workspace CheckBox control Properties Component Inspector Component Inspector Window -> Component Inspector Component Inspector Window
control
Control Symbol
Editing Checkbox Control CheckBox control state double-click symbol nested symbol double-click edit mode control graphic VB C# programming language graphic UI Menu Game UI screen screen menu Menu menu game menu symbol main CheckBox symbol selected_up Flash control
Chapter 3
Menu Game Main Menu flash main scene frame in-game menu Scores Lives symbol game run frame
menu
menu Chapter 3 drag save Main Menu frame #2 memory.fla Flash file frame #1
frame #1
mouse
Center
Start Game button GfxStartGame Button Symbol Text object Font Family Arial Style Bold Alignment Size 32 (background ) Text Static Text ( text run-time text Static Text ) Filter Filter Photoshop Layer Style symbol shadow gradient glow effect Text click select Glow Properties Panel Color Filters Blur X Blur Y Add Filter 5
Add Glow Filter to START GAME Text Over Frame Right-click -> Insert Keyframe Text object Size 34 Blur X Blur Y 7 Blur Color Down frame Insert Keyframe frame Blur X Blur Y 3 Blur Color ( ) frame text object Frame Size 30
button Start Game button frame #1 Game frame #1 code right-click -> Actions ActionScript panel stop(); ( game logo credit text )
frame #1 filter
Memory Game Main Menu Screen Start Game function screen frame button ActionScript code Game screen frame #2 ActionScript code addEventListener() Start Game game gotoAndPlay() function
// attach event listener to button var button : SimpleButton = SimpleButton(getChildByName("butStartGame")); button.addEventListener(MouseEvent.CLICK, OnStartGame); // Button click event function function OnStartGame(e : MouseEvent) { gotoAndPlay(1); } stop();
Code frame #2
StartLevel(); stop();
butStartGame (chapter 3
Main Menu
Game
Ctrl-ENTER
Building Levels Main Menu Memory game card Insert Blank Frame frame level code level Level
frame #3 frame#3
Library frame #2
GfxCard code
ActionScript panel
StartLevel(); stop();
// check whether all cards are opened public function WinGame() : Boolean { var i : int; var card : Card; for (i = 0; i < m_ItemList.length; i++) { card = m_ItemList[i]; if (!card.m_bShown) return false; } return true; }
CloseCard() function
// Close the card public function CloseCard(c : Card) { if ( m_nCardIndex > 1 ) { // check if card faces r shown if ( m_CurCard1.m_nFace == m_CurCard2.m_nFace ) { m_CurCard1.SetShown(); m_CurCard2.SetShown(); // check if the game is won and go to next level if (WinGame()) { if (currentFrame == totalFrames) gotoAndPlay(0); else nextFrame(); } } else {
// close both card back m_CurCard1.gotoAndPlay("close" + m_CurCard1.m_nFace); m_CurCard2.gotoAndPlay("close" + m_CurCard2.m_nFace); } // reset card count m_nCardIndex = 0; }
WinGame() function frame ( (currentFrame == totalFrames) frame level frame level code frame
true level
level Flash
frames
Memory.as package { import flash.display.MovieClip; import flash.events.MouseEvent; import flash.display.SimpleButton; /** * ... * @author mkt */ public class Memory extends MovieClip { public static const CARD_COUNT = 6; public public public public var var var var m_ItemList : Array; m_CurCard1 : Card; m_CurCard2 : Card; m_nCardIndex;
public function Memory() { } // start a new level public function StartLevel() { m_nCardIndex = 0; m_ItemList = new Array(); // retrieve items from the scene var i : int; var c : Card; var nFace : int = 0; var nSet : int = 0; for ( i = 0; i < numChildren; i++ ) { if ( getChildAt(i) is Card ) { Card(getChildAt(i)).Initialize(); m_ItemList.push( getChildAt( i ) ); } }
// randomize var index1 : int; var index2 : int; for ( i = 0; i < 1000; i++ ) { index1 = Math.random() * m_ItemList.length; index2 = Math.random() * m_ItemList.length; // shuffle c = m_ItemList[index1]; m_ItemList[index1] = m_ItemList[index2]; m_ItemList[index2] = c; } for ( i = 0; i < m_ItemList.length; i++ ) { c = Card( m_ItemList[i] ); // randomize the card face if ( nSet == 0 ) { nFace++; if ( nFace >= CARD_COUNT ) nFace = 0; } nSet++; if ( nSet == 2 ) nSet = 0; c.m_nFace = nFace; // set the close face c.gotoAndPlay("open"); } } // Open the card when the card is clicked public function OpenCard(c : Card) { m_nCardIndex++; if ( m_nCardIndex == 1 ) { m_CurCard1 = c; // open up the card m_CurCard1.gotoAndPlay("close"); } else if ( m_nCardIndex == 2 ) { m_CurCard2 = c; // open up the card m_CurCard2.gotoAndPlay("close"); } } // Close the card public function CloseCard(c : Card) { if ( m_nCardIndex > 1 ) { // check if card faces r shown if ( m_CurCard1.m_nFace == m_CurCard2.m_nFace ) { m_CurCard1.SetShown(); m_CurCard2.SetShown();
// check if the game is won and go to next level if (WinGame()) { if (currentFrame == totalFrames) gotoAndPlay(0); else nextFrame(); } } else { // close both card back m_CurCard1.gotoAndPlay("close" + m_CurCard1.m_nFace); m_CurCard2.gotoAndPlay("close" + m_CurCard2.m_nFace); } // reset card count m_nCardIndex = 0; } } // check whether all cards are opened public function WinGame() : Boolean { var i : int; var card : Card; for (i = 0; i < m_ItemList.length; i++) { card = m_ItemList[i]; if (!card.m_bShown) return false; } return true; } } }
Card.as
package { import flash.display.MovieClip; import flash.events.MouseEvent; /** * ... * @author mkt */ public class Card extends MovieClip { public var m_nFace : int; public var m_bShown : Boolean; public function Card() { } // Initialize the card public function Initialize() { mouseEnabled = true;
buttonMode = true; useHandCursor = true; m_bShown = false; addEventListener( MouseEvent.CLICK, OnClick, false, 0, true ); } // set the card as shown public function SetShown() { m_bShown = true; mouseEnabled = false; buttonMode = false; useHandCursor = false; } // open the card to current face public function OpenCard() { // move to the "face" animation frame gotoAndPlay( "open" + m_nFace ); } // close the card face public function CloseCard() { stop(); Memory(root).CloseCard( this ); } // on click card public function OnClick(e : MouseEvent) { if ( !m_bShown ) Memory(root).OpenCard( this ); } } }
END