CMXtraneous

Right on the edge of useful

How to Jump Randomly to Frame Labels without Repeats

Posted Wednesday, May 30, 2007 8:30:25 AM by David Stiller

David Stiller

One of the more popular entries of this blog describes How to Jump to a Random Frame Label.  The ActionScript 2.0 involved is very straightforward, weighing in at a mere 5 lines.  Its sole purpose is to choose a random label once at the beginning, then go to it (then stop).  In the Comments section, a visitor named Heather asked for a variation in which the movie starts at a random label, then proceeds to the remaining labels in order, looping around to the beginning, if necessary, to hit each label once.  I offered some suggested code, and eventually a number of other visitors asked for yet another variation:  how to jump randomly to a whole series of labels — without repeats.  That takes a bit more code, but it’s certainly doable.  Let’s take a look.

An answer, short and sweet

This bulk of this code goes in frame 1, then one of its functions gets called at the end of each span of frames represented by a label.  Here’s the code for frame 1:

function shuffle(arr:Array):Void {
  var len:Number = arr.length - 1;
  for (var i:Number = len; i >= 0; i--) {
    var p:Number = Math.floor(Math.random() * (i + 1));
    var t:Object = arr[i];
    arr[i] = arr[p];
    arr[p] = t;
  }
}

var labels:Array = ["a", "b", "c", "d", "e"];
shuffle(labels);

var currentLabel:Number = 0;
function gotoNextLabel():Void {
  if (currentLabel < labels.length) {
    gotoAndPlay(labels[currentLabel]);
    currentLabel++;
  } else {
    stop();
  }
}
gotoNextLabel();

Gulp!  That’s a lot of code, huh?  We’ll step through it in the “How it works” section, but for the time being, just know that the labels array holds your frame labels (here, “a”, “b”, “c”, and so on — but you’ll use meaningful labels) and that you’ll obviously need corresponding frame labels in the timeline that holds this ActionScript.  If your equivalent of label “a” spans from frames 2 through 100, you’ll put a new keyframe in your scripts layer at frame 100 with the following small function call in it:

gotoNextLabel();

If your equivalent of label “b” follows, spanning from 101 to 150, you’ll put a new keyframe in your scripts layer at frame 150 and call that gotoNextLabel() function again.  Do this at the end of each span.  The end result is that the playhead will visit each and every span at random without repeats.

How it works

The ActionScript above is visually organized into three sections, just for clarity.  The first section (the custom shuffle() function) isn’t my own code, but a time-tested algorithm made popular on the Adobe forums by long-time contributor kglad.  There are a number of variations on it, and a complete discussion would probably make a good blog entry of its own, but for the time being, let the shuffle() function be a magic stone in your pocket:  it shuffles whatever array is passed to it, and it works.

The second section is comprised of two lines:

var labels:Array = ["a", "b", "c", "d", "e"];
 shuffle(labels);

In this variation on the “jump to a random frame” theme, the labels array is declared outside of any function, which makes it available to all code in this timeline.  It’s pretty clear to see what these two lines do.  The first declares an Array instance named labels and sets it to a series of arbitrary frame labels.  The second shuffles that array.  After shuffle() is called, the array might look like any one of the following sample shufflings …

b,e,a,d,c
a,e,b,c,d
b,c,e,d,a
d,c,a,e,b
e,a,c,d,b

… which is great, because that means the final section of code is pretty easy.  First, an arbitrarily named variable, currentLabel, is declared and set to zero.

var currentLabel:Number = 0;

Why zero?  Because arrays start their counting at zero rather than one.  We’re going to be starting at the first element in the array, sending the playhead to that frame label, then stepping through the remaining array elements in turn.  This is accomplished by a custom gotoNextLabel() function.

function gotoNextLabel():Void {
  if (currentLabel < labels.length) {

So far, the currentLabel variable (currently 0) is checked against the Array.length property of the labels instance.  Have we reached the end of the array?  Not yet, so the following occurs:

    gotoAndPlay(labels[currentLabel]);
    currentLabel++;

The playhead is sent to the frame label represented by labels[0] (the first element), and currentLabel is incremented by one (the ++ operator).  This will happen every time the gotoNextLabel() function is called, and eventually, the value of currentLabel will no longer be smaller than the value of labels.length.  When that happens …

  } else {
    stop();
  }
}

The playhead stops.  All spans of the timeline will have been visited, and the movie will rest comfortable in the final frame of its final randomly chosen span.  To start the whole thing off, this last line of code calls the very function just described:

gotoNextLabel();
As noted earlier, you’ll have to repeat this gotoNextLabel() call in the last frame of each span of frame labels.

Have fun with it!

Category tags: Flash

Falling Off the Face of the Earth--Into Orbit

Posted Friday, May 25, 2007 10:33:06 PM by David Stiller

