Friday, October 4, 2013

Camera Controls in Flash

I was going to work more on the physics system, but we ran into some issues with our camera class, so I took some time to figure that out.  There's a little bit left to do, but it seems to be working well so far.  The big problem with Flash cameras is that coordinate systems become overly complicated.  Flash uses local coordinate systems, so objects embedded within other objects reset what their 0,0 position is.

There are a couple of ways to fix this.

point = clip.localToGlobal(point); //This code will allow you to convert a local coordinate system to the actual position on screen.

localToGlobal has some issues in that it always defaults to the top layer.  So if you have an object nested down three layers, and you want it's position on the first layer, you run into issues.

If you can, you want all of your objects to be working on the same layer, that way you don't have to deal with that huge mess.

We have a single camera class that doubles as our world class.  All of the code for the game acts in this movie clip, so we don't need to worry about conversions.  When we do need to worry about conversions, I reference back to the world class so that, again, all of our code is running in the same coordinate system and we don't need to worry about too much math.  I'm not going to go into too much detail, but I use both localToGlobal and then the reverse method globalToLocal to get the coordinates.  Because all of the game objects have references to the world class, we can use that as an interpreter for everyone's different positions.  All any one individual object needs to worry about is letting the world class know where it is, and making sure it goes through the world class whenever it references any other object's position.

In terms of the camera (world) class itself, we have fairly standard controls.  The camera can grab any movieClip and center itself on that clip.  Internally, it re-centers every update loop.  We can also set scale, etc... although most of that code will probably be written directly within the game's core update loop, rather than in a separate method.

The camera itself doesn't use localToGlobal.  It manually calculates the changes in coordinate systems.  The problem is that localToGlobal changes as the camera adjusts itself.  Doing the manual conversions provides a lot more consistency.

There's still a lot to do - looping the world around, deciding how objects are updated, etc... still needs to be coded.  But we have a good base to do that now.

Finally, I'm experimenting with parallax effects.  I don't have everything up and running yet, but what the camera should be able to do eventually is grab different layers (background, foreground) and adjust them accordingly.  From what I've been seeing with my experiments, having objects and their backgrounds move at the exact same pace is somewhat disorienting, so I'm pretty keen on trying to get that working before too long.


No comments:

Post a Comment