313 Flash Lecture

1 / 26

# 313 Flash Lecture - PowerPoint PPT Presentation

313 Flash Lecture. Lecture Three. Scripts on Frames vs. Buttons. what is the difference between the two? let’s write a goto on a frame script gotoAndStop("section1"); lets write a button event (when we press it) that takes us to a frame label on(release) { gotoAndStop(“framelabelname”); }.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about '313 Flash Lecture' - gabriel-cardenas

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### 313 Flash Lecture

Lecture Three

Scripts on Frames vs. Buttons
• what is the difference between the two?
• let’s write a goto on a frame script

gotoAndStop("section1");

• lets write a button event (when we press it) that takes us to a frame label

on(release) {

gotoAndStop(“framelabelname”);

}

Conditionals (if)

These are the gateways to creating smart and intelligent Flash movies.

If (currentProfessor == “overlydemanding” {

trace (“What did I do to deserve this?);

}

If (this condition is met) {

then execute this statement

}

Loops
• The repeating of the tedious tasks that we don’t want to do!
• How could we get the computer to output a list of numbers from 1 through 5?
• You already know one simple way?
Loop (trace)

trace (1);

trace (2);

trace (3);

trace (4);

trace (5);

But what if we want the series to start at 100? There must be an easier way!

Loop (trace) with variable

var x = 1;

trace(x);

what do you think would come next to get to two?

x = x + 1;

trace(x):

if we change the starting number when we assign x to 100 it will now count up from 100 without changing five numbers, we only change one.

what if we wanted to count to 1000?

the while loop

var x = 1;

while (x <= 5) {

trace (x);

x = x + 1;

}

Functions

Functions allow us to create little sections of modular code.

We can also make/define our own.

function area(height, width) {

return height * width;

}

Functions take arguments and return information.

Variables continued…
• to create a new variable we use the var statement
• one of the best places to do this is in an init script at the beginning of your movie (normally after a pre-loader)
• var whatever;
• var nameof book;
• var x;
Legal Variable Names
• may only contain letters, numbers, dollar signs and under-scores
• must start with a letter or underscore or a dollar sign
• are case-insensitive
• var firstName_str;
• var products_array;
_mc MovieClip

_array Array

_str String

_btn Button

_txt Text Field

_sound Sound

_color Color

_video Video

Code hints
Assignment Order

variable assignment works from right to left

variableName = value;

simple example

speed = 100;

complex example

x = x + 1

= vs. == operator

if (speed = 100) { //changes the value of speed

if (speed == 100) { //does a comparison

= sets a value

== compares a value

Variable TypingStrict vs. Automatic

Most programming languages require that we tell a variable what type of datum it can accept. Actionscript 2.0 allows for automatic typing, although it is being discouraged. Let’s put this in frame one.

var firstName;

firstName = “Jim”;

firstName = “Daisy”;

trace (firstName)

Variable Scope(where it all breaks down…)
• Flash is very picky in wanting to know what is being executed where. You have to let it know where variables exist. There are several strategies used to achieve the communication you desire among your Flash minions.
• Let’s do a little experiment.
Variable Scope cont.

go to frame two of the main timeline

var x;

x = 10;

go to frame three of the main timeline

trace(x)

test the movie, what happens?

Variable Scope cont.

get rid of the code on frames two and three

go to frame thirty of the main timeline

var x;

x = 10;

stop();

go to frame fifteen of the main timeline

trace (x);

test the movie, what happens and why?

Global vs Local
• Variables that can be accessed throughout the program are called global variables.
• Variables that are accessible only to limited sections of the program are called local variables.
• In Flash, all variables must be scoped to one of the following;
• A function (local timeline)
• The main timeline or movie clip (timeline variable)
• The _global object (global variable)
Variables on different timelines

create two different shapes (square, circle) and define them as movie clip symbols

on frame 1 of square set x to 3

var x;

x = 3;

on frame 1 of circle set y to 4

var y;

y = 4;

place instances of the clips on frame 1 layer 1 of the main timeline and name the instances square and circle

on frame 1 of the main timeline enter the following frame script

trace (x);

trace (y);

test the movie, what happens?

UNDEFINED!

Scope Crash!
• Variables attached to a movie clip timeline have scope limited to that timeline. they are not directly accessible to scripts on other timelines, such as our main movie timeline.
• What if we place the trace(x) and trace(y) statements on frame 1 of the square movie clip
• Comment out the trace commands on the main timeline with // at the beginning of the line. That will leave the code, but the flash interpreter will ignore it

//trace(x);

• They are not directly accessible, but can be accessed indirectly using dot syntax.
• movieClipInstanceName.variableName
• If we refer to a variable on another timeline we must use the name of the movie clip to get there. For example, if we are in main timeline and want to get to the variables in our circle and square movie clips, how would we get there?

square.x

circle.y

This is known as a qualified reference.

Some gotcha’s
• make sure that you have named your movie clips with instance names
• make sure that your movie clips are persistent on the stage. If you have a keyframe on the main timeline, the variables will be lost.
• you have to make sure that the variable has been created in the movie clip before you call on it in the main timeline. ex. if you declare the variable in frame one of the movie clip it is not accessible until frame two of the main timeline
Accessing Variables on Different Timelines (cont.)
• To get access a variable from square while inside the circle timeline. We need to use an absolute reference.
• If we are in circle, we can get to square’s x variable.

_root.square.x

This command will work anywhere in our movie.

Let’s comment out the trace command in frame one of the main timeline and add a trace in circle that gets to square’s x value.

We can also move relatively between clips.

by using the _parent.myVariable syntax we can go up in the timeline.

this refers to the variables on the timeline above the location you are calling it from

create a new flash file

go to frame one of the main timeline and type

var x;

x = 10;

create another movie clip symbol and put it on the timeline. Go to frame one on the timeline of that movie clip.

trace(_parent.x);

test the movie

Let’s try a global variable

Go to frame 1 of the square movie

_global.day = “Wednesday”;

Go to frame 1 of the main timeline

trace (day);

Test the movie

Go to frame 1 of the circle movie

var day = “Friday”;

trace (day);

Test the movie, which variable has precedence?