David Stiller

When the concept of orbit was explained to me—this was many, many moons ago, in Canadian grade school—I was riveted. The act of orbiting Earth was, in fact, the act of falling … of falling at such a rate and angle that the falling was continuous. You didn’t ever land, because your trajectory was far enough that the planet’s curvature bent away beneath you.

How does this relate to me today?  Well, the phrase, “fallen off the face of the Earth” generally means a person has fallen out of touch, and that definitely describes my blog activity this month.  I regret that.  I’ve received quite a few emails asking if everything is all right (it is!), if I’ve decided to hang up my blogging hat (no way!), and so on.  So that explains the orbit business, because even if I’ve fallen off, I’m still around.  ;)

Not long ago, I mentioned with glee that Foundation Flash CS3 for Designers is complete, and wow, that was a naïve misstatement!  The actual writing is done, yes, but then Tom and I went through the author review process, where copyeditors tidied up our Word documents.  Currently, we’re going through PDF reviews, which is exciting, because I get to see how the pages will look when they’re actually printed.  The downside is, all of this takes time.  I’m looking forward to free evenings more than I have the energy to express.  Those free evenings are almost here!  When they are, I’ll be right back in the thick of things.  Thanks, sincerely, to everyone who has checked in on me.

Flash CS3 is in the stores now, and it’s great.  It’s bringing up all sorts of new questions on the forums, and I’m excited about the continued interest in an application I use practically every day of my life.  I’ve got a backlog of blog entries, and I’ll be back soon.

Category tags: On the Personal Side

CFUnited 2007 - Savvy Software

Posted Thursday, May 17, 2007 5:59:12 PM by Chaz

Interview with Sponsor Joshua Cyr from "Savvy Software, Inc."

Michael Smith: This time we are talking with sponsor Joshua Cyr from Savvy Software, Inc about their ColdFusion based Web Content Management software. So what brings you to CFUnited this year?

Joshua Cyr: We have been a sponsor and exhibitor for the past few years. It is a great event for us to interact with our customers and meet potential new customers. It is also a great networking and learning event for us.

MS: Tell us a little about what Savvy software does?

JC: We have a ColdFusion based web CMS. It is known for being easy to install and easy for end users to use, while also offering developers a lot of flexibility. While we do work directly with both large and small companies, we also have a strong partner program for developers who regularly install CMS's for their clients.

MS: What exactly is a CMS and why would I need one?

JC: Definitions vary but generally a web based content manager allows multiple non-technical people to update a web site in a structured and controlled way. Many developers would rather that content contributors (customers, coworkers, etc) make text and content changes. That way they may focus on more important tasks such as implementing new features. Other benefits include historical storage of content, change audit logs and ease of site build-out / design changes.

MS: I thought Content Management Systems were expensive and hard to configure?

JC: Many are! However in the CF world we have many open source and lower priced CMS options. Regarding implementation, the complexity and difficulty run the gamut. We recommend you demo a variety of CMS options so that you can find the right CMS option for your specific needs. Our aim is to keep Savvy CM low cost and easy to install while keeping it flexible for the developer. All of this without cutting any corners on delivering a quality web site. In fact many of our partners use Savvy CM because it makes building web sites faster.

MS: How about SEO (search engine optimization)? Does Savvy generate search friendly pages?

JC: Absolutely! You have full control over real page names and your directory structure as well as your title, description, etc for each page. In addition our built in navigation object is very search engine friendly. The templates can be developed with table structured HTML or with CSS positioning. We work very hard to ensure that Savvy CM doesn't present a roadblock for SEO and helps enforce good SEO techniques when possible.

MS: Joshua, we look forward to seeing you at CFUnited and recommend everyone stop by and say hello and be sure to check out Savvy Software's Content Management Solution.

You can see more interviews at http://www.cfunited.com/interviews.cfm
CFUNITED-07 is Wed 6/27/07 - Sat 6/30/07 in Bethesda MD, just outside Washington DC. It costs $1049.
For more information on CFUNITED see http://www.cfunited.com/

 

Category tags: ColdFusion

TODCon Schedule Online!

Posted Tuesday, May 08, 2007 3:33:50 AM by Stephanie

Stephanie

We've finally got the TODCon Vegas 2007 schedule and speaker list up. Check it out. Lots of excellent sessions about the new Adobe products, running your web business, usability, CSS and more. There's still time to register for the best little conference in Vegas! Come play on the strip with the rest of us geeks. You know you wanna... :)

Category tags: Adobe, Bridge, ColdFusion, CSS, Dreamweaver, Fireworks, Flash

CFUnited 2007 - Ray Camden

Posted Friday, May 04, 2007 6:34:45 PM by Chaz

Interview with Raymond Camden on "CFC Development"

