TweetFollow Us on Twitter

Macromedia Flash; Visual vs. Language-Based programming

Volume Number: 18 (2002)
Issue Number: 8
Column Tag: Reviews

Macromedia Flash; Visual vs. Language-Based programming

by Bradley Kaldahl and Jacob Kirk

Macromedia Flash has taken the web by storm and with it a new programming paradigm has evolved that we refer to as "Visual Programming." Non-traditional "Visual Programmers" are producing algorithms that are really quite intriguing. In some respects this new visual paradigm represents an opportunity to create algorithms based on visual logic, which is an interesting alternative to traditional language-based algorithms. Please read on to see if you agree.

In the traditional language-based programming paradigm, the developer relies almost completely on the programming code to accomplish the desired task. While several highly respected authors such as Grady Booch and Coad & Yourdon have created visual diagram-based models for OO development, ultimately the design is translated into code. On the other hand Flash allows both design and implementation in a visual environment. This visual style of programming obviously has a strong appeal to certain individuals, most notably creatives, designers, and those unfamiliar with traditional programming languages and conventions.

Any programmer called upon to work or consult on an ActionScript project should be aware of the issues and idiosyncrasies of this new trend in "Visual Programming" as they will likely encounter legacy code that has been developed using the visual paradigm.

Problem definition

To contrast the visual paradigm with the language-based approach we have defined a simple programming problem.

Using Macromedia Flash 5, animate an object to move across the stage. When the user rolls-over the object have it reverse direction.

If you are familiar with Flash and ActionScript then we hope that you will enjoy the contrast of these two disparate techniques. If you are new to ActionScript we hope you will benefit from our discussion of some of the programming anomalies you will encounter in Flash when using the language-based approach.

Anomalies in ActionScript

While ActionScript resembles JavaScript, it is helpful to remember that ActionScript must conform to the animation and encapsulation features found in Flash. One of the more noticeable programming differences you will encounter in Flash is that you cannot animate an object using a standard FOR or WHILE repeat loop. There has been some debate as to why this is the case. Some have suggested that it is because the computer can execute a repeat loop at a much faster rate than the screen can redraw. While on the surface this sounds plausible, it suggests that computer animation within a repeat loop cannot be accomplished. According to Michael Williams, Associate Product Manager at Macromedia: "The issue here is not the fact that FOR and WHILE execute too quickly, it is because ActionScript, on a frame, executes before the stage is updated. The stage is only updated after all scripts on that frame have been executed."

If you do attempt to use the FOR or WHILE repeat loop for animation you will most likely encounter the following alert.


Figure 1. Flash places a 15 second time-out limit on FOR and WHILE repeat loops.

Mr. Williams explains: "The Flash Player itself provides this option to abort the script. The Flash Player will do this when it plays a movie which contains a one-frame ActionScript loop which can repeat itself indefinitely or for too long. A movie with such a loop can tax the resources of the computer system playing it. Giving viewers the option to end the script helps the Flash Player prevent an SWF file from freezing the viewer's browser or system. To avoid creating a movie that triggers this error message when it is played in the Flash Player, create a loop that goes through frames. You can do this by evaluating a variable in an if statement and, if the variable conditionalizes as true, then have the movie break the loop by going to the next frame. If the variable does not conditionalize as true, then have the movie loop back to the first frame (usually two to three frames back) of the movie."

The question still remains: How can an object be animated and controlled with ActionScript?

As discussed by Mr. Williams: "Use ActionScript applied to a Movie Clip, also known as clipEvents. For example, you have a movieClip on stage named "circle" the following code will move the circle across the X axis 10 pixels once per frame:

onClipEvent(enterFrame) {
    _x += 10;
}

This is dependent on the frame rate of the movie. In the default case this will refresh 12 frames/sec, but this can be increased or decreased by changing the movies frame rate at authortime."

The Language-Based Solution

PsuedoCode

Move the object by 15 pixels per frame.
   If the object goes off the left edge of the stage 
      then place the object on the right side of the stage.
   If the object goes off the right side of the stage
      then place the object on the left side of the stage.
