【问题标题】:character is bouncing when hits the ground. it does not stay stable角色落地时弹跳。它不稳定
【发布时间】:2014-02-07 23:39:19
【问题描述】:

问题很明确。角色在舞台上稳定,背景和地面在移动。但是当角色落地时,它会一次又一次地弹跳。这是代码。我该如何解决?当我移动角色时,它的工作很完美,但是当移动物体是地面弹跳问题时会发生。 这是问题所在:http://www.swfcabin.com/open/1391814250

public function controller():void
    {
        if (rightKey || leftKey || jumpKey)
        {
            if (rightKey)
            {
                if (jumpWalk || canJump)
                {   
                    hero.gotoAndStop(2);
                    hero.scaleX = 1;
                    xSpeed -=  speed;
                }
            }
            if (leftKey)
            {
                if (jumpWalk || canJump)
                {   
                    hero.gotoAndStop(2);
                    hero.scaleX = -1;
                    xSpeed +=  speed;
                }
            }

            if (jumpKey && canJump)
            {   
                ySpeed +=  15;
                canJump = false;
            }

        }
        else
        {
            hero.gotoAndStop(1);
        }
        if(!canJump){
            hero.gotoAndStop(3);
        }

         ySpeed -= gravity;

        if(ySpeed >10){
            ySpeed = 10;
        }
        else if(ySpeed < -10){
            ySpeed = -10;
        }
        if(xSpeed>10){
            xSpeed = 10
        }
        else if(xSpeed < -10){
            xSpeed = -10;
        }
        xSpeed *= 0.8;

        level.x += xSpeed;
        level.y += ySpeed;

    }// controller function


public function loop(event:Event):void
    {
        controller();
        while(level.hitTestPoint(hero.x , hero.y + hero.height/2 -1 - ySpeed , true)){
            trace(ySpeed +"dasd");
            ySpeed ++;
            canJump = true;

        } }

【问题讨论】:

  • 你能制作一个关于发生了什么或什么的 GIF 吗?我真的无法想象这应该是什么样子。
  • 这里是 .swf:swfcabin.com/open/1391814250
  • 嗯,你能给我们看一段trace(ySpeed)输出的内容吗?

标签: actionscript-3 platform hittest


【解决方案1】:

现在正在发生的事情是:当您嵌入地形时,您正在增加 ySpeed(向上动量),直到一个“滴答”将角色拉出地面。但是后来人物是向上飞的,因为你增加了他们向上的动力。在重力将它们拉回原位之前,它们仍然具有向上的动力,因此它们将继续“弹跳”。要解决此问题,请执行以下操作:

代替ySpeed ++; 在主循环中,尝试level.y++; 这表示当英雄嵌入时将其拉出地形(或者实际上将地形拉下,因为你的角色是静止的),而不是改变他的把他赶出去的动力。

您还应该在其中添加ySpeed = 0;。这意味着当你落地时失去所有的 y 动量。

所以你的主循环应该是这样的:

public function loop(event:Event):void
    {
        controller();
        while(level.hitTestPoint(hero.x , hero.y + hero.height/2 -1 - ySpeed , true)){
            trace(ySpeed +"dasd");
            level.y++;
            ySpeed = 0;
            canJump = true;
        } }

【讨论】:

  • 它不起作用。当我更改 ySpeed = 0 时,flash 自行终止。
  • 您遇到了无限循环,请参阅我更新的答案。 --,不是 ++。我没注意到你的 ySpeed 倒退到了坐标系。
  • 非常感谢!我明白事实。它解决了我的问题,但现在地面和英雄以 ySpeed -1 值向上移动。
  • hero.y--; 更改为level.y++;,抱歉,没有意识到角色是静止的。这应该可以做到这一切,真的。耶。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-25
  • 2023-03-18
相关资源
最近更新 更多