且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动作脚本3.当角色移动时更改动画

更新时间:2023-11-01 12:34:04

gotoAndPlay() 函数采用框架索引( int )或框架标签( String )参数,而不是另一个影片剪辑的名称。

The gotoAndPlay() function takes either a frame index (int) or a frame label (String) parameter, not the name of another movie clip.

您想要做的事情可以通过几种方式完成 - 在我看来最简单的是设置一个 MovieClip ,其中包含您的角色作为单独关键帧的所有动画。然后,您可以使用唯一标签为每个关键帧添加标签,然后使用 gotoAndStop() 功能在动画之间切换。

What you're trying to do can be accomplished in several ways - in my opinion the simplest is to set up one MovieClip that contains all the animations of your character as individual keyframes. You can then label each keyframe with a unique label and then use the gotoAndStop() function to switch between animations.

首先,将每个字符动画创建为单独的 MovieClip - 您不需要导出这些(除非您需要以编程方式访问它们)。完成后,创建一个新的 MovieClip ,并为您制作的每个动画添加关键帧。所以如果你的角色有4个动画(例如 walking idle1 idle2 $

First, create each character animation as a separate MovieClip - you don't need to export these (unless you need to programmatically access them). Once this is done, create a new MovieClip and add a keyframe for each of the animations you made. So if your character has 4 animations (for example walking, idle1, idle2, running) you'd create 4 keyframes in this clip.

将每个动画在一个关键帧(仔细对齐它们,以便当你切换字符不会跳跃),然后标记每个关键帧与适当的动画名称。从库中导出此影片剪辑。您可以通过单击时间轴中的关键帧,然后在属性窗口中添加标签名称来标记关键帧。一旦框架标签就位,框架将具有小的红色标记。以下是Flash CS 5中标签的屏幕截图(图片编辑后仅显示相关部分):

Put each of the animations in one of the keyframes (carefully align them so that when you switch the character won't jump) and then label each keyframe with the appropriate animation name. Export this movie clip from the library. You can label a keyframe by clicking the keyframe in the timeline and then adding a label name in the Properties window. Once the frame label is in place, the frame will have a small red flag. Here's a screenshot with the labels in Flash CS 5 (the image is edited to show only relevant parts):

完成此操作后,您现在可以创建一个您的角色并通过调用以下方式设置正确的动画状态:

Once this is done, you can now create an instance of your character and set the correct animation state by calling:

...
Hero.gotoAndStop("walking");
...

您还可以使用 Hero.gotoAndStop (1); 如果步行动画是在关键帧1,但如果你改变动画的顺序,你必须更新所有的代码,如果你使用帧索引。使用框架标签是非常灵活的。

You could also use just Hero.gotoAndStop ( 1 ); if the walking animation is on keyframe 1 but if you change the order of your animations you'd have to update all the code if you use frame indexes. Using frame labels is a lot more flexible.

您需要使用 gotoAndStop()指定的关键帧并保持在该帧中显示动画。如果你使用 gotoAndPlay(),你只会看到所有状态动画的第一帧作为Flash循环通过关键帧。

You need to use gotoAndStop() to let Flash stay on the specified keyframe and keep showing the animation in that frame. If you used gotoAndPlay() you'd only see the first frame of all state animations as Flash cycles through the key frames.