0

我正在尝试为我的 Flash 游戏创建介绍动画,但是当它应该转到动画的第二帧时出现问题。

这是菜单中的开始游戏按钮应该执行的代码(位于文档类中):

gotoAndStop(3);

animationHero1.addEventListener(Event.ENTER_FRAME, resetWalkAnimation);

var command1:Command =
    new SerialCommand(0, //start animation
        new TweenLiteToCommand(0, animationHero1, 6, { x: 1300, ease:Linear.easeNone } ), //move hero to door
        new ChangeFrameCommand(0, animationHero1, 1), //stop hero animation
        new ParallelCommand(0,
            new PlaySoundCommand(0, new OpenDoorSound), //open the door
            new TweenLiteToCommand(0, animationHero1, 1, { alpha: 0 } ) //fade out hero
        ),
        new ChangeFrameCommand(0.5, this, 4), //next frame
        new TraceCommand(1, this.currentFrame.toString()) //trace current frame
        //new TweenLiteToCommand(0.5, this.animationHero2, 3, { x: 1100, ease:Linear.easeNone } ) //Walk hero across the room
    );
    command1.start();

如您所见,我正在使用命令模式。changeFrameCommand 将“this”设置为第 4 帧。但是,当我在下一个命令中跟踪 currentFrame 时,输出为“3”。这没有任何意义,因为当我运行程序时帧确实会更改为第 4 帧。以下现在已注释掉的命令随后会抛出“无法访问空对象引用的属性或方法”,因为 animationHero2 位于第 4 帧中。

我认为问题最可能的地方是changeFrameCommand,所以这是该类的代码:

    package commands  
    {
import commands.Command;
import flash.display.MovieClip;
import flash.display.Stage;

/**
 * ...
 * @author Berry Hermans
 */
public class ChangeFrameCommand extends Command 
{
    private var mc:MovieClip;
    private var frame:int;

    public function ChangeFrameCommand(delay:Number, movieclip:MovieClip, frameNumber:int) 
    {
        super(delay);
        mc = movieclip;
        frame = frameNumber;
    }

    override protected function execute():void
    {
        mc.gotoAndStop(frame);
        complete();
    }

}

    }

如果有人能对这种情况有所了解,我将永远感激不尽,因为我的截止日期是 2 天。

根据要求,附加代码。

串行命令类:

    package commands {
import flash.events.Event;

public class SerialCommand extends Command {

    private var _commands:Array;

    public function SerialCommand(delay:Number, ...commands) {
        super(delay);
        _commands = commands;
    }

    private var _completeCommandCount:int;

    override final protected function execute():void {

        //set the complete command count to zero
        _completeCommandCount = 0;

        //listen for the complete event of the first subcommand...
        _commands[0].addEventListener(Event.COMPLETE, onSubcommandComplete);

        //...and start the subcommand
        _commands[0].start();
    }

    private function onSubcommandComplete(e:Event):void {

        //stop listening for the complete event
        Command(e.target).removeEventListener(Event.COMPLETE, onSubcommandComplete);

        //increment the complete command count
        _completeCommandCount++;

        //if all the commands are complete...
        if (_completeCommandCount == _commands.length) {

            //...then this serial command is complete
            complete();
        } else {

            //...otherwise listen for the complete event of the next subcommand...
            _commands[_completeCommandCount].addEventListener(Event.COMPLETE, onSubcommandComplete);

            //...and start the subcommand
            _commands[_completeCommandCount].start();
        }
    }
}
    }

命令类:

    package commands {
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.TimerEvent;
import flash.utils.Timer;

public class Command extends EventDispatcher {

    private var _timer:Timer;

    public function Command(delay:Number = 0) {
        _timer = new Timer(int(1000 * delay), 1);
        _timer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
    }

    private function onTimerComplete(e:TimerEvent):void {
        execute();
    }

    /**
     * Starts the command.
     * Waits for the timer to complete and calls the execute() method.
     * This method can be used directly as an event listener.
     */
    public final function start(e:Event = null):void {
        _timer.start();
    }

    /**
     * The abstract method for you to override to create your own command.
     */
    protected function execute():void {

    }

    /**
     * Completes the command.
     * Dispatches a complete event.
     * This method can be used directly as an event listener.
     */
    protected final function complete(e:Event = null):void {
        dispatchEvent(new Event(Event.COMPLETE));
    }
}
    }
4

0 回答 0