Flash动作脚本3 - 移动播放器周围的一切(平台游戏)

时间:2014-04-22 20:20:22

标签: actionscript-3 flash flash-cs3

我可以向左和向右滚动地面,但我无法上下滚动地面。

私有函数scrollStage():void         {

        if (lastPosX != lastPosX)
        {
            canScrollStage = false;
        }
        else if (lastPosX == lastPosX)
        {
            canScrollStage = true;
        }

        if (canScrollStage)
        {
            if (rightKey)
            {
                //move background left
                //something.x +=(stage.stageWidth * 0.5) - character.x * 2;
            }
            else if (leftKey)
            {
                //move backgrounf Roight 
            }
            for (var b:int = 0; b < childrenOnStage; b++)
            {
                if (getChildAt(b).name == "enemy" || getChildAt(b).name == "enemyRed" || getChildAt(b).name == "knife")
                {
                    getChildAt(b).x += (stage.stageWidth * 0.5) - character.x;
                        //getChildAt(b).y += - character.y;
                }
            }
            ground.x += (stage.stageWidth * 0.5) - character.x;
            //ground.y += (stage.stageHeight * 0.5) - character.y;

                //ground.y += (stage.stageHeight) - character.y;
        }
        else
        {
            //move the background 
        }

        // do this last, everything moves around object
        character.x = stage.stageWidth * 0.5;

        lastPosX = character.x;

我试过了             character.y = stage.stageHeight * 0.5;

但是这只是将字符y位置粘贴到屏幕的中心。

当世界可以滚动时,我尝试将其实现到我的代码中                 //ground.y + =(stage.stageHeight * 0.5) - character.y;

但这也不起作用。

任何人都可以请我指出相同的方向或给我提示和建议,我已尝试实施此代码

     // A 'Camera' is really just a Point within the world we want to center on
     // the screen.
         var camera:Point = new Point();

     // Set the camera coordinates to the char coordinates.
         //camera.x = character.x;
         camera.y = character.y;

     // Adjust the world position on the screen based on the camera position.
        // world.x = -camera.x + (stage.stageWidth / 2);
        ground.y = -camera.y + (stage.stageHeight / 2);

确实有效!但屏幕猛烈地上下摇晃

我跟踪了球员的位置并且它上下移动

但是没有那个代码,y位置保持不变。

谢谢。

编辑 - 重力

在我的主要课程中,我有这个可以阻止玩家摔倒。

        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))
                { //if the boundary collides with the player or enemy
                    while (ground.level1Ground.hitTestPoint(getChildAt(c).x + 13, getChildAt(c).y, true) || ground.level1Ground.hitTestPoint(getChildAt(c).x - 13, getChildAt(c).y, true))
                    {
                        OnGround(getChildAt(c)).incrementUp();
                        //bump up the object until it isn't hitting the boundary;
                        if (ground.level1Ground.hitTestPoint(getChildAt(c).x + 13, getChildAt(c).y, true) || ground.level1Ground.hitTestPoint(getChildAt(c).x - 13, getChildAt(c).y, true))
                        {
                            // do nothing
                            //touchingGround = false;
                        }
                        else
                        { // once it isn't hitting the boundary, do this function for keeping the object on the boundary
                            OnGround(getChildAt(c)).keepOnGround();
                            touchingGround = true;
                        }

                    }
                        //  ends while (  _boundaries.hitTestPoint ( getChildAt(c).x , getChildAt(c).y, true) ) {;
                }
                else
                {
                    // ends if ( _boundaries.hitTestPoint ( getChildAt(c).x , getChildAt(c).y, true) )
                    touchingGround = false;
                }

这是onGround课程。

public class OnGround extends MovieClip
{
    protected var grav:int =  1;
    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);
        grav = 1;
        addEventListener(Event.ENTER_FRAME, fall);

    }

    private function fall(e:Event):void 
    {
        grav ++;
        if (grav > 15)
        {
            grav = 10;
        }

        this.y += grav;

    }

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

    public function keepOnGround():void 
    {
        grav = 0;   
        positionOnLand();
    }

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







}

}

我担心camera.y无法跟上character.y或character.y的变化,尽管代码告诉游戏&#34;停止下降,你的y轴正在被调整。&#34; < / p>

2 个答案:

答案 0 :(得分:1)

瞥了一眼代码,我会把它作为一个可能的答案。看起来即使玩家的Y位置发生最轻微的变化,相机的Y位置也会发生变化。 (为什么X上没有发生这种情况比我好。)

首先想到的是我们需要让相机的灵敏度降低很多。我们可以通过强制函数使用位置的估计来实现此目的。 (顺便说一句,只有当角色没有粘在屏幕中心时,这才会起作用。)

由于我没有整个项目,我无法测试。如果此行无法解决问题,请使用概念。

camera.y = Math.round(character.y);

基本上,我会强制相机使用整个Y值,而不是十进制值。从理论上讲,这可以减少一些震动。

如果有所帮助,但还不够,我们可以实现这一点,使其基于十个像素,而不是一个像素。再次,未经测试(数学可能稍微偏离),但你明白了。

camera.y = Math.round(character.y/10) * 10;

第二种方法的一个理论缺点是屏幕会在重新调整时跳转。您可能需要进行一些额外的编码才能使其顺利进行。

答案 1 :(得分:1)

这是一个潜在的答案,我所做的是将这段代码更改为......

              /*if (ground.level1Ground.hitTestPoint(getChildAt(c).x + 13, getChildAt(c).y, true) || ground.level1Ground.hitTestPoint(getChildAt(c).x - 13, getChildAt(c).y, true))
                { //if the boundary collides with the player or enemy
                    getChildAt(c).y --;
                    while (ground.level1Ground.hitTestPoint(getChildAt(c).x + 13, getChildAt(c).y, true) || ground.level1Ground.hitTestPoint(getChildAt(c).x - 13, getChildAt(c).y, true))
                    {
                        //bump up the object until it isn't hitting the boundary;
                        if (ground.level1Ground.hitTestPoint(getChildAt(c).x + 13, getChildAt(c).y, true) || ground.level1Ground.hitTestPoint(getChildAt(c).x - 13, getChildAt(c).y, true))
                        {

                            // do nothing
                            //touchingGround = false;
                        }
                        else
                        { // once it isn't hitting the boundary, do this function for keeping the object on the boundary
                            OnGround(getChildAt(c)).keepOnGround();
                            touchingGround = true;
                        //  getChildAt(c).y --;
                        }

                    }
                        //  ends while (  _boundaries.hitTestPoint ( getChildAt(c).x , getChildAt(c).y, true) ) {;
                }
                else
                {
                    // ends if ( _boundaries.hitTestPoint ( getChildAt(c).x , getChildAt(c).y, true) )
                    touchingGround = false;
                }
                */

此!我删除了一会儿!

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的位置,因此没有更多的地震意味着龙可以现在睡吧

向JasonMC92致信,为我提供帮助我实现这一目标的每一步都是我现在的解决方案。