Michael Smith: This time we are talking with Raymond Camden about his CFUNITED-07 talk "CFC Development". So why should a developer come to your session Raymond?

Raymond Camden: ColdFusion Components are really the main way a developer can take his or her code to the "next level" of development. In this 3 hour extended session I will cover both the syntax as well as show numerous examples and suggestions for using CFCs.

MS: How will my code be better if I use CFCs?

RC: Beginning developers tend to rebuild code they have used before. They do this because their earlier code wasn't properly abstracted, so it's not reusable. CFCs are one way within ColdFusion to reuse code.

MS: What do you mean by "abstracted" - that sounds complex!

RC: It really isn't. Here is a simple example. The total value of a shopping cart is each line items unit price times the quantity. That is one simple business rule. However - you use this logic in multiple places on your site. You "abstract" it by simply packaging it up into one file. Then every place that needs this logic calls that file. Let's then say that on Mondays you want a 10% deduction applied to the total. If you modify that one file, every place that uses it will see the new logic.

MS: So are CFC hard to learn?

RC: Like most things in ColdFusion, they are easy. Using them right though takes a bit of time. Once you learn the basic syntax, there is a lot of thinking that needs to take place on how to organize them.

MS: What tags do I need to use to write a basic CFC?

RC: There a few basic tags and functions. They are cfcomponent, cffunction, cfargument, cfproperty, cfinvoke, cfinvokeargument, createObject(), and I'm sure I forgot one or two. The CFML behind CFCs really aren't that complicated.

MS: Is there anything people need to know before they come to your talk?

RC: Nope! You can come to this session knowing nothing about CFCs and I will teach how to do it the right way from the ground up. Of course I assume you do know some basic CF code like CFOUTPUT and CFIF!

MS: Will you have any code sample or demos in your talk?

RC: Heck yes. I'm a big believer in code examples.

MS: Cool - see you at CFUNITED!

You can see more interviews at http://www.cfunited.com/interviews.cfm
CFUNITED-07 is Wed 6/27/07 - Sat 6/30/07 in Bethesda MD, just outside Washington DC. It costs $1049.
For more information on CFUNITED see http://www.cfunited.com/

CFC Development

This three hour session will delve into the basics of building ColdFusion Components (CFCs) as well as discuss various methods of CFC development. We will have many examples that you can take home with you to help you start building your first site with CFCs!

Speaker Bio:

A long time ColdFusion user, Raymond is a member of Team Macromedia and one of the managers of the Acadiana MMUG. Raymond is a co-author of the Mastering ColdFusion series published by Sybex Inc, the ColdFusion MX Developer's Handbook, and a co-author to the "ColdFusion Web Application Construction Kit". He also presents at numerous conferences and contributes to online webzines. He is a contributor and technical editor of the ColdFusion Developer's Journal. He and Rob Brooks-Bilson created and run the Common Function Library Project, an open source repository of ColdFusion UDFs. You can reach him at ray@camdenfamily.com. Raymond’s blog is at http://ray.camdenfamily.com

Category tags: ColdFusion

How to Pan the Audio in an FLVPlayback Video

Posted Tuesday, May 01, 2007 4:58:27 PM by David Stiller

David Stiller

This one came to me in a flash (ha ha … Flash!), thanks to a friendly discussion I had the other day with site visitor Michael Lokner.  He was wondering if it was possible to pan the audio portion of an FLV file in cases where the video is played in an instance of the FLVPlayback Component.  In another recent article, I explained how to use the MovieClip.attachAudio() method to control NetStream-based video, but that’s a different ball of wax.  The FLVPlayback Component has its own volume property, and even skins that feature a volume slider, but what about panning?  After bouncing ideas back and forth, Michael and I arrived at a simple answer.

An answer, short and sweet

On his own, Michael had arrived at a respectable workaround, which was to create a Sound instance associated with the main timeline (accomplished by omitting any movie clip reference when constructing the instance).  By setting up the Sound instance to control the whole movie, any panning would naturally affect the video’s audio track.  The only shortcoming is that the panning would also affect all other audio in the movie.  How to associate this panning with the FLVPlayback instance only?

The answer turned out to be effortless.  As it turns out, all ActionScript 2.0 Components, if they have a UI, are ultimately instances of the MovieClip class.  Look up any one of them in the Components Language Reference and you’ll see that it extends MovieClip.  What this means, of course, is that you can use the FLVPlayback Component itself as the parameter when creating a Sound instance.  Assuming an FLVPlayback instance name of videoPlayer:

var videoSound:Sound = new Sound(videoPlayer);
videoSound.setPan(-100) // hard left
videoSound.setPan(100) // hard right

How it works

Even though Components are very sophisticated objects, with lengthy inheritance chains, in the end, they are what they are:  movie clips.  Sound objects associated with a movie clip control that movie clip’s audio.

Category tags: Flash