560 likes | 693 Vues
This guide explores the fundamental concepts of conditional statements, blocks, and functions with examples using ActionScript in the SCM-CityU context. You'll learn about simple if statements, if-else structures, and nested conditions. Discover how to define and call functions, manage program flow with braces and indentation, and utilize events and listeners effectively. By the end, you'll grasp these essential programming constructs, making it easier to write and debug code.
E N D
Topic 04: Properties and Events Part I SCM-CityU
Today’s Examples • Simple_Flow.swf and Interactive_Album.swf in W drive SCM-CityU
Review: if Conditional ... if(conditionA){ // process B } ... if Condition A false var score:Number=87; if(score >=90){ trace("A+"); } if(score <90&& score >=85) { trace("A"); } true Process B SCM-CityU
Review: if-else Conditional if(conditionA){ // process B }else{ // process C } … if Condition A true false Process C Process B if(currentFrame == 1){ gotoAndStop(totalFrames); }else{ prevFrame(); } SCM-CityU
Review: else-if Conditional if(conditionA){ // process C }elseif(conditionB){ // process D }else{ // process E } if Condition A true false else-if Process C Condition B false true Process D var age:uint =15; if(age >=21){ trace("adult"); }elseif(age >12){ trace("teen"); }elseif(age >7){ trace("child"); }else{ trace("infant"); } Process E second if-else statement SCM-CityU
Review: if Conditional Summary • Requires oneif • Only one optionalelse can be used • Any number of optionalelse-if test can be used if(...){ } if(...){ }elseif(...){ }elseif(...){ } if(...){ }else{ } if(...){ }elseif(...){ }elseif(...){ }else{ } SCM-CityU
Review: Functions • Example: math function f(x, y) = x2+y2 trace(f(1,2)); trace(f(2,3)); trace(f(3,4)); trace(f(4,5)); function f(x:Number, y:Number):Number{ return x*x + y*y; } function calls function definition SCM-CityU
Review: Functions • Position of function definition doesn’t influence program flow function f(x:Number, y:Number):Number{ return x*x + y*y; } trace(f(1,2)); trace(f(2,3)); trace(f(3,4)); trace(f(4,5)); trace(f(1,2)); trace(f(2,3)); function f(x:Number, y:Number):Number{ return x*x + y*y; } trace(f(3,4)); trace(f(4,5)); trace(f(1,2)); trace(f(2,3)); trace(f(3,4)); trace(f(4,5)); function f(x:Number, y:Number):Number{ return x*x + y*y; } SCM-CityU
Review: Statements and Blocks • Simple statements are expressions followed by a semicolon (;) // the following code contains four simple statements var a:int=3; trace(a); a +=5; trace(a); SCM-CityU
Review: Statements and Blocks • Braces { and } group multiple simple statements into a compound statement or block • { and } must be matched • Indentation highly recommended • Blocks can be nested function genderTest(male:Boolean):void{ if(male){ trace("male"); trace("playing games"); }else{ trace("female"); trace("going shopping"); } } genderTest(false); Question 1: How many blocks? Question 2: Output? SCM-CityU
Review: Indent Style var a:Boolean=true; var b:Boolean=false; if(a){ if(b){ trace(10); } trace(5); }else{ trace(20); } var a:Boolean=true; var b:Boolean=false; if(a){ if(b){ trace(10); } trace(5); }else{ trace(20); } Auto Format SCM-CityU
Review: Indent Style • Indentation reveals program structure easily var a:Boolean=true; var b:Boolean=false; if(a){ if(b){ trace(10); }else{ trace(20); } trace(5); } var a:Boolean=true; var b:Boolean=false; if(a){ if(b){ trace(10); }else{ trace(20); } trace(5); } SCM-CityU
Review: Brackets (wiki) • Parentheses: () • Function parameter list • Precedence ordering • Braces: {} • Define a block, which groups a set of statements • Box/square brackets: [] • Array trace(para1, para2, para3, …, paraN); trace((2+3)*4);// 20 if(female){ trace("female"); trace("hobby: shopping"); } var a:Array=[val1, val2, val3]; SCM-CityU
Review: ; : , . var a:int=3; trace(a); • ; • A semicolon ends a simple statement • : • A colon for data type declaration • , • A comma for separate parameters • . • A.B means A contains B (B can be either a property or a method/function) var pi:Number=3.14; function f(x:Number):Number; function f(x:Number, y:Number):Number; trace(a, b, c, d); bird.speed = 5; bird.fly(); btn_prev.addEventListener(MouseEvent.CLICK,onPrevButtonClick); SCM-CityU
Events & Listeners SCM-CityU
Events & Listeners • Multiple parties might be interested in special things, called events (e.g., clicking), happened to a target object(e.g., a button) Hi folks, I have been clicked! SCM-CityU
Got it. I will perform some follow-up task Hi folks, I have been clicked! Who cares! I’m only interested in keyboard events on you SCM-CityU
Got it. I will perform some follow-up task Hi folks, I have been clicked! • In ActionScript, eventTarget.addEventListener ( EventType, eventListener); • btn.addEventListener(MouseEvent.CLICK, clickTask); • btn.addEventListener(KeyboardEvent.KEY_DOWN, keyDownTask); Who cares. I’m only interested in keyboard events on you SCM-CityU
eventTarget.addEventListener (EventType, eventListener); • eventTarget can be • Stage • Any symbol instance you create, e.g., a button! btn_prev.addEventListener(MouseEvent.CLICK, onPrevButtonClick); function onPrevButtonClick(e:MouseEvent):void { prevFrame(); txt_index.text = currentFrame +"/"+ totalFrames; } SCM-CityU
eventTarget.addEventListener (EventType, eventListener); • Listen to an event type you only care! • There are many event types defined in Event, TimerEvent, KeyboardEvent, MouseEvent, … • E.g., MouseEvent.CLICK Event, MouseEvent … are complex data types for events btn_prev.addEventListener(MouseEvent.CLICK, onPrevButtonClick); functiononPrevButtonClick(e:MouseEvent): void { prevFrame(); txt_index.text = currentFrame + "/" + totalFrames; } SCM-CityU
eventTarget.addEventListener (EventType, eventListener); • eventListener is simply a function but always with a single special parameter • Through this parameter you can get the context where the event happens Note: data type of parameter should becompatible with the one used for eventtype btn_prev.addEventListener(MouseEvent.CLICK,onPrevButtonClick); functiononPrevButtonClick(e:MouseEvent): void { prevFrame(); txt_index.text = currentFrame + "/" + totalFrames; } SCM-CityU
Execution Flow • When an event happens to a target, all registered listeners that are listening to this event type are invoked • E.g., when click event happens to btn • clickTask function will be called • But keyDownTask function won’t • btn.addEventListener(MouseEvent.CLICK, clickTask); • btn.addEventListener(KeyboardEvent.KEY_DOWN, • keyDownTask); SCM-CityU
Simple Flow Control • Our previous album demo • A keyframe in every frame • Frame jumping using ActionScript (at Frame 1) SCM-CityU
Simple Flow Control • Today’s first example • Jumping between tween animations SCM-CityU
Basic Steps Image Source • Change stage size to 181 x 380 • Load images (File > Import to Stage) • Select all images of interest • All images will be loaded into single frame and single layer • In previous example, only first image is selected, all images under the same directory are loaded as a sequence, each image placed into individual key frames • Distribute images to individual layers SCM-CityU
Basic Steps • Drag keyframe of 1.jpg and move it to frame 2 Then insert frame at frame 10 SCM-CityU
Basic Steps • Create motion tweens • Convert to symbols first • Why: motion tween requires symbol instances • How: select 1.jpg image on the stage & right click…. SCM-CityU
Basic Steps • Create motion tweens • Convert to symbols first • Change properties of symbol instances • E.g., alpha in our case Alpha = 0 @Frame1 Alpha = 1 @Frame10 SCM-CityU
Timeline SCM-CityU
Class Exercise • Task: create motion tween for images 2-4 31-40 21-30 11-20 2-10 Frame No SCM-CityU
Basic Steps • Add user control • Have a layer for AS and span the entire timeline • Insert frame at Frame 40 • Insert control button (from built-in component) • This button will appear in the entire animation Instance: playBtn SCM-CityU
Basic Steps • Add user control • Add an event listener for the control button I have been clicked! Event Type Got it. I will play the next key frame. Event Listener Event Target SCM-CityU
Basic Steps • Add user control • Add an event listener for the control button • At Frame 1 Event Target Event Type Event Listener playBtn.addEventListener(MouseEvent.CLICK,onButtonClick); functiononButtonClick(e:MouseEvent):void{ if(currentFrame==1){ gotoAndPlay(2); } } SCM-CityU
Basic Steps • Add user control • Stop animation at Frame 10 • Insert a key frame at Frame 10 • Add stop(); to stop animation • Class exercise • Complete the whole animation • Hints • Event listeners are shared among key frames • So the same playBtn can be used for playing control • Add code in onButtonClick (at Frame 1) SCM-CityU
Frame Labels • 10, 20, 30, 40 are like magic numbers in our app • If you change position of key frames, you have to modify the code correspondingly • A better solution: assigning some labels to key frames and accessing those frames in AS by label SCM-CityU
Frame Labels • Example • Select key frame 10 • Assign a frame label in Properties panel • E.g., 1e (end of 1.jpg) SCM-CityU
Frame Labels • Just like currentFrame representing the number of frame being played, currentFrameLabel is the label at the current frame • currentFrameLabel is of String type (ref) • gotoAndPlay and gotoAndStop accept both frame number or label as input (ref) mc1.gotoAndPlay(currentFrame +5); mc1.gotoAndPlay("1e"); SCM-CityU
Properties SCM-CityU
Properties • Symbol instances provide a large set of predefined propertiesfor manipulation, e.g., • x, y • width, height • scaleX, scaleY • alpha • visible • Access properties using dot syntax • E.g., ball.rotation += 30; SCM-CityU
Properties • Why bother property changing in ActionScript? • To support interactivity! • But you can use properties panelto set initial properties SCM-CityU
Properties: x, y • Using dot syntax • Units: pixels X O Note: y-axis is downward! Y SCM-CityU
Properties: width, height X O box.width box.height Y SCM-CityU
Properties: scaleX, scaleY • Multiplies current width/height by a scale factor X O Y box.scaleX = 0.5; box.scaleY = 0.5; SCM-CityU
Properties: rotation • Clockwise; angle in degree X O Y SCM-CityU
Properties: alpha • Range: [0, 1] box.alpha = 0.5; box.alpha = 1; SCM-CityU
Example: Interactive Album • Control multiple objects • Sharing event listeners • All objects have the samebehavior! SCM-CityU
Basic Steps • Load images to the stage • Convert to symbols and assign instance names • E.g., names: s1, s2, s3, s4 • Resize symbol instances and have an initial layout like s4 s1 s2 s3 SCM-CityU
Add User Controls • Add event listeners for s1 • MouseEvent (ref) • Event types: CLICK, DOUBLE_CLICK, MOUSE_DOWN, MOUSE_UP, MOUSE_MOVE • Repeat this for the other images • All images share same event listeners s1.addEventListener(MouseEvent.MOUSE_DOWN, onPhotoDown); s1.addEventListener(MouseEvent.MOUSE_UP,onPhotoUp); SCM-CityU
Add User Controls Note 1: e.targetrefers to event target where event happens. Note 2: as operator does data type conversion (DisplayObject MovieClip) • Make images draggable • Start to drag when mouse is down • Stop to drag when mouse is up functiononPhotoDown(e:MouseEvent):void{ var photo:MovieClip= e.target as MovieClip; photo.startDrag(); } functiononPhotoUp(e:MouseEvent):void{ var photo:MovieClip= e.target as MovieClip; photo.stopDrag(); } SCM-CityU
Add User Controls • Bring the image being dragged onto topmost (ref) • Each symbol instanceon the stage has an index • numChildren -1 means topmost • 0 means bottommost function onPhotoDown(e:MouseEvent):void{ var photo:MovieClip= e.target as MovieClip; photo.startDrag(); // bring the image being dragged to the top setChildIndex(photo, numChildren -1); } SCM-CityU