Programming games
Another sound example
General review
Homework: [finish project], look over
study guide
• Giving your user a way to download a file
• I did this by adding on to the playing sound
file example. IT CAN BE ANY FILE!
• Need to test this on the server, but you
can look at the source code (the .fla file)
• I did NOT put in code to monitor the
process and report back errors!!!
from the code
var downloadURL:URLRequest;
var fileName:String = lastplayed;
var file:FileReference;
downloadURL = new URLRequest();
downloadURL.url = lastplayed;
file = new FileReference();, fileName);
• There IS jargon in computing. It pays to
learn it.
• The jargon is [mostly] English words with
somewhat more specific meaning.
• Examples:
– Event & event handling
– Function
– Variable
– Instance
• Much of jargon is common to other
computer languages
• You have seen similarities in syntax
between JavaScript and ActionScript
• Note: ActionScript 3.0 actually is MORE
like Java than JavaScript
– Strong-typing
– import
Building an application
• Planning
• Design
• Choosing technologies
– This is NOT part of most college courses, but may be
something you will need to do
– Most projects are part of / relate to other projects
Distribute / deliver / install
This is called the waterfall model.
Real process may not be so neat.
Constant iteration.
• Most projects are team projects
• But…don't specialize / limit what you want to
learn & practice
• Most of my other courses feature team work.
– Last Fall, maybe Fall 2011: Creating Databases for
Web Applications
– Now: Robotics: Spring 2010
– Creating User Interfaces: Spring 2011
Note: Computability: Fall 2010
Creating games: general
• Prepare 'board'
– static material
– dynamic material
• things that move
• things that change
• Define (internal) information
– variables
• strings, numbers, booleans.
• arrays
• objects (properties and methods)
• Implement player moves & other action
– identify/define events and program event handlers
• include random (stochastic) elements
– implement logic of game
Creating games: Flash
Prepare board
• Prepare symbols
– graphics
– instances
– buttons
• Prepare stage
– move instances of symbols & components to stage
• this may be 'off-stage', to be re-positioned by code
– draw directly on stage (including moving graphic
symbols to stage)
– text fields on stage
• static, dynamic, input
Creating games: Flash
Define (internal) information
• variables
– global variables: defined outside of functions,
in main movie and
– local variables: variables defined in functions
– variables in instances
• programmer defined. See next.
– Datatypes: strings, numbers, booleans
– Composite types: arrays, objects
Creating Games: Flash
• Determine built-in classes (coding) to be used
– For example: playback of video
• Determine what classes YOU have written
previously can be re-used
– can be used for any jigsaw!
• Determine what new classes are to be defined
– External .as file(s)
Built-in attributes aka built-in properties
• ball.x,
• ball.y
ActionScript 3.0.
• target.rotation
drops the
underscore _ you
• globe.alpha
will see in old Flash
• block.width
• block.height
• others
Creating games: Flash
Implement player moves and other actions
• ActionScript in frames
• ActionScript for instances "depreciated".
use procedural/frame
– onclipEvent
– on(release)
code to set up event
• ActionScript may also be in frames of movie clip
symbols brought into main movie
– Target in cannonball had stop()
– Other?
There will be another version of
• Basic concepts the same!
– Functions, naming, logic, variables.
– Event handling
• Use Help
– May need to be on-line
Creating games logic
• ActionScript closely resembles other
• statements
– assignment
– if (condition) { }
– if (condition) { } else { }
– for (initialization;condition; increment) {
– switch
Logic: object view
(If you study OOPL, such as Java)
• Logic sometimes is implemented more-or-less
• In objects (movie clip instances are objects), the
programmer can define methods each different
types (sub classes) so the 'if' is done by which
method. Example:
– Method to calculate power done one way for super
hero, another way for mortal.
– Shipping cost: free for preferred customer, a
calculation for regular customer.
• aka pseudo-random because the random
values are produced by a well-defined
procedure. They appear random.
• Math.random() produces a number
(fraction) from zero up to but not including
• Math.floor(Math.random()* 6) produces
one of 0,1,2,3,4,5
• Functions are ways to package code to be
used (and re-used)
• Function definitions specify name and
• Variables defined (var statement) within
the function cannot be accessed outside
the function.
Remember: questions about function, variable,
array refer to technical meaning (programming
jargon) NOT the English meaning of the term.
• A function in programming is a construct that
sets up a sequence of statements, along with
definition of parameters. The coding for function
definition is
function fn (parameter name(s)) { code}
coding for a call to a function uses its name
followed by ( the parameter(s)) and closing )
Definitions, continued
• A variable in programming is associating
a name with a value. The value can
change! Using the name will return the
current value.
• An array in programming is a particular
type of value, namely a set of values. The
common form of array is a sequence of
values. To get the individual values, you
write code with arrayname[indexvalue]
Programming uses a variety of symbols
• ( and ) are used to
– control order of evaluation: a*(b+c)
– hold the parameters in a function definition
– hold the parameters in a function call
– hold the condition in an if statement
– hold the condition in a while statement or
switch statement
– hold the set up values for a for loop
Notation, cont.
• { and } are used
– hold the body of the function in a function
– hold the if true clause and the else clause
– hold the clause in a while or for loop
– hold the clause in a for loop
• [ and ] are used
– setting up arrays, as in var ma = [];
– indexing arrays
For internal calculations and for display
• Use playing of frames (standard is 12 fps)
• Use Timer object
– See bouncing ball, bouncing stuff, cannonball,
Flash issues
• Main movie and movie clip instances each loop
from frame to frame in main movie AND each
movie clip instances unless there are stop(); or
gotoAndPlay() statements
• Functions and variables (outside of functions)
are accessible by name of movie instance.
– suggestion: put all functions and main movie
variables in first frame, actions layer.
• Material on stage in a frame (and in a layer)
– layers are defined to layer graphical material
– organize actionscript, graphics, frame labels
Flash possibilities
• For games, more likely to have fewer frames,
more movement by program control
– Remember: cel animation vs computed animation
Applications may use both!
• Game/application can contain many movie clip
– Use frame code or code in external .as files to create
• Need to addDisplay
– movie clip symbols may be simple or complex (for
example, multi-frame)
– movie clip instances can have actions
• Instead of locating objects in/on 2 dimensional
Stage, position in 3 dimensions
– also may need to orient objects
– position and orient camera/eye to do calculation for
what you see
• 3D in Flash is only partial 3-D. The display list
and layers overrides the 3D positioning.
• I expect improved 3D is coming….
Reprise for games
• What's on the board
– static
– dynamic (changing)
• What is the information
• What are the events and what should be the
response (event handler)? What is the logic of
the game?
– capture and respond to player moves
– respond to other events (for example, timed events)
• Publish application
– Use .fla file and, possibly. .as files
– produces .html and .swf files
• One copy of AS.RunActiveContent.js needed
– Need to upload both .html and .swf
– If you are using any .flv files or .mpg, need to upload
them and use correct reference.
– Warning: the published application has smaller
screen than the development application (.fla file)
• Go from angle to position and position to
• used in ballistics, robotics, driving, etc.
Y = R * sin(a)
X = R * cos(a)
a = atan(Y/X)
Degrees and radians
• How to measure an angle?
• Traditional way: 360 degrees is a full circle
– 90 degrees is a 'right angle'
– 180 degrees is a half circle. 'Do a 180' means to turn
• Alternative measure: a circle is PI (3.14159…)
worth of radians. This is an intrinsic measure.
• Conversion formula:
radians = (PI/180) * degrees
Flash uses both!!!! cannon.rotation takes degrees, the
Math trig methods use radians.
• THE position of an object in Flash and
other languages: x, y [and z] values.
• Is dragon near enough to the gold ? Say
within gap both horizontally and vertically
What if it can be to the right or the left and
above or below?
Use Math.abs() Math.abs(30)=Math.abs(30)
if ((Math.abs(dragon.xgold.x)<gap)&&(Math.abs(dragon.ygold.y))
• Flash provides two methods:
dragon.hitTestPoint(gold.x,gold.y,true) checks if
the single point gold.x, gold.y overlaps with
any occupied pixel of the dragon
dragon.hitTestObject(gold) checks if the
bounding box containing the dragon overlaps
with the bounding box containing the gold.
3D modeling
• Build a virtual world
– approximate shapes using polyhedra (facetted objects)
• surface textures
– special procedures (e.g., fractals) for objects in nature
• Move objects
– articulated (jointed) motions
– expression
• Render objects
– eye (camera), focal point, lighting
Products to do these things but imagination AND
knowledge of mathematics & physics is helpful.
Try Google SketchUP (free version)
• Language built on Java for graphics,
physical computing, other….
• My initial attempts:
Programming Courses
Math/CS (often cross-listed with New Media)
• CS I: programming in Processing!, every semester
• CS II: every Spring
• Creating User Interfaces: Spring 2011
• Games for Change: Spring 2011
• Social Software: Fall 2010
• Physical Computing: Fall 2010
• Robotics, Creating Databases for Web application, others
probably every other year.
• [Finish project
– Upload ]
• Review study guide
• Review lecture charts

Programming games - Purchase College Faculty Web Server