PDA

View Full Version : [AS3] Make screen follow the player


FTLRalph
05-13-2010, 01:24 AM
Hey guys.

Just looking for some input from other AS3 programmers.

When making a typical game (a platformer for example), what is the best method of having the screen "follow" the player, like a camera?

What are some of the common ways to accomplish this?

Just curious to see what the norm is, thanks!

canarybirdfly
05-13-2010, 08:56 AM
Do some research into a V-Cam, theres loads of tuts on youtube :)

FTLRalph
05-13-2010, 12:53 PM
Hey, thanks for the reply.

I should have mentioned that I'm looking for alternatives to the vCam. I'm making a game and I've read (and experienced) that the vCam should not be used with games. IN my case, it produced a huge amount of lag (for a reason I still don't understand, but when I took the vCam out, everything was fine).

So yeah, I'm curious into what are some alternatives to that.

NoobsArePeople2
05-13-2010, 05:54 PM
Hey guys.

Just looking for some input from other AS3 programmers.

When making a typical game (a platformer for example), what is the best method of having the screen "follow" the player, like a camera?

What are some of the common ways to accomplish this?

Just curious to see what the norm is, thanks!

Download flixel (http://flixel.org) and/or flashpunk (http://flashpunk.net) and check out how they do it.

FTLRalph
05-14-2010, 04:09 PM
Thanks for the input.

Not a bad idea, I've always wanted to check out Flixel anyway.

But I'm also hoping for just some simple answers from fellow AS developers. I mean, getting the screen to follow an object has to be one of the most general things in almost every Flash game.

NoobsArePeople2
05-14-2010, 04:20 PM
I spose there are many ways you can do it but this is what I've generally done.

Define the size of the screen as a Rectangle. Define the size of the world as a Rectangle. Assume the player will always be in the center of the screen (there are exceptions to this, but this is the base assumption).

On each tick of the engine update the position of the screen based on the player's location. Test if this screen then exceeds the bounds of the world. If it does, clamp the screen to the world bounds. Clamping the screen is the exception to the player being dead center on the screen.

FTLRalph
05-14-2010, 11:54 PM
On each tick of the engine update the position of the screen

Yeah, that's the part I'm having a problem with. How would I go about updating the position of the screen? (Thanks for all the help btw)

NoobsArePeople2
05-15-2010, 01:45 AM
Yeah, that's the part I'm having a problem with. How would I go about updating the position of the screen? (Thanks for all the help btw)

Yeah, so my game is an AS Project in Flex Builder -- the details may vary depending on how your project is setup.

In my case, my main application class extends Sprite. So I simply update it's x, y to match the camera's each tick of the engine. It helps to think of it this way: the entire game world is added to the stage and the application Sprite is a window into this world.

FTLRalph
05-15-2010, 03:35 AM
Ah, Flex Builder, hm. Don't think that'll help me where I'm at right now.

So far I've given vCam another chance and for some reason it breaks my game. The player doesn't register collisions with walls and instead collides with those same walls elsewhere in the level (they're invisible and not where they're supposed to be).

There's no lag though... So if I could just figure out why the vCam would be doing this...

PS - This bug only happens in Flash Player 10.1 and not 9 or 10.0. Since 10.1 is still in beta, can it be a bug in the Flash Player and not on my end?

Durnus
05-15-2010, 03:33 PM
On a very basic level, you can set the stage (or some other movieclip containing all game elements) to have x = -player.x and y = -player.y, plus some offset. You can also scale that movieclip to zoom and stuff, but that's a bit more complex.

I've never had to play with vCam or any other things like that, and Flixel's pretty nice too if you want to get into it. But, I don't know how to fix either. :p

FTLRalph
05-17-2010, 12:50 AM
Hm, I get what you're saying, but I'm don't think I understand completely. How do I have a movieclip "contain" other movieclips?

Also, I've tried setting stage.x = player.x but I get an error. I don't think you can set the stage x/y properties.

Durnus
05-17-2010, 05:00 PM
Quick fix: don't set the stage's x and y, just change the document's. Just say x=-player.x and y=-player.y. (This is the same as this.x = -player.x, this.y = -player.y, because of the object-oriented nature of AS3.)

Ok, containing objects. Containing other objects is a very fundamental ability of Flash. If you know about nesting movieclips, then you know what I mean, although you might not know it.

For example, if you have a player, and within that player object you have other movie clips for the arms and legs, then this is the same thing. By putting the players, enemies and other stuff within a giant "game" movie clip, you can move it ALL around.

Examples in code. When you call addChild, which I will assume you have, you are calling this.addChild, which adds the specified movieclip as a child/inside the movieclip "this," which in the case of timeline code refers to a movieclip inside the stage. So you can say player.addChild(head) and you'd be putting a head into the player movieclip. Similarly, you could say level.addChild(player), which would put the player in a level. This is what you need to do.

And remember that negative! It's not a mistake!