AS3:没有尽头的视口 [英] AS3: Viewports without an end

查看:26
本文介绍了AS3:没有尽头的视口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个太空导航游戏.所以它从宇宙飞船上的用户开始,然后当他按下向上键时,飞船前进,地图"总是不同的,我有 5 个不同的恒星和 2 个不同的行星,所以它们基本上是随机产生"的,而用户导航.我可以进行键检测,电影剪辑生成器代码,但我不知道如何制作导航代码,我的意思是如何在用户按下键时使视口移动,......我已经看到了我不太明白的代码,这家伙基本上创建了一个巨大的电影剪辑,根据按下的键移动.这在我的情况下不起作用,因为我希望它随机生成所有内容,并且当用户按下向下箭头时,我希望它返回,并使用与之前相同的地图".请帮助我,我对所有这些视口的事情都感到困惑.而且,我希望游戏运行得很快,我对动作脚本有点陌生,我不知道如果渲染未显示的对象,它是否会变重,如果是这样,一个简单的 'obj.visible = false' 有效吗?提前致谢.

I'm making a space navigation game. So it starts with the user on the spaceship and then when he press the up key the ship goes forward, the 'map' is always different, I have 5 variations of stars and 2 variations of planets, so they basically 'spawn' randomly while the user navigates. I can make the key detection, the movie clips generator code, but I don't know how do I make the navigation code, I mean how do I make the viewport move when the user press the key, ... I've saw a code that I didn't understand too well that the guy basically created a giant movie clip that moves according to the key that was pressed. That won't work in my case because I want it to generate everything randomly and when the user press the down arrow, I want it to go back, with the same 'map' that he was before. Please help me out guys I'm totally confused with all this viewport thing. And also, I want the game to run fast, I'm kind of new to the Action Script, and I don't know if it gets heavy if you are rendering objects that are not being displayed, if so will a simple 'obj.visible = false' works? Thanks in advance.

推荐答案

我在这里做的是:

创建一个带有属性 cameraMap 类,这是另一个自定义类 MapCamera.

Create a Map class with a property camera which is another custom class MapCamera.

MapCamera 有五个属性:

  1. _x
  2. _y
  3. map - 对拥有此 MapCamera
  4. Map 实例的引用
  5. offsetX
  6. offsetY

  1. _x
  2. _y
  3. map - a reference to the instance of Map owning this MapCamera
  4. offsetX
  5. offsetY

  • 偏移值表示距屏幕左边缘和上边缘的 x 和 y 间距,应将其设置为舞台宽度和高度的一半,以便相机正确地居中.
  • _x_y 属性是 private,并且有 getter 和 setter.
  • The offset values represent the x and y spacing from the left and top edges of the screen, which should be set to half of the stage width and height so that the camera will centre on the stage correctly.
  • The _x and _y properties are private, and have getters and setters.

getter 非常基础:

The getters are pretty basic:

public function get x():Number{ return _x; }
public function get y():Number{ return _y; }

setter 是将改变视口的地方,如下所示:

The setters are where the viewport will be altered, like so:

public function set x(n:Number):void
{
    _x = n;
    map.x = -(_x + offsetX);
}

public function set y(n:Number):void
{
    _y = n;
    map.y = -(_y + offsetY);
}

从这里,您将您的孩子添加到 Map 容器中,然后可以简单地:

From here, you add your children into the Map container and then can simply go:

map.camera.x = player.x;
map.camera.y = player.y;

这将导致玩家始终位于屏幕中央.

Which will cause the player to always be in the centre of the screen.

这篇关于AS3:没有尽头的视口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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