When the user rolls-over the object
   stop the object from moving.
When the user rolls-off the object
   reverse the direction of the object.

Listing 1: Language-Based MovieClip Script

Animate and wrap object.
/* onClipEvent(load) is being used to define some of the variables.
The variable moveAmount is the number of pixels the object will be animated.

ballLoc: (ball location) is used to define the location of the center point of the movieClip that 
encapsulates the ball-Button object. 

_parent: is a relative reference to the movieClip that encapsulates the ball object.*/

onClipEvent (load) {         
   moveAmount = 15;   
   ballLoc = _parent
               
}

/* The (enterFrame) clipEvent is the animation repeat loop used by Flash.
ballLoc._x += moveAmount will move the object by the moveAmount each time a new frame is called. The 
two IF statements (with the magic numbers) are checking to see if the object has exceeded the left or
right edge of the stage. They are set to create an object wrap so the object appears to leave one 
edge and return on the opposite edge of the stage. */

onClipEvent (enterFrame) {
   ballLoc._x += moveAmount;
   if (ballLoc._x>640) {
      ballLoc._x = 0;   
   }
   if (ballLoc._x<0) {
      ballLoc._x = 640;
   }
}

Listing 2: Language-Based Object Script

Reverse object direction.

/* This script is placed inside a button object on the stage. 
Both rollOver and rollOut use the this. reference which insures that only the object that received 
the event will have its variables altered. If multiple instances of the ball object were used without 
the this. parameter all the objects on the stage would reverse direction. storePosition is a global 
variable created on the fly. It holds the original moveAmount before temporarily setting the 
moveAmount to 0 to stop the object. rollOut restores the original moveAmount, then reverses the 
direction of the object by setting it to the opposite of its original value. */

   on (rollOver) {
      this.storePosition=moveAmount;
      this.moveAmount=0;
      }
   on (rollOut) {
      this.moveAmount = storePosition;
      this.moveAmount = moveAmount *-1;
      }

Since the programmer has taken control of the object with ActionScript, knowledge of the timeline is not needed. This animation takes place on a single frame. However, the developer does need to know how to create a button symbol since it is the only object that will respond to a rollOver event. Additionally the developer needs to know how to encapsulate the button in a movieClip symbol, since it is the only object that will accept a clipEvent.

In contrast the visual solution, presented below, utilizes the Flash timeline and its tweening capabilities to create the movement. The animation capabilities of Flash simplify the scripting.

The Visual Solution

Some might refer to the visual solution as a "designers approach" or a "non-programmers" solution, but to minimize it would be inappropriate as it demonstrates many of the features that programmers strive for when creating an algorithm. It is simple, elegant, fast and responsive, and with Flashs built-in encapsulation capabilities, it is easily reusable. Bradley Kaldahl refers to the visual algorithm presented below as "Equal Parallel Inverse Animation's," or EPI.

Description of the visual solution

Frame #            Frame #
1------------------------40 *      100-----------------140**
 >>>>>>>>>>>>>>>      <<<<<<<<<<<<<
Animate Left to Right      Animate right to left
   * = Script to loop back to frame 1
   ** = Script to loop back to frame 100

The object is animated to move from left to right (frame 1-40) using a motion tween. In frame 1 the object is on the LEFT side of the stage and in frame 40 the object is on the far RIGHT. When the animation playback-head reaches frame 40 it loops back to frame 1 using a simple "gotoAndPlay" navigation script placed on the timeline. An equal inverse animation is created further out on the timeline, in which the object is animated from right to left (frame 100-140). In frame 100 the object is on the RIGHT side of the stage and in frame 140 the object is on the LEFT. The two animation's are on the same timeline but move in opposite directions as shown in Figure 2.


Figure 2. The Animation Timeline

When the animation begins, the playback head is looping from frame 1-40, i.e., the object is moving from left to right. To capture the rollOver event and jump the playback-head to the inverse animation another simple navigation script is placed inside the object.

On RollOver
gotoAndPlay frame (x of the inverse animation)
End RollOver

