0

我正在尝试制作一个传送游戏,而我的角色需要有一些速度和重力,有人知道我需要多少总和才能完成这个吗?

到目前为止,这是我的代码:

var char = this.addChild(new Char());
char.width = 20;
char.height = 20;
char.x = startPos.x;  //startPos is an invisible movieclip that I can move around to     make the starting position
char.y = startPos.y;        // accurate
var vx:Number = 0;
var vt:Number = 0;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

function keyDownHandler (e:KeyboardEvent):void {
    switch (e.keyCode)  {
        case Keyboard.UP:
        char.y = char.y - 5   
    }
}
4

1 回答 1

1

如果你的 char 只需要上升,那么下面的代码就可以完成这项工作。

但如果它需要向各个方向移动,则需要大量高级代码。跟随各个方向的移动角色。

这是满足您需求的快速解决方案。

var gravity:Number = 2;

var velocity:Number = 1.1;

var move:Boolean = false;

function moveChar(e:Event):void
{
   if(move)
   {
       gravity *= velocity; 
       char.y -= gravity; // move char
   }
}
char.addEventListener(Event.ENTER_FRAME, moveChar, false, 0, true);


//Keyboard events
function keyDownHandler (e:KeyboardEvent):void
{
    switch (e.keyCode) 
    {
        case Keyboard.UP:
        move = true;
    }
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);

function keyUpHandler (e:KeyboardEvent):void
{
    move = false;
    gravity = 2;
}
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
于 2013-03-31T19:13:09.077 回答