13
Variables, Scope, Images, Sound IM 215 - ActionScript

Variables, Scope, Images, Sound

Embed Size (px)

DESCRIPTION

Variables, Scope, Images, Sound. IM 215 - ActionScript. 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 - PowerPoint PPT Presentation

Citation preview

Page 1: Variables, Scope, Images, Sound

Variables, Scope, Images, Sound

IM 215 - ActionScript

Page 2: Variables, Scope, Images, Sound

Legal Variable Names•

may o

nly

con

tain

le

tters, n

um

bers,

dolla

r sign

s an

d u

nd

er-

score

s

•m

ust sta

rt with

a le

tter

or u

nd

ersco

re o

r a

dolla

r sign

•are

case

-inse

nsitiv

e

•su

ffixe

s on

you

r varia

ble

nam

es a

llow

fo

r easy

type

iden

tifica

tion w

hen

re

ad

ing

•var fi

rstNam

e_str;

•var p

rodu

cts_arra

y;

Page 3: Variables, Scope, Images, Sound

Code hints

_mc

Movie

Clip

_arra

y

Arra

y

_str Strin

g

_btn

B

utto

n

_txt

Text Fie

ld

_sound

Sound

_colo

r C

olo

r

_vid

eo

Vid

eo

• Typical variable suffixes by type:

Page 4: Variables, Scope, Images, Sound

