Professional Documents
Culture Documents
Flash
Multimedia:
Game
Design
Kelly Short 1
Flash 8 Multimedia: Game Design Sydney CEO
Contents
Section Page
5 Responding to a hitTest 12
6 Adding a score 14
7 Shooting 15
8 Adding sound to movement 17
Kelly Short 2
Flash 8 Multimedia: Game Design Sydney CEO
Screen Layout
Kelly Short 3
Flash 8 Multimedia: Game Design Sydney CEO
Library
Timeline
Properties Bar
Stage
Toolbar
Kelly Short 4
Flash 8 Multimedia: Game Design Sydney CEO
The Timeline
Empty Key Frame
Key Frame
Empty Frame
Layer Name
Onion Skinning
The Tools
Select (V) Sub Select (A)
Stroke Colour
Fill Colour
Black and White, No colour, Swap Colours
Kelly Short 5
Flash 8 Multimedia: Game Design Sydney CEO
Delete the
scene
Duplicate the
current scene Add a new
scene
Kelly Short 6
Flash 8 Multimedia: Game Design Sydney CEO
Add an action
script statement
Kelly Short 7
Flash 8 Multimedia: Game Design Sydney CEO
Which Key
is Pressed?
Print Message to say Print Message to say Print Message to say Print Message to say
“Right” “Left” “Up” “Down”
End
Kelly Short 8
Flash 8 Multimedia: Game Design Sydney CEO
The following is the code to add the keylistener: (line numbers are for reference only)
Add this as a frame script
1. /* =======================Key Listener=============================*/
2. var myListener:Object = new Object();
3.
4. //=============================================on key release
5. myListener.onKeyUp = function () {
6. switch (Key.getCode()) {
7. case Key.RIGHT :
8.
9. break;
10. case Key.LEFT :
11.
12. break;
13. case Key.UP:
14.
15. break;
16. case Key.DOWN:
17.
18. break;
19. }//end switch
20.
21. }//end function
22.
23. Key.addListener(myListener);
At the moment though, this does not do anything aside from add a listener. It doesn’t actually respond to the
listener, because you haven’t programmed it to do anything.
/* =============Key Listener============================================*/
var myListener:Object = new Object();
//=============================================on key release
myListener.onKeyUp = function () {
switch (Key.getCode()) {
case Key.RIGHT :
trace ("RIGHT");
break;
case Key.LEFT :
trace ("Left");
break;
case Key.UP:
trace ("Up");
break;
case Key.DOWN:
trace ("Down");
break;
}//end switch
}//end function
Key.addListener(myListener);
Kelly Short 9
Flash 8 Multimedia: Game Design Sydney CEO
Of course, the trace function is simply there to see if your ball will actually move. In order
to make it move, you have to program it to. Add the bold sections to your code
/* =======================Key Listener==================================*/
var myListener:Object = new Object();
//=============================================on key release
myListener.onKeyUp = function () {
switch (Key.getCode()) {
case Key.RIGHT :
_root.ball1._x= _root.ball1._x + 10; //moves ball 10 right of
//current x position of ball
break;
case Key.LEFT :
_root.ball1._x= _root.ball1._x - 10;//moves ball 10 left of cur
//rent x position of ball
break;
case Key.UP:
_root.ball1._y= _root.ball1._y - 10;//moves ball 10 UP of cur
//rent y position of ball
trace ("Up");
break;
case Key.DOWN:
trace ("Down");
_root.ball1._y= _root.ball1._y + 10;//moves ball 10 Down of cur
//rent y position of ball
break;
}//end switch
}//end function
Key.addListener(myListener);
Kelly Short 10
Flash 8 Multimedia: Game Design Sydney CEO
Hit Test
http://www.kirupa.com/developer/actionscript/hittest.htm
Select the circle movie clip. Once the circle movie clip has been se-
lected, click the Instance tab to display the Instance panel. In the
Name field of the Instance panel, enter the word circle
Because it takes at least two objects to create a collision, you will need to name the second object: the square
movie clip. Select the blue square and give it the name: block. You would enter the word 'block' in the Instance
panel just like you did for the circle movie clip in Step 1
Once you named the block movie clip, it is time to add the actions. Right click on the circle movie clip and
select Actions. The Object Actions window will appear. Copy and paste the following code into that window:
onClipEvent (enterFrame) {
The code following If the two shapes
this statement will if (_root.circle, hitTest(_root.block)) {
are colliding
be enabled each
time the frame of _root.text = "Collision Detected";
the movie clip
loads. Basically, } else {
enterFrame, en-
ables the action to _root.text = "No Collision";
loop continuously.
}
Displays in the
text box on the
screen
Kelly Short 11
Flash 8 Multimedia: Game Design Sydney CEO
if (_root.circle, hitTest(_root.block)) {
} else {
• Explode
Exploding:
Double click on the circle. In the timeline, set a keyframe at Frame 2. This is where the explosion will start.
Place a stop script on frame 1. This way, the explosion will only start when the object gets sent to Frame 2
Set a stop();
script here.
Change this to a
spot, or differ-
This should be ent shape and
the same as the shape tween
first frame
Kelly Short 12
Flash 8 Multimedia: Game Design Sydney CEO
onClipEvent(enterFrame) {
if(this.hitTest(_root.square1)) {
_root.ball1.gotoAndPlay(6);
}
}
Kelly Short 13
Flash 8 Multimedia: Game Design Sydney CEO
Adding a score
Adding a score is very simple. First, create a dynamic text field to store the score and add the Var property
of score.
onClipEvent(enterFrame) {
if(this.hitTest(_root.square1)) {
_root.ball1.gotoAndPlay(2);
_root.score=_root.score+1;//make sure there
//are no spaces or it won’t
//work!
}
}
Also, on the frame script, declare and initialize the variable score by:
var score:Number = 0;
Kelly Short 14
Flash 8 Multimedia: Game Design Sydney CEO
Shooting
Kelly Short 15
Flash 8 Multimedia: Game Design Sydney CEO
Add a stop();
script here, as
most of the
time, you do
not want to Change the position of the bul-
shoot, only let to where the ball is
when key is
pressed.
If the space key
is pressed
Kelly Short 16
Flash 8 Multimedia: Game Design Sydney CEO
Select Export for ActionScript and Export in First Frame; then give the
sound the identifier Swish1
break;
Kelly Short 17
Flash 8 Multimedia: Game Design Sydney CEO
Create a button
to turn sound on
and off
on (release) {
if(SoundOn==1){
SoundOn=0;
} else {
SoundOn=1;
}
trace (SoundOn);
Kelly Short 18
Flash 8 Multimedia: Game Design Sydney CEO
Kelly Short 19
Flash 8 Multimedia: Game Design Sydney CEO
/* =======================Key Listener==================================*/
var myListener:Object = new Object();
//=============================================on key release
myListener.onKeyUp = function () {
switch (Key.getCode()) {
case Key.RIGHT :
_root.ball1._x= _root.ball1._x + 10; //moves ball 10 right of current
//x position of ball
break;
case Key.LEFT :
_root.ball1._x= _root.ball1._x - 10;//moves ball 10 left of current
//x position of ball
break;
case Key.UP:
_root.ball1._y= _root.ball1._y - 10;//moves ball 10 UP of current y
//position of ball
trace ("Up");
break;
case Key.DOWN:
trace ("Down");
_root.ball1._y= _root.ball1._y + 10;//moves ball 10 Down of current
//y position of ball
break;
case Key.SPACE:
ypos=getProperty(ball1,_y);
setProperty(shot1,_y,ypos);
shot1.gotoAndPlay("start");//throwshot
if(SoundOn==1)//if the sound is on
{
//add sound
var HitSound:Sound = new Sound();
HitSound.attachSound("Swish1");
HitSound.start();
break;
}
}//end switch
}//end function
Key.addListener(myListener);
Kelly Short 20
Flash 8 Multimedia: Game Design Sydney CEO
onClipEvent(enterFrame) {
if(this.hitTest(_root.square1)) {
_root.ball1.gotoAndPlay(2);
_root.score=_root.score+1;
}
}
on (release) {
if(SoundOn==1){
SoundOn=0;
} else {
SoundOn=1;
}
trace (SoundOn);
}
Kelly Short 21