我正在尝试为我的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天。
根据要求,附加代码。
SerialCommand类:
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));
}
}
}