Action Script 3,如何让角色跳跃?

Action Script 3, how to make characters jump?

我正在 Flash Develop as3 中创建一个简单的 2D 平台游戏,但我不知道如何为我的角色创建一种跳跃方式。

这是我的代码 main.as:

package 
{
 import flash.display.Bitmap;
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.events.KeyboardEvent;
 import flash.events.MouseEvent;

/**
 * ...
 * @author Harry
 */
public class Main extends Sprite 
{
    public var StartButton:Go;
    public var FireBoy:Hero;
    public var WaterGirl:Female;
    public var Door1:Firedoor;
    public var Door2:Waterdoor;

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    public function init(e:Event = null):void
    {
        StartButton = new Go();
        addChild(StartButton);
        StartButton.addEventListener(MouseEvent.CLICK, startgame);
    }

    private function startgame(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point
        removeChild(StartButton);

        FireBoy = new Hero ();
        stage.addChild(FireBoy);
        FireBoy.y = 495;
        //This allows movement for FireBoy
        stage.addEventListener(KeyboardEvent.KEY_DOWN, HandleHeroMove);

        WaterGirl = new Female();
        stage.addChild(WaterGirl);
        WaterGirl.x = 70;
        WaterGirl.y = 495;
        stage.addEventListener(KeyboardEvent.KEY_DOWN, HandleFemaleMove);

        Door1 = new Firedoor();
        stage.addChild(Door1);
        Door1.x = 5;
        Door1.y = 62;

        Door2 = new Waterdoor();
        stage.addChild(Door2);
        Door2.x = 100;
        Door2.y = 62;

        graphics.beginFill(0x804000, 1);
        graphics.drawRect(0, 0, 800, 40);
        graphics.endFill();

        graphics.beginFill(0x804000, 1);
        graphics.drawRect(0, 170, 600, 40);
        graphics.endFill();

        graphics.beginFill(0x804000, 1);
        graphics.moveTo(800, 200);
        graphics.lineTo(800, 700);
        graphics.lineTo(400, 700);
        graphics.lineTo(100, 700);
        graphics.endFill();

        graphics.beginFill(0x804000, 1);
        graphics.drawRect(0, 580, 800, 40);
        graphics.endFill();
    }

    //This handles FireBoys movement
    public function HandleHeroMove(e:KeyboardEvent):void
    {
        trace(e.keyCode);
        //This is for moving to the left
        if (e.keyCode == 37)
        {
            FireBoy.x = FireBoy.x - 30;
            Check_Border();
        }
        //This is for moving to the right
        else if (e.keyCode == 39)
        {
            FireBoy.x = FireBoy.x + 30;
            Check_Border();
        }
        else if (e.keyCode == 38)
        {
            FireBoy.grav = -15;
        }

    }

    //This handles WaterGirls movement
    public function HandleFemaleMove (e:KeyboardEvent):void
    {
        trace(e.keyCode);
        //This is for moving to the left
        if (e.keyCode == 65)
        {
            WaterGirl.x = WaterGirl.x - 30;
            Check_Border();
        }
        //This is for moving to the right
        else if (e.keyCode == 68)
        {
            WaterGirl.x = WaterGirl.x + 30;
            Check_Border();
        }
        else if (e.keyCode == 87)
        {
            WaterGirl.grav = -15;
        }
    }

    //This stops characters from leaving the screen
    public function Check_Border():void
    {
        if (FireBoy.x <= 0)
        {
            FireBoy.x = 0;
        }
        else if (FireBoy.x > 750)
        {
            FireBoy.x = 750;
        }
        if (WaterGirl.x <= 0)
        {
            WaterGirl.x = 0;
        }
        else if (WaterGirl.x > 750)
        {
            WaterGirl.x = 750;
        }
    }

}
}

这是我的 Hero.as 代码(我有相同的 Female.as 代码):

package  
{
import flash.display.Bitmap;
import flash.display.Sprite;
/**
 * ...
 * @author Harry
 */
public class Hero extends Sprite
{
    [Embed(source="../assets/FireBoy.jpg")]
    private static const HeroFireBoy:Class;
    private var FireBoy:Bitmap;
    public var grav:int = 0;
    public var floor:int = 580;

    public function Hero() 
    {
        FireBoy = new Hero.HeroFireBoy();
        scaleX = 0.1;
        scaleY = 0.1;

        addChild(FireBoy);
    }
    public function adjust():void 
    {
        FireBoy.y += grav;
        if(FireBoy.y+FireBoy.height/2<floor)
            grav++;
        else 
        {
            grav = 0;
            FireBoy.y = floor - FireBoy.height / 2;
        }
        if (FireBoy.x - FireBoy.width / 2 < 0)
            FireBoy.x = FireBoy.width / 2;
        if (FireBoy.x + FireBoy.width / 2 > 800)
            FireBoy.x = 800 - FireBoy.width / 2;
    }

}

}

请您准确地建议我应该编写什么代码以及将代码放在哪里,因为我找不到任何有用的东西,而且我对此感到非常压力。

一种方法是这样的:在你的 Hero.as 中,你的功能类似于跳跃、move_left 和 move_right 并保持坐标在英雄 class 中.然后在实例化 Hero 对象的任何地方调用这些方法。

作为您的 main.as 中的示例,您可以在按下 space 条或向上箭头键使您的英雄跳跃时执行此代码:

var hero:Hero = new Hero();
hero.jump();

如果是简单的Y轴跳转,那么只需要改变Y轴的值即可。如果既是X轴又是Y轴,比如向前跳跃,那你可以考虑用三角正弦函数来计算你的角色应该在哪个点上移动,这样看起来更流畅。

您不需要完全相同的 Female.as class。你应该做的是:创建一个 class(可能命名为 Character)。添加跳转,move_left,move_right功能到这个class。然后在Hero.as和Female.as中继承Characterclass。 (您需要 Google "inheritance" 才能了解更多信息)。然后您可以使用驻留在 Character class.

中的相同代码实例化 Hero 和 Female

您可能还需要在 Hero.as 和 Female.as 中覆盖:

[Embed(source="../assets/FireBoy.jpg")]
private static const HeroFireBoy:Class;

在您的 Hero.as 和 Female.as 中分配正确的图像。