Action Script 3. Change animations when character moving

痴心易碎 提交于 2019-12-11 23:36:48

问题


I'm creating simple flash game. I need to change animations when character moving. For example when going, running, jumping, attacking etc. I have different animations. Just I don't know how to add them correctly?

In Library I have created my character, It named "Hero" As Linkage too "Hero". I need double click It and in Hero's timeline create new Layers with names run, walk, attack, etc... and then put here animations? Or I need in Library create name and As Linkage "HeroRun", "heroAttack" etc...?

As I know I need to use later something like Hero.gotoAndPlay(run); but this doesn't work for me. Sorry for these newbie questions, but I really need your help, thank you.

For example this is my part of code for turn left:

    if(left){
                Hero.x_speed = -walkspeed;
                setDirection(1);
                Hero.gotoAndPlay(run);// I don't know how to use It correctly
}

UPDATE

How It looks now: Created MovieClip character Hero

Created layers hit and going

And for now I need to use Hero.gotoAndStop("hit"); if want to see "hit" animation?

UPDATE 2 All right, I did that, but now I have problem there is no animations only change image after I use Hero.gotoAndStop("attack");, but here must be animation. Here is photo how attack MovieClip looks like:

Here' my Hero screenshot.It have 2 keyframes Attack and Going.

And here is hero > going timeline's screenshot:

UPDATE:

Here is my code for space button (jumping character) when It show animation on the ground I have big lags I think that because checking for collisions and when on the air animation playing without lag.

    if(Hero.y_speed>0 && myCollisionList.checkCollisions().length > 0 ){
        Hero.y_speed=0;
        Hero.x_speed=0;

        if(space){


            if (ground.hitTestPoint(Hero.x + 28, Hero.y+20, true)){

            Hero.gotoAndStop("attack");
            stop();
            Hero.y_speed = -20;

And here is collision list:

        var myCollisionList:CollisionList = new CollisionList(Hero);
        myCollisionList.addItem(ground);
        myCollisionList.addItem(ground3);
        myCollisionList.addItem(ground5);
        myCollisionList.addItem(ground4);

回答1:


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

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.

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.

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");
...

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.

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.



来源:https://stackoverflow.com/questions/18880774/action-script-3-change-animations-when-character-moving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!