获取阶段跟随玩家Y轴。 (闪存AS3) [英] Getting the stage to follow the players Y axis. (Flash As3)

查看:94
本文介绍了获取阶段跟随玩家Y轴。 (闪存AS3)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尽我最大的努力给你尽可能多的细节,我可以。

I'll try my best to give you as much detail as I can.

我创建了一个平台的游戏,我已经实现了运动和碰撞。

I have created a platform game and I have already implemented movement and collision.

我的游戏循环运行这个(我只有一个事件监听)

My game Loop runs this (I only have one eventListener)

    private function level1GameLoop():void
    {
        groundCollision();
        movementCode();
        scrollLevel();
    }

地面碰撞负责确保玩家不倒(地面是水平的整体,道歉坏命名约定(S)。

Ground collision takes care of making sure the player doesn't fall (ground is the level overall, apologies for the bad naming convention(s).

    private function groundCollision():void 
    {
        if (ground.bottom.hitTestPoint(character.x + 13, character.y, true || ground.bottom.hitTestPoint(character.x + 13, character.y, true)))
        {
            trace("h");
            while (ground.bottom.hitTestPoint(character.x + 13, character.y, true || ground.bottom.hitTestPoint(character.x + 13, character.y, true)))
            {
                character.incrementUp();
                if (ground.bottom.hitTestPoint(character.x + 13, character.y, true || ground.bottom.hitTestPoint(character.x + 13, character.y, true)))
                {
                }
                else{
                    character.keepOnGround();
                }
            }
        }
    }

,然后你有你的典型的运动code

and then you have your typical movement code

        else if (rightKey)
        {
            character.moveRight();
            lookingRight = true;
            lookingLeft = false;
        }
        else if (leftKey)
        {
            lookingLeft = true;
            lookingRight = false;
            character.moveLeft();
        }

在滚动的水平,我测试,看看是否我的角色打所有的平台,是的,他的确。

Before scrolling the level, I tested to see does my character hit all of the platforms and yes he does.

所以,现在我想实现滚动。

So now I want to implement scrolling.

在类的定义我已经建立了一个名为容器变量,它是一个精灵。

In the class definition I have set up a variable called container which is a sprite.

    private var container:Sprite = new Sprite();

我已经开始游戏循环之前再加入我的孩子上了舞台

I have then added my children on to the stage before starting the game loop

        addChild(container);
        container.addChild(ground);
        container.addChild(character);
        addChild(timerGIF);//this does not need to scroll 

我已经然后再次测试游戏,并且碰撞能正常工作。

I have then tested the game again, and the collision works fine.

现在我已经尝试了许多尝试滚动播放,并让他在屏幕中心确保碰撞仍然正常工作。

Now I have tried many attempts to scroll the player and keep him on center of the screen making sure that the collisions still function correctly.

私有函数scrollLevel():无效         {

private function scrollLevel():void {

        /*
        _camera.x = character.x - stage.stageWidth * 0.5;
        _camera.y = character.y - stage.stageHeight * 0.5;
        //"camera" must not show areas outside of the game world
        if (_camera.x < 0) 
        {
            _camera.x = 0;
        }
        //Right
        if (_camera.x > (container.width) - stage.stageWidth) 
        {
            _camera.x = (container.width) - stage.stageWidth;
        }
        //Bottom
        if (_camera.y > (container.height) - stage.stageHeight) 
        {
            _camera.y = (container.height) - stage.stageHeight;
        }
        //Top
        if (_camera.y < 0) 
        {
            _camera.y = 0;
        }

        container.scrollRect = _camera;
        */

        //ground.x = -character.x + stage.stageWidth / 2;
    //  ground.y = -character.y + stage.stageHeight / 2;


            /*var offset:int = 10;
            var distance:Number = character.x - ((stage.stageWidth / 2) + offset);
            var distance2:Number = character.y - ((stage.stageHeight / 2));
            var ease:int = 5;
            if (distance < 0)
            {
                distance *= -1;
            }
            if (distance2 < 0)
            {
                distance2 *= -1;
            }
            if (character.x < (stage.stageWidth / 2))
            {
                var variable:int = distance / ease;
                //character.x += variable;
                container.x += variable;



            }
            if (character.x > (stage.stageWidth / 2))
            {
                var variable2:int = distance / ease;
                //character.x -= variable2;
                container.x -= variable2;


            }

            /*if (character.y < (stage.stageHeight / 2))
            {
                var variable3:int = distance2 / ease;
                character.y += variable3;
                //container.y += variable3;



            }

            if (character.y > (stage.stageHeight / 2))
            {
                var variable4:int = distance2 / ease;
                character.y -= variable4;
                //container.y -= variable4;

            }*/

            /*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.
            ground.x = -camera.x + (stage.stageWidth / 2);
            ground.y = -camera.y + (stage.stageHeight / 2);*/

    }

所有这些似乎工作,因为碰撞然后得到搞得一团糟。

None of these seem to work, since the collision then gets all muddled up.

我的意思是,我的性格最终浮动,或下降到

By this I mean, my character ends up floating, or falling through

其他的事情提了,地上是一个影片剪辑,基本上是我的水平(再次,可怜的命名约定,apoligiez)。

Other things to mention, ground is a movie clip and is basically my level(once again, poor naming conventions, apoligiez).

在地上有则h​​itTest框的和对象的角色会得到。

Inside ground there are hitTest box's and objects that the character will get.

推荐答案

您正在做一项伟大的工作,但在非常困难的办法,首先第一件事情,你应该知道,当你想你的性格在容器内移动某一个方向,你要调整容器向相反方向移动的所有其他孩子,所以它给人的感觉是,在所需方向上的角色移动..这是我的朋友是硬件的方式,我认为原因让你走那条路是你想要 timerGIF 来保持没有改变所有的时间..但是这可以在一个非常更简单的方法通过扭转你的code来实现这样的...

You are doing a great job, but in the very difficult way, first things first, you should know that when you want your character to move inside the container in a certain direction, you have to adjust all the other children of the container to move in the opposite direction, so it gives the feeling that the character moves in the desired direction.. this my friend is the hard way, and I think the reason made you go that way is that you want timerGIF to stay not changed all the time.. but this can be achieved in a very more easier way by reversing your code like that...

//reverse your strategy for coding:

var container:Sprite = new Sprite();
addChild(container);
addChild(ground);
addChild(character);
container.addChild(timerGIF);

//define some very imp vars that you will need to adjust for each level to
//prevent recoding in each level, just set the vars to the new level properties

var level_width:Number=1600;
var level_hight:Number=1200;
var screen_width:Number=800;
var screen_hight:Number=600;

//the ease of camera is the speed of camera, and whoever you want the camera to
//follow just pass it as a camera_target (that will be in this case your character)

var camera_speed:Number=5;
var camera_target:Object=charachter;

//then add your scroll function (named hear as camera();)

function camera(wide, high, target, speed) {
  if (target!=null) {
      cam_x = ((screen_width/2)-target.x-x)/speed;
      cam_y = ((screen_hight/2)-target.y-y)/speed;
      x+=cam_x;
      y+=cam_y;
  }
  if (x>0) {
      x=0;
  }
  if (x<wide*-0.5) {
      x=- wide/2;
  }
  if (y>0) {
      y=0;
  }
  if (y<high*-0.5) {
      y=- high/2;
  }
  //now the code to keep container and all its children not scrolled is just:
  container.x=- x;
  container.y=- y;
}

最后把这个下面的行放在 level1GameLoop()替换 scrollLevel();

function level1GameLoop():void
{
    groundCollision();
    movementCode();
    //scrollLevel();
    camera (level_width, level_hight, camera_target, camera_speed);
}

有一个愉快的一天,先生......

Have a nice day sir...

这篇关于获取阶段跟随玩家Y轴。 (闪存AS3)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