dig2500c: fundamentals of interactive design

46
DIG2500c: Fundamentals of Interactive Design Fall 2009 Semester Dr. Rudy McDaniel Lecture 9: ActionScript Programming: The Basics, Part 2

Upload: laasya

Post on 14-Feb-2016

40 views

Category:

Documents


0 download

DESCRIPTION

Fall 2009 Semester Dr. Rudy McDaniel. DIG2500c: Fundamentals of Interactive Design. Lecture 9: ActionScript Programming: The Basics, Part 2. Reminder. Lab 3 due this Friday by 5pm Please name your files correctly and be sure to upload them into the correct directory! - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: DIG2500c: Fundamentals of Interactive Design

DIG2500c: Fundamentals of Interactive Design

Fall 2009 Semester

Dr. Rudy McDaniel

Lecture 9: ActionScript Programming: The Basics, Part 2

Page 2: DIG2500c: Fundamentals of Interactive Design

Reminder Lab 3 due this Friday by 5pm

Please name your files correctly and be sure to upload them into the correct directory!

From now on, all code examples from lecture can be found online here: http://www.dm.ucf.edu/~rmcdaniel/courses/dig2500c/code/ Both SWF and FLA files are available for

download

Page 3: DIG2500c: Fundamentals of Interactive Design

How to Study Don’t forget: treat these programming slides

like math problems. Try each and every example on your own until you understand how the logic and syntax works. It will be slow going at first, but you’ll become more comfortable with coding over time.

Download the examples from the web, tweak them, play with the values, and experiment! You’ll need to use these techniques for your final project!

Page 4: DIG2500c: Fundamentals of Interactive Design

Lecture Review from Last Time Computers are stupid. That makes

our jobs difficult as programmers.

Think iteratively and design in English, pseudocode, then code.

Last week, we discussed variables, variable typing, instance names, and basic dot notation on objects.

Page 5: DIG2500c: Fundamentals of Interactive Design

Remember the Method

Write it out in English first Then sort into the programming structures

Identify all variables, conditions, loops, and functions required

Write out in pseudo-code Refine pseudo-code

Investigate if functions close to what is needed are built into programming language Study vocabulary and syntax!

Then, code

Page 6: DIG2500c: Fundamentals of Interactive Design

This Week

Controlling the Timeline and Movie Clips

Loading External Web Pages

Conditional Logic

Loops

Functions

Page 7: DIG2500c: Fundamentals of Interactive Design

Controlling the Timeline and Movie Clips

Controlling the Timeline Controlling Movie Clips The gotoAndPlay and gotoAndStop

functions URL objects and functions

Page 8: DIG2500c: Fundamentals of Interactive Design

Controlling the Timeline

Movies by default play in their entirety until it reaches the last frame of the Timeline, unless you tell it to do otherwise.

ActionScript can control the movie with play and stop actions.

You can apply these actions to both frames and buttons

Page 9: DIG2500c: Fundamentals of Interactive Design

Controlling Movie Clips

Movie Clips can be controlled just like the main Timeline

Instance naming (we’ve seen this already)

The dot (.) syntax

Page 10: DIG2500c: Fundamentals of Interactive Design

The goto Actions

nextFrame(); prevFrame(); gotoAndStop(); v. gotoAndPlay();

Page 11: DIG2500c: Fundamentals of Interactive Design

Moving through Frames

nextFrame(); // moves to next frame in timeline

prevFrame(); // moves to previous frame in timeline

Page 12: DIG2500c: Fundamentals of Interactive Design

Example

Consider this scene, which has five keyframes, each which displays a box with a different background color

Page 13: DIG2500c: Fundamentals of Interactive Design

By Default

If we compile and run this SWF file with no ActionScript, it will create a strobe-like effect.

Live Demo

Page 14: DIG2500c: Fundamentals of Interactive Design

Adding a Timer and Frame Control

Page 15: DIG2500c: Fundamentals of Interactive Design

New Version

We control advancing through each keyframe, one time unit at a time.

Live Demo

At this point, we’ve used objects, functions, event handling, and timeline control functions!

Page 16: DIG2500c: Fundamentals of Interactive Design

gotoAndPlay vs. gotoAndStop Both move the playhead to specific locations

on the timeline. Can be used with frame numbers or frame labels.

gotoAndPlay: plays from a set frame until the next forced stop (either the end of the timeline or a stop() action).

gotoAndStop: goes to a set frame, plays it, then stops until the timeline is triggered again.

Page 17: DIG2500c: Fundamentals of Interactive Design

Questions

In general, when is it appropriate to use these different actions?

Specifically, when might each of these be appropriate in your interactive movie site?

Page 18: DIG2500c: Fundamentals of Interactive Design

Example (Movie Clip “BlueBall”)

Page 19: DIG2500c: Fundamentals of Interactive Design

Live Demo

[Live Demo]

But what if we want the blue ball and the green rectangle to animate independently of one another, and perhaps at irregular times?

What if we want to cycle a dropping ball on a different timetable?

Page 20: DIG2500c: Fundamentals of Interactive Design

Example (Scene 1, Using Frame Number)

We use dot notation to specify the correct frame number to start playing from.

Page 21: DIG2500c: Fundamentals of Interactive Design

Example (Scene 1, Using Label)