It is important to note that there are two separate instances of the ball object shown in figure 2 above. Each instance of the ball object can have a different script. The first instance begins on frame 1, and the second instance begins on frame 100. (Technically speaking there are actually 4 instances of the ball because each keyframe on the timeline represents an instance. Frame 40 and frame 140 both display a one frame instance of the ball object. For the sake of discussion we will refer to the first animation as instance one and the second animation as instance two). To view the code in the first instance of the ball object you would click on frame 1 on the timeline then click on the ball object on the stage. To view the code in the second instance of the ball object, click on frame 100 on the timeline then click on the ball object on the stage.

The final question is: Which frame in the inverse animation will produce a smooth reverse?

Algorithm - Equal Parallel Inverse Animation's with Flash

Flash provides an easy-to-use property called _currentFrame, which provides the number of the frame when the event occurs. Using this property the following algorithm finds the inverse frame in the reverse animation.

    Inverse of first the first animation instance = Number of Frames in the Animation - Current Frame Number + Distance to the Reverse Animation or (NFA - CFN + D2RA = Inverse).

    Inverse of the second animation instance = Distance to the Reverse Animation - Current Frame Number + Number of Frames in the Animation or (D2RA - CFN + NFA = Inverse).

For example, using figure 2 and discussion above, if the user rolled over the object while it was on frame 23... NFA(40) - CFN(23) + D2RA(100) = Frame number 117.

By jumping the playback head to frame 117 the object appears unchanged with the exception of reversing direction of movement.

Listing 1: Visual Solution, first animation timeline loop script

Placed in frame 40 of the timeline
gotoAndPlay (1);

Listing 2: Visual Solution, second animation timeline loop script

Placed in frame 140 of the timeline
gotoAndPlay (100);

Listing 3: Visual Solution, first object instance script

Placed in the first instance of the object
// The Number of Frames in the Animation = 40;
// Distance to the Reverse Animation = 100;
on (rollOver) {
   stop ();
}
on (rollOut) {
   gotoAndPlay (40 - _currentFrame + 100 );
}

Listing 4: Visual Solution, second object instance script

Placed in the second instance of the object
on (rollOver) {
   stop ();
}
on (rollOut) {
   gotoAndPlay (100 - _currentframe + 40 );
}

Encapsulation

Encapsulation is what really makes Flash so powerful. Converting either the language or visual animation into a movieClip not only captures the animation but also encapsulates both Frame and Object scripts. Basically a movieClip becomes a self contained animated object that knows how it should behave to specific events. Once encapsulated as a movieClip multiple copies can be dropped on the stage and each plays independently. We have encapsulated and extended the code from this article to produce a couple of different web games. The games and source code can be found at www.hotFlashBook.com

Conclusion

While certain problems clearly require a language-based solution, such as score keeping in a game, a variety of other functions are more easily accomplished using the visual paradigm. To see two examples that appear fairly complex but were easily accomplished using the visual solution visit the web site listed below. The fact that these functions can be accomplished with little code and an aptitude for logical thinking makes it easy to see why many creatives are so excited about Flash. It is interesting that, when presented with a Flash problem, we both tend to look for code-based solutions first, yet on several occasions we have been delighted to discover simpler visual solutions that met our needs. One thing that is clear is that ActionScript programmers need to be familiar with Flash and its visual capabilities as well as the scripting language. An excellent resource for both novice and experienced Flash users is the book EZ Flash 5 by Bradley Kaldahl. It uses short 2-8 page hands-on projects to provide a wealth of both visual and code-based ideas and solutions. To find out more about the book EZ Flash 5 visit www.hotFlashbook.com

Acknowledgements

Special thanks to Michael Williams, from Macromedia, for your insight and information and to Suzy Ramirez, from Macromedia, for your wonderful assistance.


Bradley Kaldahl is a professor of Computer Publishing at Montgomery College and is author of the book EZ Flash 5. You can contact him through www.hotFlashBook.com

Jacob Kirk is a Designer, Teacher, Author and an all around Interactive Web "Whiz-kid". He specializes in using Macromedia Flash and Adobe LiveMotion. More information about what Jacob is up to can be found at www.EmazingMedia.com.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.