AS3: Viewports without an end

2019-08-29 13:02发布

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.

1条回答
小情绪 Triste *
2楼-- · 2019-08-29 14:07

What I do here is:

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

The MapCamera has five properties:

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

    • 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.

The getters are pretty basic:

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

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);
}

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.

查看更多
登录 后发表回答