= vs. == operatorif (sp

eed

= 1

00

) {

//chan

ges th

e v

alu

e o

f sp

eed

if (speed

==

10

0) {

//d

oes a

com

pariso

n

= se

ts a v

alu

e

==

com

pare

s a v

alu

e

Th

is is a co

mm

on

erro

r. If you

r if/wh

ile

state

men

ts are

n’t

workin

g, ch

eck th

is first.

Page 5: Variables, Scope, Images, Sound

Variable TypingStrict vs. Automatic

Most p

rog

ram

min

g

lan

gu

ag

es re

qu

ire th

at

varia

ble

s have e

xp

licit d

ata

typ

es.

Actio

nscrip

t allo

ws fo

r au

tom

atic ty

pin

g:

var fi

rstNam

e;

firstN

am

e =

“Jim”;

firstN

am

e =

“Daisy

”;tra

ce (fi

rstNam

e);

Con

ven

ien

t, bu

t use

exp

licit typ

ing

inste

ad

in

AS

3, e

.g.:

var fi

rstNam

e:S

tring

;

Page 6: Variables, Scope, Images, Sound

Global vs Local•

Varia

ble

s that ca

n b

e

acce

ssed

thro

ugh

ou

t th

e p

rogra

m a

re ca

lled

g

lob

al v

aria

ble

s.

•Varia

ble

s that a

re

acce

ssible

on

ly to

lim

ited se

ctions o

f the

pro

gra

m a

re ca

lled

lo

cal v

aria

ble

s.

•In

Flash

, all v

aria

ble

s m

ust b

e sco

ped

to o

ne

of th

e fo

llow

ing

;

•A

fun

ction

(loca

l tim

elin

e)

•Th

e m

ain

timelin

e o

r m

ovie

clip (ro

ot

varia

ble

)

Page 7: Variables, Scope, Images, Sound

Variables on different timelines

•C

reate

two d

iffere

nt sh

apes (sq

uare

, circle

) on tw

o d

iffere

nt la

yers. C

reate

an

Actio

ns La

yer, to

o.

•O

n fra

me 1

of th

e sq

uare

layer se

t som

eX

to

3

var so

meX

:Num

ber;

som

eX

= 3

;

•O

n fra

me 1

of th

e circle

layer, se

t som

eY

to 4

var so

meY:N

um

ber;

som

eY =

4;

•Pla

ce in

stance

s of th

e clip

s on fra

me 1

la

yer 1

of th

e m

ain

timelin

e a

nd n

am

e

the in

stance

s square

and circ

le

•O

n fra

me 1

of th

e A

ctions la

yer e

nte

r the

follo

win

g fra

me scrip

t

trace

(som

eX

);tra

ce (so

meY);

Test th

e m

ovie

, what h

appens?

(NaN

=>

Not A

Num

ber)

Page 8: Variables, Scope, Images, Sound

Scope Crash!•

Varia

ble

s atta

ched

to a

m

ovie

clip tim

elin

e

have sco

pe lim

ited to

th

at tim

elin

e. T

hey a

re

not d

irectly

acce

ssible

to

scripts o

n o

ther

timelin

es, su

ch a

s our

main

movie

timelin

e.

•W

hat if w

e p

lace

the

trace

state

men

ts on

fra

me 1

of th

e sq

uare

la

yer?

•N

ow

wh

at h

ap

pens

wh

en y

ou

pu

t the

trace

state

men

ts in

the circle

layer?

Page 9: Variables, Scope, Images, Sound

Scope in Functions•

Try th

is in A

ction

s:var num1:int = 30;

var num2:int = 40;

var num3:int = 50;

addIt();

function addIt():void

{var sum:int = num1 +

num2 + num3;

trace(“Sum from

inside the

function”);

}

•W

hat h

ap

pen

s wh

en

you

trace

the su

m a

fter

the a

dd

It() fun

ction

ca

ll?

Page 10: Variables, Scope, Images, Sound

Images•

Cre

ate

an

em

pty

M

ovie

Clip

:

•In

sert ->

New

Sym

bol

•G

ive it th

e n

am

e

“spotFo

rPicture

•The sy

mbol lo

catio

n o

n

your sta

ge is w

here

th

e u

pper le

ft of y

our

image w

ill appear

•N

am

e th

e m

ovie

clip o

n

the sta

ge

curre

ntPictu

re

•A

dd

the fo

llow

ing

AS

co

de:

•var p

ic:Loader =

new

Lo

ader();

•pic.lo

ad(n

ew

U

RLR

equest(“fi

lenam

e.

jpg”));

•cu

rrentPictu

re.a

ddC

hil

d(p

ic);

•U

se th

e sa

me co

de to

lo

ad

a n

ew

imag

e—

just

don

’t rein

itialize

the p

ic varia

ble

.

Page 11: Variables, Scope, Images, Sound

Sound Objects•

To cre

ate

the so

un

d

and

start it

var

my_in

troso

un

d:S

ou

nd

= n

ew

Sou

nd

();m

y_in

troso

un

d.lo

ad

(n

ew

U

RLR

eq

uest("so

un

ds/fi

len

am

e.m

p3

"));

var

chan

nel:S

ou

nd

Ch

an

nel;

chan

nel =

m

y_in

troso

un

d.p

lay

();

•To

stop

the so

und

chan

nel.sto

p();

•S

ee p

p. 2

96

-31

2 in

you

r book

Page 12: Variables, Scope, Images, Sound

Exercise•

Cre

ate

a p

rog

ram

that

has o

ne fra

me a

nd

fou

r d

iffere

nt b

utto

ns, S

how

Im

ag

e 1

, Sh

ow

Imag

e

2, P

lay S

ou

nd

1, P

lay

sou

nd

2.

•D

o n

ot p

lay a

sou

nd

or

show

an

imag

e in

itially.

•W

hen

the u

ser se

lects a

p

lay so

un

d b

utto

n, a

u

niq

ue so

un

d w

ill pla

y

for e

ach

bu

tton

.

•W

hen

the u

ser se

lects a

sh

ow

imag

e b

utto

n, a

u

niq

ue im

ag

e w

ill be

disp

layed

for e

ach

b

utto

n.

Page 13: Variables, Scope, Images, Sound

Sources:•

“Actio

nS

cript 3

.0 w

ith

Flash

CS

3 a

nd

Flex”

(Ch

ap

ters 3

an

d 8

) by

Web

ster, Ya

rd, a

nd

M

cSharry

•“E

ssen

tial A

ction

Scrip

t 3

.0” b

y C

olin

Moock

(O’R

eilly

)