"The games of a people reveal a great deal about them.“
Marshall McLuhan
Intro to Action Script 11
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
The player controls an object or a character that moves left-right
Other object fall down
The player must either catch or avoid falling objects
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
The fox moves along the bottom of the stage left –right controlled by
the user through left and right arrow keys on the keyboard
Fox moves as long as the arrow key is pressed
Fox stops automatically before going beyond the left and right sides of
the screen
Apples fall down from the tree at random places and random times
But not too quickly
The apples start falling slowly and then fall faster and faster as the
game progresses
The fox has to catch apples
Each successfully catch apple increments
the player score shown in the score text field
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
Open applecatch.fla flash file.
Pay attention to the 3 frames.
1 frame – “start”,
2 frame – play,
3 frame – “game over”
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
1 frame – “start”
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
2 frame – play
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
3 frame – “game over”
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
“running fox” movie clip in the library which contains animation
1 frame “stand”
2-6 frames “run”
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
“running fox”
movie clip exported for Action Script as “running fox”
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
“apple movie clip exported for Action Script as “apple”
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
attachMovie is used to make apples
timeSinceLastApple counter starts with 0 for each new apple
incremented once per frame
then counter becomes 20, another apple is
created
this assures the time between the apples
(20 frames)
appleSpeed
variable to control falling speed of apples
after each new apple it is increased
the apples fall faster and faster
Each apple movie clip is removed then fox catches it or it hits the
bottom of the stage
After certain number of dropped apples the game ends and the score is
displayed
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
“Actions” movie clip contains the following script that calls
Function initGame() once the actions movie clip is loaded
Calls functions moveFox(), dropNewApple(), moveApples()
every frame.
onClipEvent (load) {
_root.initGame();
}
onClipEvent (enterFrame) {
_root.moveFox();
_root.dropNewApple();
_root.moveApples();
}
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
Second frame (play frame) script. The functions are declared in the
main Timeline
stop();
function initGame() {
// sets the variables to start the game and creates the fox
movie clip
// the range of falling apple clips
firstApple = 1;
lastApple = 0;
// init the score
score = 0;
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
// set the number of apples to fall
totalApples = 20;
// init the speed and time delay
timeSinceLastApple = 0;
appleSpeed = 5;
}
// create the fox so that it is on top of the apples
attachMovie( "running fox", "fox", 999999 );
fox._x = 275;
fox._y = 300;
Because the fox movie clip is created through script as the movie is built
it appears on top of the apples the assigned level to fox movie clip
is 999 999. All apples start at level 1 so they are behind the fox.
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
Key.isDown(Key.RIGHT))
checks if the right arrow key is down
Key.isDown(Key.LEFT))
checks if the left arrow key is down
dx
variable set to 10 / -10 to indicate how
much and in what direction fox should
move
_xscale
negative or positive value flip fox movie
clips horizontally so that fox is facing
the direction of the movement
Math.abs (x)
computes and returns an absolute
value for the number specified by the
parameter x .
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
function moveFox() {
// check for arrow keys
if (Key.isDown(Key.RIGHT)) {
dx = 10;
// fox faces right
fox._xscale = -Math.abs(fox._xscale);
} else if (Key.isDown(Key.LEFT)) {
dx = -10;
// fox faces left
fox._xscale = Math.abs(fox._xscale);
} else {
// no movement
dx = 0;
}
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
// move the fox for a value of dx
//and limit that movement
fox._x += dx;
if (fox._x < 30) fox._x = 30;
if (fox._x > 520) fox._x = 520;
}
// make the fox run or stand still
if ((dx != 0) and (fox._currentFrame == 1)) {
fox.gotoAndPlay("run");
} else if ((dx == 0) and (fox._currentFrame != 1)) {
fox.gotoAndPlay("stand");
}
If there is a movement going on and fox is standing still on the first
frame then clip sent to 2 frame “run”.
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
function dropNewApple() {
// drop only if it has been long enough
if (timeSinceLastApple > 20) {
// drop only if there are more apples
if (lastApple < totalApples) {
// drop only 10% of the time
//prevents apples to be dropped with at exact time intervals
if (Math.random() < .1) {
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
// create next apple and set its locations
lastApple++;
attachMovie( "apple", "apple"+lastApple, lastApple );
//horizontal location of each new apple between 30 and 520
_root["apple"+lastApple]._x = Math.random()*490+30;
_root["apple"+lastApple]._y = 0;
// reset time delay for next apple
timeSinceLastApple = 0;
// increase apple speed
if (appleSpeed < 10) appleSpeed += .5;
}
}
}
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
}
// even if no apple dropped, get closer to next drop
timeSinceLastApple++;
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
function moveApples() {
// loop through all existing apple clips
for (i=firstApple;i<=lastApple;i++) {
// get apple location
x = _root["apple"+i]._x;
y = _root["apple"+i]._y + appleSpeed;
// see whether apple reached ground
if (y > 400) {
removeApple(i);
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
// see whether apple hit basket
} else if ((Math.abs(y-fox._y) < 10) and (Math.abs(x-fox._x)
< 25)) {
removeApple(i);
score += 1;
}
}
// continue to move apple
} else {
_root["apple"+i]._y = y;
}
Math.abs used to determine collision between basket and apples
When an apple within 10 pixels of vertical and 25 pixels of horizontal
proximity, the collision is detected
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Catch games applecatch.fla
function removeApple(n) {
// take away apple movie clip (delete)
_root["apple"+n].removeMovieClip();
// reset range of apples to move
firstApple = n+1;
}
// see whether this was the last apple
if (n == totalApples) {
fox.removeMovieClip();
gotoAndPlay("game over");
}
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
Catch game with selection
add/remove power
add/remove score
add/remove objects in the game scene etc.
Modified appleCatch game
Each apple has a letter inside.
The user has to catch vowels and avoid consonants
The right apple with vowel increments user’s score
The wrong apple with a consonant decrements the score
the score should not go below 0
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
Catch game with selection
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
bad apple - consonants
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
good apple - vowels
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
Two apple movie clips- good (vowels) and bad (consonants)
“good apple”
5 frames each showing a vowel
“bad apple”
20 frames each showing a consonant
When it is time to drop there is a chance it is good apple or bad apple
The script displays a random frame from the appropriate movie clip
When the player catches the apple the script determines if its good or
bad one and adjusts the score
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
The “actions” movie clip contains the same script as in the applecatch
game:
onClipEvent (load) {
_root.initGame();
}
onClipEvent (enterFrame) {
_root.moveFox();
_root.dropNewApple();
_root.moveApples();
}
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
initGame() function is the same as in the applecatch game:
stop();
function initGame() {
// the range of falling apple clips
firstApple = 1;
lastApple = 0;
// init the score
score = 0;
// set the number of apples to fall now 50
totalApples = 50;
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
// init the speed and time delay
timeSinceLastApple = 0;
appleSpeed = 5;
}
// create the fox so that it is on top of the apples
attachMovie( "running fox", "fox", 999999 );
fox._x = 275;
fox._y = 300;
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
moveFox() function is the same as in the applecatch game:
function moveFox() {
// check for arrow keys
if (Key.isDown(Key.RIGHT)) {
dx = 10;
// fox faces right
fox._xscale = -Math.abs(fox._xscale);
} else if (Key.isDown(Key.LEFT)) {
dx = -10;
// fox faces left
fox._xscale = Math.abs(fox._xscale);
} else {
// no movement
dx = 0;
AD }
206 Intermediate CG : School of Art and Design : University of Illinois at Chicago :
Spring 2009
Avoid games applelettercatch.fla
// move the fox and limit that movement
fox._x += dx;
if (fox._x < 30) fox._x = 30;
if (fox._x > 520) fox._x = 520;
}
// make the fox run or stand still
if ((dx != 0) and (fox._currentFrame == 1)) {
fox.gotoAndPlay("run");
} else if ((dx == 0) and (fox._currentFrame != 1)) {
fox.gotoAndPlay("stand");
}
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
dropNewApple() function decides which type of apple to drop,
And then pick a random frame in the corresponding movie clip to
display.
function dropNewApple() {
// only drop if it has been long enough
if (timeSinceLastApple > 20) {
// only drop if there are more apples
if (lastApple < totalApples) {
// only drop 10% of the time
if (Math.random() < .1) {
// create next apple and set its locations
lastApple++;
if (Math.random() < .5) {
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
// 50% chance of a bad apple
attachMovie( "bad apple", "apple"+lastApple, lastApple );
"bad";
_root["apple"+lastApple].type =
} else {
// 50% chance of a good apple
attachMovie( "good apple", "apple"+lastApple, lastApple );
_root["apple"+lastApple].type =
"good";
}
f=
int(Math.Random()*_root["apple"+lastApple]._totalFrame
s) + 1;
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
_root["apple"+lastApple].gotoAndStop(f);
_root["apple"+lastApple]._x = Math.random()*490+30;
_root["apple"+lastApple]._y = 0;
// reset time delay for next apple
timeSinceLastApple = 0;
// increase apple speed
if (appleSpeed < 10) appleSpeed += .5;
}
}
}
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
}
// even if no apple dropped, get closer to next drop
timeSinceLastApple++;
function moveApples() {
// loop through all existing apple clips
for (i=firstApple;i<=lastApple;i++) {
// get apple location
x = _root["apple"+i]._x;
y = _root["apple"+i]._y + appleSpeed;
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
// see whether apple reached ground
if (y > 400) {
removeApple(i);
// see whether apple hit basket
} else if ((Math.abs(y-fox._y) < 10) and (Math.abs(x-fox._x)
< 25)) {
if (_root["apple"+i].type == "good") {
// good apple, get a point
//differentiates “good” and “bad” catches
score += 1;
} else {
// bad apple, lose a point
score -= 1;
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
// don't go below 0
if (score < 0) score = 0;
}
removeApple(i);
}
}
// continue to move apple
} else {
_root["apple"+i]._y = y;
}
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
Avoid games applelettercatch.fla
function removeApple(n) {
// take away apple movie clip
_root["apple"+n].removeMovieClip();
// reset range of apples to move
firstApple = n+1;
}
// see whether this was the last apple
if (n == totalApples) {
fox.removeMovieClip();
gotoAndPlay("game over");
}
AD 206 Intermediate CG : School of Art and Design : University of Illinois at Chicago : Spring 2009
© Copyright 2026 Paperzz