首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动作脚本3.平台游戏物理故障(重力)

动作脚本3.平台游戏物理故障(重力)
EN

Stack Overflow用户
提问于 2014-04-24 06:13:09
回答 1查看 231关注 0票数 0

当我的球员接触到地板时,他不能移动,因为他在地板上,并被递增。

在主课上,我有一个动作

代码语言:javascript
复制
    private function processMovement():void
    {
        if (touchingGround)
        {   
            if (upKey)
            {
                character.jumpUp();
            }   

            if (leftKey)
            {
                character.moveLeft();
            }

            if (rightKey)
            {
                character.moveRight();
            }

            if (!leftKey && !rightKey && !upKey)
            {
                character.dontMove();
            }

        }
    }

然后,在character类中,您将看到以下内容。

代码语言:javascript
复制
public class player extends OnGround 
{
    public var canJumpAn:Boolean;
    public var attackAn:Boolean;
    public  var jumpheight:Number = 18;

    public function player() 
    {
        addEventListener(Event.ADDED_TO_STAGE, onAdd);

    }

    private function onAdd(e: Event): void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, onAdd);
    }

    public function moveLeft():void 
    {
        //decrease VELOCITY
        xV -= 2;
        if (xV > -7)
        {
            xV = -7;
        }           
        this.gotoAndStop("run");

        //add this to the Mc . x pos baby. 
        this.x += xV;

        this.scaleX = -1;
        charIsrunning = true;
    }


    public function moveRight():void 
    {
        //increase VELOCITY
        xV += 2;
        if (xV > 7)
        {
            xV = 7;
        }
        this.gotoAndStop("run");

        //add this to the Mc . x pos baby. 
        this.x += xV;
        this.scaleX = 1;
        charIsrunning = true;
    }



    public function dontMove():void 
    {
        //if no button presses then do this 
        this.gotoAndStop("stop");

        //slowd down ball 
        xV *= friction;
        charIsrunning = false;
        isDefending = false;

        //stop the ball if you're not moving 
        if (xV > - 1 && xV < 1)
        {
            xV = 0;
        }
    }

    override public function positionOnLand():void
    {
        isJumping = false;
        ///=gotoAndStop(1);

    }

    public function defend():void 
    {
        isDefending = true;
        this.gotoAndStop("defend");
        charIsrunning = false;
    }

    public function attack():void 
    {
        this.gotoAndStop("attack");
    }

    public function jumpUp():void 
    {

        if (!isJumping)
        {
            isJumping = true;
            this.gotoAndStop("jump");
            //
        }

    }




}

}

正如您所看到的,OnGround是player扩展的另一个类。

代码语言:javascript
复制
public class OnGround extends MovieClip
{
    public var grav:Number;
    public var friction:Number;
    public var xV:Number;
    public var yV:Number;

    protected var charIsrunning:Boolean;
    protected var isDefending:Boolean;
    protected var isJumping:Boolean;

    public function OnGround() 
    {
        addEventListener(Event.ADDED_TO_STAGE, init)
        charIsrunning = false;
        isDefending = false;
        //gotoAndStsop("jump");
    }

    private function init(e:Event):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);

        //gravity
        grav = 0.6;

        //y velocity 
        yV = 0;

        //x velocity 
        xV = 0;

        //
        friction = 0.9;
        addEventListener(Event.ENTER_FRAME, fall);

    }

    private function fall(e:Event):void 
    {
        //add grav to y VELOCITY 
        yV += grav;
        trace(yV);


        this.y += yV

    }

    public function incrementUp():void 
    {
        this.y -= 0.1;
        //trace("incrementing");
    }

    public function keepOnGround():void 
    {
        //trace("onGroundBitch");
        grav = 0;   
        yV = 0;
        positionOnLand();
    }

    public function positionOnLand():void 
    {
        //overide
    }

}

}

这是一个在主类中的函数

代码语言:javascript
复制
        for (var c:int = 0; c < childrenOnStage; c++)
        {

            if (getChildAt(c).name == "player")
            {
                if (ground.level1Ground.hitTestPoint(getChildAt(c).x + 13, getChildAt(c).y, true) || ground.level1Ground.hitTestPoint(getChildAt(c).x - 13, getChildAt(c).y, true))
                {
                    getChildAt(c).y --;

                    //OnGround(getChildAt(c)).incrementUp();
                    OnGround(getChildAt(c)).keepOnGround();

                    touchingGround = true;

                }
                else
                {
                    touchingGround = false;
                }

问题是,当玩家接触地面时,它的y位置增加,直到它不接触地面,然后它应该保持在地面上,通过将grav设置为= 0,将y速度设置为0。

这意味着重力被关闭,球员的y位置不会向上移动(当我跳跃时),或者当下降时保持在地面上。

如果有人能帮我一把或给我指明正确的方向,我将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2014-04-24 22:14:05

我不确定为什么每个对象都有一个重力值,除非你真的想让不同的对象实例具有单独的重力。

如果不是,我会引用一个重力常数。

反重力是这个游戏的一个特点吗?如果不是,那么我会避免将重力设置为0。它可能“工作”(在一定程度上),但它不是一个好的现实模型/抽象。

相反,我要做的是分离作用力和当前的动量:

重力(向下)在任何时候都会被施加。

当你站在地面上时,一个与重力相反的向上的力被施加。当您跳跃时,添加一个一次性的向上力实例。

使用当前力来更改当前动量,并使用当前动量来更改位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23256192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档