multimedia lecture actionscript3

35
Eng: Mohammed Hussein 1 Lecturer, and Researcher at Thamar University By Eng: Mohammed Hussein [email protected]

Upload: mohammed-hussein

Post on 22-May-2015

366 views

Category:

Education


0 download

DESCRIPTION

Adobe Action Script 3.0 code

TRANSCRIPT

Page 1: Multimedia lecture ActionScript3

Eng: Mohammed Hussein1

Lecturer, and Researcher at Thamar University

By Eng: Mohammed Hussein

[email protected]

Page 2: Multimedia lecture ActionScript3

Output and trace in ActionScript 3.0

Eng: Mohammed Hussein2

Define a variable and print it.

Define for loop.

Define a function to object

during an event.

Page 3: Multimedia lecture ActionScript3

Arrays In ActionScript 3.0

Eng: Mohammed Hussein3

For example :

Page 4: Multimedia lecture ActionScript3

Using Mouse Events to Control Properties

Eng: Mohammed Hussein4

Events are responsible for setting your scripts in motion, causing

them to execute.

A button can be triggered by a mouse event, text fields react to

keyboard events—even calling your own custom functions.

In ActionScript 3.0, trapping events is simplified by relying on one

approach for all event handling, which is to use event listeners

regardless of the type of event or how it is used.

Page 5: Multimedia lecture ActionScript3

Events

Eng: Mohammed Hussein5

What is addEventListener() function and its two parameters?

What is Event.ENTER_FRAME?

What does trace( ) function?

In action script 3 look at the following code and give it a title name ?

Page 6: Multimedia lecture ActionScript3

Event Handling

Eng: Mohammed Hussein6

Event handling is the process by which any sort of interactivity is

created in ActionScript 3.0.

Event Handling system of AS3 are reacting to a mouse click, a

keyboard stroke, or any event happening in Flash which is divided into

the following sections:

1. Basic Event Handling Using the .addEventListener() method.

2. Unregistered Events Listeners using

the removeEventListener() method.

3. Working with Event Targets.

Page 7: Multimedia lecture ActionScript3

Event Listener

Eng: Mohammed Hussein7

An ActionScript Event is any interaction happening in the Flash environment, whether it was a mouse click, the movement of the timeline to a new frame, the completion of the loading process of an external asset, or any other occurrence.

ActionScript can make any object react to any event by using an Event Listener.

An event listener is basically a function created with the very specific purpose of reacting to an event.

An object can react to an event using an event listener. This is done by using the .addEventListenermethod. This method simply registers an Event Listener and an Event to an object.

The process described above is written in ActionScript using in the format shown below:

myObject.addEventListener(Event, eventListenerFunction);

Page 8: Multimedia lecture ActionScript3

addEventListener() method

Eng: Mohammed Hussein8

Our Event Listener will obviously have to be specified by declaring

the function the same way any other function is declared in

ActionScript, the only difference is that the listener function must

have one argument to represent the event.

This event argument can have any identifier as its name, usually used

the letter 'e' for it as shown in the generalized code below:

myObject.addEventListener(Event, eventListenerFunction);

function eventListenerFunction (e:Event):void{

//ActionScript statements to be executed when the event happens.

}

Page 9: Multimedia lecture ActionScript3

Event can be registered

Eng: Mohammed Hussein9

for example, if we want a graphical object placed on stage to act like a button by reacting to a mouse click over it, we can simply register an event and an event listener to it this way:

For example, if you are using the Loader Class to load an external asset at run time, you can perform a specific action only when the asset you are trying to load finishes loading. For this, you will need to register for the Event.COMPLETE as shown in the example below:

myButton.addEventListener(MouseEvent.CLICK, myClickReaction);

function myClickReaction (e:MouseEvent):void{

trace("I was clicked!");

}

my_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, startListener);

function startListener (e:Event):void{

trace("Loading Completed");

}

Page 10: Multimedia lecture ActionScript3

Unregistering Event Listeners

Eng: Mohammed Hussein10

To unregister an event you can use the .removeEventListener() method in

the same exact way the .addEventListener() method is used. This method

