第一个问题。请温柔一点。
我正在制作一个游戏角色的简单(我希望)键盘控制的电影剪辑。Movieclip 有五个帧,每个帧内部都有一个简单的动画,用于播放玩家按下箭头键、上、下、稳定等时的动画。
我在主应用程序类文件中做了一个简单的原型,它工作得很好。我还跟踪了 currentFrame 和 totalFrames,这也是正确的。按下键时,它会播放正确的帧,并显示总帧数为 5。太好了!
但是我想制作一个多关卡游戏,所以使用应用程序类作为关卡切换器,每个关卡和对象都是它自己的类。在此迁移期间,MovieClip 停止工作。它可以很好地添加到舞台上,并且可以使用箭头键移动,但不会播放单个动画。它只是在五个关键帧中的每一个中闪烁。另外,当我跟踪它时,它说 currentFrame 是 0(?),totalFrames 是 1(?)
它也不会被 .stop 或 .gotoAndPlay 控制。
请帮忙!
这是运行良好的单类版本。
package
{
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
[SWF(width="650", height="450",backgroundColor="#FFFFFF", frameRate="60")]
public class CharacterMovieclip extends MovieClip
{
//Create and initialize the vx and vy variables
public var vx:int = 0;
public var vy:int = 0;
[Embed(source="../swfs/characterRes.swf", symbol="Character")]
public var Character:Class;
public var character:MovieClip = new Character();
public function CharacterMovieclip()
{
//Add Character
stage.addChild(character);
character.x = 200;
character.y = 100;
character.gotoAndStop(1);
//Add the event listeners
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
public function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
vx = -5;
character.gotoAndStop(4);
}
else if (event.keyCode == Keyboard.RIGHT)
{
vx = 5;
character.gotoAndStop(5);
}
else if (event.keyCode == Keyboard.UP)
{
vy = -5;
character.gotoAndStop(2);
}
else if (event.keyCode == Keyboard.DOWN)
{
vy = 5;
character.gotoAndStop(3);
}
}
public function keyUpHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT
|| event.keyCode == Keyboard.RIGHT)
{
vx = 0;
character.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.DOWN
|| event.keyCode == Keyboard.UP)
{
vy = 0;
character.gotoAndStop(1);
}
}
public function enterFrameHandler(event:Event):void
{
//Move the player
character.x += vx
character.y += vy;
trace(character.currentFrame);
trace(character.totalFrames);
trace(character.x);
}
}
}
但是,一旦我在几节课上尝试过,这一切都将化为乌有。这是字符类。
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
public class Character extends MovieClip
{
[Embed(source="../swfs/characterRes.swf", symbol="Character")]
private var CharacterImage:Class;
//Private properties
private var _characterImage:DisplayObject = new CharacterImage();
private var _character:MovieClip = new MovieClip();
//Public properties
public var vx:int = 0;
public var vy:int = 0;
public function Character()
{
_character.addChild(_characterImage);
this.addChild(_character);
}
}
}
然后将角色对象添加到 LevelOne 类
package
{
import flash.display.Sprite;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class LevelOne extends Sprite
{
//Declare the variables to hold
//the game objects
private var _character:Character;
private var _background:Background;
//A variable to store the reference
//to the stage from the application class
private var _stage:Object;
public function LevelOne(stage:Object)
{
_stage = stage;
this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function addedToStageHandler(event:Event):void
{
startGame();
this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
}
private function startGame():void
{
//Create Game Objects
_character = new Character();
_background = new Background();
//Add them to stage
this.addChild(_background);
_background.x = 0;
_background.y = 0;
this.addChild(_character);
_character.x = 300;
_character.y = 50;
_character.gotoAndStop(1);
//Event listeners
_stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
_stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(event:Event):void
{
//Move the game character and check its stage boundaries
_character.x += _character.vx;
_character.y += _character.vy;
checkStageBoundaries(_character);
trace(_character.currentFrame);
trace(_character.totalFrames);
trace(_character.x);
}
private function checkStageBoundaries(gameObject:MovieClip):void
{
if (gameObject.x < 50)
{
gameObject.x = 50;
}
if (gameObject.y < 50)
{
gameObject.y = 50;
}
if (gameObject.x + gameObject.width > _stage.stageWidth - 50)
{
gameObject.x = _stage.stageWidth - gameObject.width - 50;
}
if (gameObject.y + gameObject.height > _stage.stageHeight - 50)
{
gameObject.y = _stage.stageHeight - gameObject.height - 50;
}
}
private function keyDownHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT)
{
_character.vx = -5;
_character.gotoAndStop(4);
}
else if (event.keyCode == Keyboard.RIGHT)
{
_character.vx = 5;
_character.gotoAndStop(5);
}
else if (event.keyCode == Keyboard.UP)
{
_character.vy = -5;
_character.gotoAndStop(2);
}
else if (event.keyCode == Keyboard.DOWN)
{
_character.vy = 5;
_character.gotoAndStop(3);
}
}
private function keyUpHandler(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.LEFT
|| event.keyCode == Keyboard.RIGHT)
{
_character.vx = 0;
_character.gotoAndStop(1);
}
else if (event.keyCode == Keyboard.DOWN
|| event.keyCode == Keyboard.UP)
{
_character.vy = 0;
_character.gotoAndStop(1);
}
}
}
}
并且 levelOne 由 Main 应用程序类添加到阶段。
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.display.MovieClip;
[SWF(width="650", height="450",
backgroundColor="#FFFFFF", frameRate="60")]
public class Main extends Sprite
{
private var _levelOne:LevelOne;
public function Main()
{
_levelOne = new LevelOne(stage);
stage.addChild(_levelOne);
}
}
}
在这一切的某个地方,我的 MovieClip 停止播放单个动画。它在舞台上,可以用方向键移动,但不会播放关键帧动画。(并且 trace 似乎认为它的播放帧为 0,它也只有 1 帧。)
任何帮助将不胜感激。我头疼。