尝试使主mc跳转,代码不能在AS3上工作

时间:2012-12-23 16:03:20

标签: actionscript-3

我想要做的是让mcMain跳跃,但是当我尝试游戏时我的代码似乎不起作用(我的mc会水平移动)。我被告知要尝试在eFrame()函数中运行mainJump()函数。但是,由于我是初学者,我完全不知道这意味着什么(特别是因为英语不是我的第一语言)。如果您能告诉我要添加或更正的内容,我将非常感激!所以我的代码是这样的:

var mainSpeed:int = 25; //how fast the character move side to side
var mainJumping = false; //whether or not main is in the air
var jumpSpeed:Number = 0; //how quickly he's jumping at the moment
var jumpSpeedLimit:int = 25; //how quickly he'll be able to jump

addEventListener(Event.ENTER_FRAME, eFrame);

function eFrame(e:Event):void{
    //making the character follow the mouse
    if(mouseX > mcMain.x + 25){ //if the mouse is to the right of mcMain
        mcMain.x += mainSpeed;//move mcMain to the right
    } else if (mouseX < mcMain.x - 25){//same thing with the left side
        mcMain.x -= mainSpeed;
    } else {
        mcMain.x = mouseX;//if it's close enough, then make it the same x  value
    }
}


stage.addEventListener(MouseEvent.CLICK, startJump);//if the user clicks

function startJump(e:MouseEvent):void{//then run this function
    if(!mainJumping){//main isn't already jumping
        mainJumping = true;//then we can start jumping
        jumpSpeed = jumpSpeedLimit*-1;//change the jumpSpeed so that we can   begin jumping
    }
}

function mainJump():void{
    if(mainJumping) {//if jumping has been initiated
        if(jumpSpeed < 0){//if the guy is still going up
            jumpSpeed *= 1 - jumpSpeedLimit/120;//decrease jumpSpeed slightly
            if(jumpSpeed > -jumpSpeedLimit*.1){//if jumpSpeed is small enough
                 jumpSpeed *= -1;//then begin to go down
            }
        }
        if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){//if main is going down
            jumpSpeed *= 1 + jumpSpeedLimit/120;//incrase the falling speed
        }
        mcMain.y += jumpSpeed;//finally, apply jumpSpeed to mcMain
        //if main hits the floor, then stop jumping
        if(mcMain.y >= 387.5){
            mainJumping = false;
            mcMain.y = 387.5;
        }
    }
    if(mcMain.y > 387.5){
        mcMain.y = 387.5;
    }
}

1 个答案:

答案 0 :(得分:0)

您的问题是,您实际上从未在mainJump函数中运行代码。这一行:

addEventListener(Event.ENTER_FRAME, eFrame);

确保每帧都运行eFrame函数,但它没有说明何时运行mainJump函数。幸运的是,您可以在mainJump函数中调用eFrame函数,如下所示:

function eFrame(e:Event):void {
    // ... original code ...
    mainJump();
}

这将在每一帧运行mainJump功能,因此它应该允许你的玩家跳跃。