requires specifying the object from which the event listener is to be

unregistered, the event to stop listening to, and the function that was

assigned to this specific event. Here is a generalized code on the usage of

this method:

myObject.removeEventListener(Event, eventListenerFunction);

For example, if an event listener function was registered to be triggered on

the entry of each new frame on the main timeline we would have registered

it this way:

this.removeEventListener(Event.ENTER_FRAME, loading);

Page 11: Multimedia lecture ActionScript3

Event Targets and Event Propagation

Eng: Mohammed Hussein11

Depending on the event handled, an event would usually occur to a specific object. For example, a click event will happen to a specific button and a load complete event will happen to a specific instance of the loader class.

1. Event Target with name movie, if we want an object to become hidden when clicked, within the listener function to hide it this way:

2. Event Target without specifying its name complex movies , we use the keyword e.currentTarget because you want to reuse the same listener function with more than one object. Now reuse this listener function for more than one object without fear of breaking the code because of the smart reference to our event target.

my_btn.addEventListener(MouseEvent.CLICK, hideMe);

function hideMe(e:MouseEvent):void{

my_btn.visible=false;

}

my_btn.addEventListener(MouseEvent.CLICK, hideMe);

function hideMe(e:MouseEvent):void{

e.currentTarget.visible=false;

}

my2_btn.addEventListener(MouseEvent.CLICK, hideMe);

Page 12: Multimedia lecture ActionScript3

Adding children

Eng: Mohammed Hussein12

It is possible to refer to the children of an object to which an event

was registered using the keyword e.target (as opposed to

e.currentTarget) to refer directly to these objects.

For example, if we have a MovieClip movie that has three buttons, we can

hide each of these buttons on its own when individually clicked by

registering ONE event listener to ONE object only, which is in this case

the display object container, i.e. the menu MovieClip, here is an example:

var myMenu_mc:MovieClip = new MovieClip();

myMenu_mc.addChild(my1_btn);

myMenu_mc.addChild(my2_btn);

myMenu_mc.addChild(my3_btn);

myMenu_mc.addEventListener(MouseEvent.CLICK, hideThisButton);

function hideThisButton(e:MouseEvent):void{

e.target.visible=false; }

MovieClip: myMenu_mc

has three buttons, we can hide

each of these buttons on its

own when individually

clicked.

The event listener function

registered with the actual

button clicked to e.target

Page 13: Multimedia lecture ActionScript3

Sound effects

13 Eng: Mohammed Hussein

Page 14: Multimedia lecture ActionScript3

Sound effect example

14 Eng: Mohammed Hussein

Page 15: Multimedia lecture ActionScript3

Playing sounds using ActionScript 3.0

Eng: Mohammed Hussein15

Playing sounds using ActionScript 3.0 is not as simple as we hoped

it to be as it requires using more than one class to perform even the

simplest of tasks relating to sound such as pausing or changing the

sound volume.

Introduction to Sound Related Classes.

Playing an Internal Sound.

Playing an External Sound.

Stopping a Sound.

Pausing a Sound.

Changing Sound Volume.

Page 16: Multimedia lecture ActionScript3

Sound classes Sounds in ActionScript 3.0 are manipulated through the collaborative work of several

classes together. This format will provide you with greater control and the ability to micro

manage sounds. Here are the relevant classes related to sounds:

1. Sound Class -This is the main class in which a sound will actually reside. This class is

the starting point of any sound related program and is used to start playing a sound.

2. SoundChannel Class - A sound can be played through a sound channel which

provides additional controls over a sound object, the most basic of these additional

controls is the ability to stop the playback of a sound.

3. SoundTransform Class -This class is responsible for altering sound volume and

sound panning (manipulating the balance between the left and right speakers).

4. SoundMixer Class -This class has global control over all sounds played in the Flash

player. It's most basic function is to stop all playing sounds regardless of source.

16 Eng: Mohammed Hussein

Page 17: Multimedia lecture ActionScript3

Load sound

Eng: Mohammed Hussein17

Two ways to load sound into flash:

1. First by using this code of AS3

2. Second one using import sound into

