MovieClip时间轴动画不会跨课程播放 - Flash游戏

时间:2013-09-11 17:34:04

标签: actionscript-3 flash oop model-view-controller movieclip

第一个问题。请温柔。

我正在制作一个简单的(我希望的)键盘控制的游戏角色动画片段。 Movieclip有五个框架,每个框架内部都有一个简单的动画,用于当玩家按下箭头键,上下行走,稳定等时。

我在主应用程序类文件中创建了一个简单的原型,它完美地运行。我还跟踪了currentFrame和totalFrames,这也是正确的。当按下键时,它会播放正确的帧,并且表示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添加到舞台上。

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停止播放单独的动画。它在舞台上,可以用箭头键移动,但不会播放关键帧动画。 (并且跟踪似乎认为它的播放帧为0,它也只有1帧。)

非常感谢任何帮助。我的头疼。

1 个答案:

答案 0 :(得分:0)

问题在于您的新Character课程。

让我们看看第一个完美运行的课程,你从symbol="Character"文件中嵌入了characterRes.swf,这是你的5帧影片剪辑。

现在看到第二课,那就是破了:你做同样的嵌入,现在你的5帧影片剪辑是_characterImage,而不是你创建的Character类。从OOP的角度来看,在相同的API中隐藏实现(来自库或自定义类的直接MovieClip)是正确的决定(在两种情况下,Character都在一个简单的MovicClip之外)并且可以有两种解决方案:

首先 - 尝试为孔类嵌入符号并删除当前不需要的代码:

[Embed(source="../swfs/characterRes.swf", symbol="Character")]
public class Character extends MovieClip
{
    //Public properties
    public var vx:int = 0;
    public var vy:int = 0;

    public function Character()
    {
    }
}

第二步:从swf中为Character制作Character类包装器 - 将所有MovieClip API从它传递到_character MovieClip:

package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;

public class Character extends MovieClip
{
    [Embed(source="../swfs/characterRes.swf", symbol="Character")]
    private var _characterClass:Class;

    //Private properties
    private var _character:MovieClip = new _characterClass();

    //Public properties
    public var vx:int = 0;
    public var vy:int = 0;

    public function Character()
    {
        addChild(_character)
    }

    override public function gotoAndPlay(frame:Object, scene:String=null):void
    {
        _character.gotoAndPlay(frame, scene);
    }
    override public function gotoAndStop(frame:Object, scene:String=null):void
    {
        _character.gotoAndStop(frame, scene);
    }
    override public function play():void
    {
        _character.play();
    }
    override public function stop():void
    {
        _character.stop();
    }

    //and other methods and getters for currentFrame, totalFrames
 }
}

第一种方法更简单,第二种方法更灵活,因为它是独立的图形和逻辑 - 因此您将来可以在Character构造函数中传递此链接并动态加载资源或使用Character类作为来自加载的swfs的不同MovieClip的动画控制器。