Labels are useful because they will always work as expected during run time.

Page 22: DIG2500c: Fundamentals of Interactive Design

Revising the ActionScript

Makes the code much easier to understand from within the scene!

Page 23: DIG2500c: Fundamentals of Interactive Design

Live Demo

[Live Demo]

Sequence of events: Main timeline animates BlueBall object is located (movie clip) The keyframe sequence labeled “drop”

within the BlueBall object animates

This allows for sophisticated animation within collections of movie clips!

Page 24: DIG2500c: Fundamentals of Interactive Design

Go to Web Page BehaviorKey Objects and Functions: URLRequest(URL); navigateToURL(URLRequest,target);

Relative v. Absolute Addressing Targeting Sending email with a mailto:URL link

Page 25: DIG2500c: Fundamentals of Interactive Design

Example Code (Using Exception Handling)

var url:String = "http://www.dm.ucf.edu"; var request:URLRequest = new URLRequest(url); try

{ navigateToURL(request, '_blank'); // second argument is target }

catch (e:Error) { trace ("Error occurred!"); }

Page 26: DIG2500c: Fundamentals of Interactive Design

Adding Event Handler

If we wanted to only create the URLRequest object when a button was clicked, we would need to attach an event handler for that button.

Page 27: DIG2500c: Fundamentals of Interactive Design

Event Handling Code

Page 28: DIG2500c: Fundamentals of Interactive Design

Live Demo

[Live Demo]

In this code, the web site is only launched once the user has clicked on the button.

We are gradually moving towards a more interactive experience.

Page 29: DIG2500c: Fundamentals of Interactive Design

Remember How Flash Works Flash movies play in their entirety

until they reach the last frame of the timeline, unless you tell it to do otherwise.

ActionScript can control a timeline with play(); and stop(); You can apply these actions to the main

timeline and to the timelines inside movieclips

Page 30: DIG2500c: Fundamentals of Interactive Design

Operators and Datatypes Most of the time, when we're using

comparison operators, we're comparing numbers

When the two operands of any comparison operator belong to different datatypes, or when neither operand is a string or a number, the interpreter attempts a type conversion according to the following steps:

Page 31: DIG2500c: Fundamentals of Interactive Design

Steps to Compare Different Datatypes1. If both operands are numbers, compare the operands

mathematically and return the result. If either number is (or if both numbers are) NaN, the result of the comparison is false, except in the case of the != operator.

2. If both operands are strings, compare the operands by Unicode code point and return the result. For characters in the Latin alphabet, this is a case-sensitive, alphabetic comparison.

3. If one operand is a number and the other is a string, convert the string to a number and go back to Step 1.

4. If either operand is a Boolean, null, or undefined, convert the operand to a number and go back to Step 1.

5. If either operand is an object, invoke its valueOf ( ) method to convert the object to a primitive value and go back to Step 1. If the valueOf ( ) method fails or does not return a primitive value, return false.

6. For any other condition, return false.

Page 32: DIG2500c: Fundamentals of Interactive Design

Add logical operators to gain more options AND operator

(operand1) &&(operand2) OR operator

(operand1) || (operand2) NOT operator

!operand Useful for constructing more

complex logic

Page 33: DIG2500c: Fundamentals of Interactive Design

Simple Example (Using Input Text)

Page 34: DIG2500c: Fundamentals of Interactive Design

Dynamic Text Fields

Page 35: DIG2500c: Fundamentals of Interactive Design

Code Part I (Event Handler)

Note how we access the text field properties of our dynamic text inputs using dot notation.

Page 36: DIG2500c: Fundamentals of Interactive Design

Code Part II (Conditional Logic)

Page 38: DIG2500c: Fundamentals of Interactive Design

Loops - The for Loop

Here's the syntax of the for loop: for (initialization; condition; update) { substatements }

Before the first iteration of a for loop, the initialization statement is performed (once and only once).

It is typically used to set the initial value of one or more iterator variables

Page 39: DIG2500c: Fundamentals of Interactive Design

For Loop Example

Easier to understand if we do an example:

for (var i = 1; i <= 10; i++) { trace("Now serving number " + i); }

Remember that + here acts as a concatenation (glue) operator.

Page 40: DIG2500c: Fundamentals of Interactive Design

A More Complicated (But Interesting) Example

Did you know you can draw graphics without even using the GUI tools?

Page 41: DIG2500c: Fundamentals of Interactive Design

Output Cool, huh? How do you

think we would change the: Color? Stroke? Number of

squares? Distance

between squares?

Page 42: DIG2500c: Fundamentals of Interactive Design

Loops – The while Loop

Structurally, a while statement is constructed much like an if statement: a main statement encloses a block of substatements that are executed only when a given condition is true:while (condition) { substatements}

Page 43: DIG2500c: Fundamentals of Interactive Design

While Loop Example

The same loop as before, but as a while loopvar i = 1;while (i <= 10) { trace("Now serving number " + i); i++;}

Page 44: DIG2500c: Fundamentals of Interactive Design

Other Code as While Loop

Page 45: DIG2500c: Fundamentals of Interactive Design

In Review

Loops – limit the number of duplicate segments of code we have to write

Questions?

Page 46: DIG2500c: Fundamentals of Interactive Design

Homework

Keep Working on Lab 3

Keep up with your readings from the Flash book!