Actionscript 3 动作脚本3,如何让角色跳跃?

Actionscript 3 动作脚本3,如何让角色跳跃?,actionscript-3,flashdevelop,2d-games,Actionscript 3,Flashdevelop,2d Games,我正在Flash Develop as3中创建一个简单的2D平台,但我不知道如何为我的角色创建一种跳跃方式 这是我的main代码。如: package { import flash.display.Bitmap; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; /** * ... *

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

这是我的main代码。如

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;
        }
    }

}
}
包
{
导入flash.display.Bitmap;
导入flash.display.Sprite;
导入flash.events.Event;
导入flash.events.KeyboardEvent;
导入flash.events.MouseEvent;
/**
* ...
*@作者哈里
*/
公共类Main扩展了Sprite
{
公共var开始按钮:开始;
公共消防队员:英雄;
女:女;
公共门1:防火门;
公共门2:水门;
公共函数Main():void
{
if(stage)init();
else addEventListener(Event.ADDED_TO_STAGE,init);
}
公共函数init(e:Event=null):void
{
StartButton=新Go();
addChild(开始按钮);
StartButton.addEventListener(MouseEvent.CLICK,startName);
}
私有函数startName(e:Event=null):无效
{
removeEventListener(Event.ADDED_TO_STAGE,init);
//入口点
removeChild(开始按钮);
消防员=新英雄();
舞台:addChild(消防员);
消防员y=495;
//这允许消防员移动
stage.addEventListener(KeyboardEvent.KEY_DOWN,handleRomove);
WaterGirl=新女性();
舞台:addChild(WaterGirl);
WaterGirl.x=70;
水上女孩y=495;
stage.addEventListener(KeyboardEvent.KEY_DOWN,handleFemalMove);
门1=新的防火门();
阶段。添加儿童(门1);
门1.x=5;
1.y=62;
门2=新的水门();
阶段:addChild(门2);
Door2.x=100;
2.y=62;
图形。beginll(0x804000,1);
graphics.drawRect(0,0,800,40);
graphics.endFill();
图形。beginll(0x804000,1);
graphics.drawRect(0、170、600、40);
graphics.endFill();
图形。beginll(0x804000,1);
图形。移动到(800200);
图形。lineTo(800700);
图形。lineTo(400700);
图形。lineTo(100700);
graphics.endFill();
图形。beginll(0x804000,1);
graphics.drawRect(0580800,40);
graphics.endFill();
}
//这是处理消防员运动的
公共功能HandleRoMove(e:键盘事件):无效
{
跟踪(如键码);
//这是用来向左移动的
如果(e.keyCode==37)
{
FireBoy.x=FireBoy.x-30;
检查_Border();
}
//这是为了向右移
否则如果(e.keyCode==39)
{
消防员.x=消防员.x+30;
检查_Border();
}
否则如果(e.keyCode==38)
{
FireBoy.grav=-15;
}
}
//这就是水上女孩运动
公共函数HandleFemalMove(e:键盘事件):无效
{
跟踪(如键码);
//这是用来向左移动的
如果(e.keyCode==65)
{
WaterGirl.x=WaterGirl.x-30;
检查_Border();
}
//这是为了向右移
否则如果(e.keyCode==68)
{
WaterGirl.x=WaterGirl.x+30;
检查_Border();
}
否则如果(e.keyCode==87)
{
WaterGirl.grav=-15;
}
}
//这将阻止字符离开屏幕
公共函数检查_Border():void
{
如果(消防队员x 750)
{
消防员x=750;
}
如果(WaterGirl.x 750)
{
WaterGirl.x=750;
}
}
}
}
这是我的英雄代码。as
(我有相同的女性代码。as):

包
{
导入flash.display.Bitmap;
导入flash.display.Sprite;
/**
* ...
*@作者哈里
*/
公共类英雄扩展精灵
{
[Embed(source=“../assets/FireBoy.jpg”)]
私人消防队员:班级;
私人消防队员:位图;
公共变量grav:int=0;
公共var楼层:int=580;
公共职能英雄()
{
FireBoy=新英雄。HeroFireBoy();
scaleX=0.1;
scaleY=0.1;
addChild(消防员);
}
公共函数adjust():void
{
消防员。y+=重力;
如果(FireBoy.y+FireBoy.height/2800)
FireBoy.x=800-FireBoy.width/2;
}
}
}

请你确切地建议我应该写什么代码以及把它放在哪里,因为我找不到任何有用的东西,而且我为此感到非常紧张

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

如main.As中的示例,当按下空格键或向上箭头键使您的英雄跳跃时,您可以执行以下代码:

var hero:Hero = new Hero();
hero.jump();
如果是Y轴上的简单跳跃,则只需更改Y轴值。如果它是X轴和Y轴,就像向前跳跃一样,那么你可以考虑三角正弦函数来计算你的角色应该移动的点以使它看起来平滑。 你不需要完全相同的女性。您应该做的是:创建一个类(可能是命名字符)。将jump、move_left、move_right函数添加到此类。然后继承Hero.as和femal.as中的角色类。(你需要谷歌“继承”来了解更多信息)。然后,您可以使用驻留在Character类中的相同代码实例化Hero和Femal

您可能还需要在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;
    }

}

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

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

完成的代码对于跳跃来说是什么样子的?你尝试编写代码怎么样?如果不起作用,我将尝试帮助你。也许我对这个问题的回答会有帮助: