Archive for July, 2008

July 31st, 2008

Creating buttons that link to different scenes, and within a scene, using ActionScript 3.0

by Jen deHaan

A common request in the comments is for information on how to create a button that links to a scene using ActionScript 3.0 in Flash CS4 or CS3. Luckily, it’s largely the same as creating a normal button that links to whatever, and uses the same gotoAndPlay format as in ActionScript 2.0. So hopefully it’ll make sense once you see it.

So if you’re unsure on general button code, see this post here: Making a button work in AS3. More links about writing button code at the end of this post.

Now you’re ready for some scenes-with-buttons action. Here’s some code:

stop();button1.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);function mouseDownHandler(event:MouseEvent):void {
 
gotoAndStop(1, "Scene 2");
 
}

So that code assumes that you have a button with the instance name button1, and a scene in your document called Scene 2 in the Scenes panel. The number "1" in that code points to frame 1 of the scene.

So what if you want to, say, link to a particular frame? Let’s link to frame 5 of a scene called monkeyScene:

stop();button2.addEventListener(MouseEvent.MOUSE_DOWN, monkeyDownHandler);
 
function monkeyDownHandler(event:MouseEvent):void {
 
gotoAndStop(5, "monkeyScene");
 
}

Hope that answers the question. I’ve put an FLA file online that links each scene using buttons so you can see this in action.

Linking to buttons within a scene

There have also been questions about linking to frames within a scene. I’m not positive what the specific problem is, but I’ll take a stab at it (or at least show what seems to work for me.

Now, what causes many of the problems with Scenes in Flash (and why they are officially discouraged from use in Flash best practices – I never use ‘em unless I have next to no code in a FLA), is as follows. From what I understand, when you publish your SWF file, all of the scenes in a FLA are made into one big giant string of frames. Kind of like sticking all of your scenes into one big timeline (one big scene, essentially) and pushing it out as a SWF. Therefore, this can mess up your code. If you’re pointing to a frame 56… which one? To you it may be frame 56 of scene 3, but to Flash that may be frame 56 of the giant new timeline in the SWF that’s published.

So, it’s good to avoid scenes.

But I digress. So if you’re linking to buttons within the same scene, you could specify what scene you’re pointing to in each button. So if you’re in a scene called monkeyScene, you could make your button that’s on frame 1 point to frame 2 of monkeyScene as follows:

stop();
 
mbutton1.addEventListener(MouseEvent.MOUSE_DOWN, monkeyHandler1);
 
function monkeyHandler1(event:MouseEvent):void {
 
gotoAndStop(2, "monkeyScene");
 
}

I’ve uploaded another real ugly FLA file that links multiple buttons to frames within a single scene. Check out monkeyScene in the FLA file.

MORE INFORMATION ON BUTTONS:

July 29th, 2008

Set Transformation Point and Reset Transformation Point in Flash CS3

by Jen deHaan

When you right-click a symbol instance on the Stage in Flash, there are a couple new options that were added in CS3: “Set Transformation Point” and “Reset Transformation Point”. There’s not much out there about these features, as I understand they were added quietly late in the game. But they’re very useful nonetheless. The problem is it’s a bit tricky to figure out how these things are useful without knowing what the feature is or how it works – because (at least to me) it sounds like they do something when they actually do something a bit different (and once I found out what they did, it made a lot of sense). So lets run through this new feature.

  1. Drag an instance of a symbol to the Stage from the Library.
  2. Select the instance using the Free Transform tool.
  3. Drag the transformation (the white circle) to a new location on the instance.
  4. Right-click the instance and choose Set Transformation Point.
  5. Drag a new instance of that symbol to the Stage, and select it with the Free Transform tool.

As you can see, the Transformation pointof the second instance is exactly where you placed it in the first one. That’s essentially what this first option does, and it can be very handy if you’re using a bunch of instances in a FLA that need to rotate or otherwise transform from the same point – saves a lot of dragging/estimating.

As for Reset Transformation Point-

  1. Right-click the instance again, and choose Reset Transformation Point.
  2. Drag another instance onto the Stage, and select it with the Free Transform Tool.

Now the Transform point is back in the default central position for the new instances of this symbol. Alternatively, you can change the Transformation point location, and just choose Set Transformation Point again and it will save this new location for the instances.

This feature appears to work on a per-symbol basis. You can set the transformation point for all instances of a particular symbol for multiple symbols in the library. For example, you have Symbol 1 and Symbol 2 in the library. You can set the transformation point for an instance of Symbol 1, and all instances of that symbol will keep that transformation point location until you reset it or set a new location. You can independently set a transformation point for Symbol 2 and it is saved until you reset it on an instance of that symbol. And so on.

As an aside/note/whatever, remember that you can set the Transformation point to the Registrationpoint by double-clicking the white circle.

If you’re new to Flash and you’re wondering what the heck a Transformation point is – it is the point around which the symbol rotates or transforms, and looks like a white circle when you have the instance selected using the Free Transform tool. The best way you can illustrate this is by creating a new instance, and rotate it using the Free Transform tool (select the instance using the tool, and move the cursor around the edge until you see a little round arrow and then drag). Now change the location of the white circle and rotate the instance again. This also affects how the instance skews and resizes. The Transformation point has also been referred to as a “Control point” in Flash (some references in documentation are to the Control point), but it is different than the Registration point (the small black crosshair).