flash

Page 18: Multimedia lecture ActionScript3

Stop sound

Eng: Mohammed Hussein18

To stop all sounds in your movie use this code:

Page 19: Multimedia lecture ActionScript3

Animated Speakers

Eng: Mohammed Hussein19

Sound example

Page 20: Multimedia lecture ActionScript3

Animated Speakers and Equalizer Trick

(ASET) project

Eng: Mohammed Hussein20

.stop() - this method stops the sound

playing through the channel.

.position - this property is used to

retrieve the current playback

position of the sound playing

through the channel.

.soundTransform - this property is used

to set and retrieve sound

transformations such as volume

and panning.

Page 21: Multimedia lecture ActionScript3

(ASET) project steps

Eng: Mohammed Hussein21

1. Set Boolean value for buttons Play and Stop functions.

2. Create the sound object

3. Create the URL request that grabs the MP3 to play from your server or hard drive.

4. Load the URL request into the Sound object

5. Create the SoundChannel variable.

6. Start playing the sound here in the channel variable

7. Set "isPalying" Boolean value to true because it is now playing.

8. Add listener to see when the song finishes to run function [onPlaybackComplete] when it does Add listener to trigger [onEnterFrame].

Page 22: Multimedia lecture ActionScript3

(ASET) project code

Eng: Mohammed Hussein22

onEnterFrame

function run in loop

which can manipulate

our idea.

onPlaybackComplete

function, used when

the song finishes to

stop channel playing.

Page 23: Multimedia lecture ActionScript3

Playing Video example

23 Eng: Mohammed Hussein

To add video into the Display List use

the addChild() method.

The .source property is used to specify the video to be played.

Page 24: Multimedia lecture ActionScript3

Applying a Skin to FLVPlayback Component

Eng: Mohammed Hussein24

The FLVPlayback Component is used when you want to play a video.

To import the component to Library will require us to access the

Components Panel by going through Window>Component , look for the

FLVPlayback Component under the Video category and then drag and drop

an instance of it on stage and then delete it. Which should store an instance

of the component in the Library.

The graphical elements of skin are actually saved in a separate SWF file that

is loaded at run time by the main SWF movie. If you have the skin SWF file

available you simply set its URL as the value for a property called .

Select your component on the stage and properties then skin.

Page 25: Multimedia lecture ActionScript3

Slide show

Eng: Mohammed Hussein25

Give a title name

for this

ActionScript3 code?

Page 26: Multimedia lecture ActionScript3

Color

Eng: Mohammed Hussein26

Give a title name

for this

ActionScript3

code?

AS3 Changing

Colors

Page 27: Multimedia lecture ActionScript3

What is the output of this code?

Eng: Mohammed Hussein27

Output:

Venus,Earth,Mars

Page 28: Multimedia lecture ActionScript3

What is the output of this code?

Eng: Mohammed Hussein28

Output :Maybe #0: I

Maybe #1: am

Maybe #2: Here

Maybe #3: Ok

Maybe # 3: Ok 4

Maybe # 3: Ok 4

Page 29: Multimedia lecture ActionScript3

What is the output of this code?

Eng: Mohammed Hussein29

Output :80

Page 30: Multimedia lecture ActionScript3

What is the output of this code?

Eng: Mohammed Hussein30

Output :10 1511 1611 16

Page 31: Multimedia lecture ActionScript3

After you Clicked the button and write the

outputs ?

Eng: Mohammed Hussein31

Output :mohammed

ali

salim

Page 32: Multimedia lecture ActionScript3

What is the output of this code?

Eng: Mohammed Hussein32

Output :

-1

true

Page 33: Multimedia lecture ActionScript3

What is the output of this code?

Eng: Mohammed Hussein33

Output :

y: 33

x: 24

Page 34: Multimedia lecture ActionScript3

Correct the flowing code and write the

output ?

Eng: Mohammed Hussein34

Page 35: Multimedia lecture ActionScript3

What is the output of this code?

Eng: Mohammed Hussein35

Output :Hello, Dr. Mohammed, nice to meet you.

Hello, saddam, nice to meet you.