4 October 2010
This article is the second of seven in a series highlighting examples of programming strategies that make it easier than ever before to create rich content in Adobe Flash CS5 Professional. Adobe Flash Player 10 introduced the ability to control parts of an animation using inverse kinematics (IK). Now, using Flash CS5, you can apply the effect of a spring to an IK armature.
The Developer Center article titled Exploring the Spring tool in Flash Professional CS5 explains how to add and control the spring's motion. In order to control an IK armature with ActionScript 3, you must first create the animation elements in the Flash authoring environment.
The following sample movie is designed as a bare bones test, so that is easier to understand how to set and control the properties. In a real world example, you can use any number of elements in the armatures you create.
Notice that when you are working in the authoring environment, you could set the value for Spring by entering a numeric value in the Spring section at the bottom of the Property inspector; leave this value set to 0.
A Spring is assigned to a reference of an IK joint obtained from the IK bone. The process for controlling IK joints with ActionScript is essentially the same as in Flash CS4. First, you'll use the IKManager.getArmatureByName() method to get an IKArmature object, in order to pass in the IK Armature's name. Next, call the IKArmature.getBoneByName() method with the IKArmature object referrer and pass an IK bone's name to the method as an argument. The method will return the reference of the IKBone object. Finally, you can manipulate the IKJoint object by setting its IKBone.headJoint and IKBone.tailJoint properties.
At this point, a spring can be assigned to the IKJoint object. As you saw in Figure 4, the Spring section in the Property inspector for an IK bone contains two controls, Strength and Damping. Using ActionScript, two properties are available to set the values: IKJoint.springStrength and IKJoint.springDamping respectively.
// frame action // IK Armature: myArmature // IK Bone: ikBoneBar import fl.ik.IKManager; import fl.ik.IKArmature; import fl.ik.IKBone; import fl.ik.IKJoint; var ikNodeBar:MovieClip; var myArmature:IKArmature = IKManager.getArmatureByName("myArmature"); var myBone:IKBone = myArmature.getBoneByName("ikBoneBar"); var myJoint:IKJoint = myBone.tailJoint; myJoint.springStrength = 50; myJoint.springDamping = 50;
In the code above, a spring was assigned to an IKJoint of the IKBone object. Because the IK armature was set to Runtime, the instance of the head joint can be dragged in the published SWF file.
If you drag the instances too quickly, the movement may appear unnatural. After testing your animation, you may choose to make the armature more complex by setting some restrictions to the joint.
As you can see from these examples, there are many new possibilities to explore when developing ActionScript 3 projects in Flash Professional CS5. Hopefully the scripts provided in these examples will serve as the starting point as you begin experimenting with the new events and capabilities for delivering rich content with interactivity.
Be sure to check out my other articles in this series:
To learn more about developing with ActionScript 3, see the following online resources:
Also be sure to visit the Flash Developer Center to find more articles and sample files to help you take your Flash projects to the next level.