RTL_Shadow
1023 posts
|
How would I go about instituting a camera into a game? It’s a top-down game using the standard flash display list. I want the player to stay in the middle.
|
|
|
UnknownGuardian
8142 posts
|
A Scrolling Container? Throw everything that moves in it. Then the HUD would be outside of it.
|
|
|
RTL_Shadow
1023 posts
|
Originally posted by UnknownGuardian:
A Scrolling Container? Throw everything that moves in it. Then the HUD would be outside of it.
How would this keep the player in the middle?
|
|
|
UnknownGuardian
8142 posts
|
You write code that makes the container’s x and y position at a place so the character is in the center?
I used something like this in a recent game, though I only required vertically centering it:
public function centerOn(s:Sprite):void
{
_activeLayer.x = -s.x + Main.WIDTH / 2;
_activeLayer.y = -s.y + Main.HEIGHT / 2;
}
|
|
|
RTL_Shadow
1023 posts
|
Originally posted by UnknownGuardian:
You write code that makes the container’s x and y position in the center?
I want to be able to do:
player.x = 400;
But, keeping the player in the middle, just moving ‘background’.
|
|
|
UnknownGuardian
8142 posts
|
I edited my last post with code.
To do something like that you might be able to override the x and y functions (not sure if that is possible) with getters and setters. i.e. public function get x():Number
Otherwise, you’ll have to use code similar to my above post.
|
|
|
jerimo
1037 posts
|
I guess if you made the setter for your positioning able to read the position of the to left corner of your camera view you could simply make player.x = 400 + camera.x; adn the same for the y?
|
|
|
DrYoshiyahu
678 posts
|
add everything except the character to the “worldHolder” as I call it, and if the player presses the left key, move the worldHolder right.
It will appear as if the world is moving under the character’s feet.
Alternatively, you can add the character to the worldHolder and when the player presses the left key, move the character left AND the worldHolder right. That way the character is literally moving left.
|
|
|
RTL_Shadow
1023 posts
|
Current:
private function center( p:Sprite):void
{
bg.x = -p.x - stage.stageWidth / 2;
bg.y = -p.y - stage.stageHeight / 2;
}
Now this leaves the problem with the player still moving when I change the x..
|
|
|
UnknownGuardian
8142 posts
|
trace(stage.stageWidth); everytime you call center()
|
|
|
RTL_Shadow
1023 posts
|
Originally posted by UnknownGuardian:
trace(stage.stageWidth); everytime you call center()
I traced it and it works fine every time, still 640. I’m not sure why that would change. If I move player.x then of course it’s going to move across the stage.
|
|
|
Senekis93
4090 posts
|
Parenthesis.
(bg.x=-p.x-stage.stageWidth)*.5;
|
|
|
BraydenBlack
271 posts
|
Originally posted by Senekis93:
Parenthesis.
(bg.x=-p.x -stage.stageWidth)*.5;
Will that even run, lol?
|
|
|
Senekis93
4090 posts
|
Originally posted by BraydenBlack:
Originally posted by Senekis:
Parenthesis.
(bg.x=-p.x -stage.stageWidth)*.5;
Will that even run, lol?
Derp… bg.x=(-p.x-stage.stageWidth)*.5;
I’m not sober. u.u
Ayway, for that matter, yes, it would run.
|
|
|
RTL_Shadow
1023 posts
|
Originally posted by Senekis93:
Originally posted by BraydenBlack:
Originally posted by Senekis:
Parenthesis.
(bg.x=-p.x -stage.stageWidth)*.5;
Will that even run, lol?
Derp… bg.x=(-p.x-stage.stageWidth)*.5;
I’m not sober. u.u
Ayway, for that matter, yes, it would run.
Allllright none of this is working out.
|
|
|
Senekis93
4090 posts
|
Well, the actual code to center something is object.x=(container.width-object.x)*.5;
|
|
|
RTL_Shadow
1023 posts
|
Originally posted by Senekis93:
Well, the actual code to center something is object.x=(container.width-object.x)*.5;
I understand that but I don’t get how I can center it and then if I do player.x++ have it stay the same but the background move.
E: What I basically want to do is have essentially a VCam where I can move the player but still have it in the center.
|
|
|
Senekis93
4090 posts
|
Move the background x instead.
If i’ ++player.x, do --background.x The players doesn’t move; everything else does.
|
|
|
RTL_Shadow
1023 posts
|
Originally posted by Senekis93:
Move the background x instead.
If i’ ++player.x, do --background.x The players doesn’t move; everything else does.
I fixed it now. Not sure I like how I did it, but the player has two sets of variables, realX and realY, and then the regular x and y.
public function center():void
{
this.x = Main.WIDTH / 2;
this.y = Main.HEIGHT / 2;
realX = this.x;
realY = this.y;
}
private function update(e:Event):void
{
player.update();
bg.x = -player.realX - Main.WIDTH / 2;
bg.y = -player.realY - Main.HEIGHT / 2;
player.realY++;
}
|
|
|
qwerber
4717 posts
|
LOOP START:
move player and update player using default display object coordinate variables.
set the position of the container in such a way that the player ends up being in the center of the screen.
not that hard, this makes the players .x and .y variables legit in the game world, and just moves the outer container to show the player what they need to see.
|
|
|
Dealmaster13
641 posts
|
Ideally you want to separate all game world objects (e.g. a player and the world it interacts with) from non-game-world objects (e.g. an HUD).
If you follow this approach, then simply have the player and background along with other game world objects in one container, which I assume you have implemented by now, and ensure that the player-game-world relationship is cohesive1 by moving the player coordinates by (deltaX, deltaY) and moving the game world container coordinates by (-deltaX, -deltaY), thus leaving the player position relative to the stage unchanged2.
The player’s coordinates are relative to the game world, and the player’s coordinates relative to the stage should never be necessary unless if the player is susceptible to external interactions3.
I apologise if this is the way you noted that you didn’t like (just posting my thoughts on the matter without reading other suggested solutions due to tiredness :( ).
1 This may be important in scenarios where you are performing abstract collision detection between say the player and platforms to jump on or projectiles to get hit by
2 This ignores game world transformations such as rotating and scaling, but these can be taken into account without difficulty when moving the game world objects container
3 Such as mouse clicking, which is actually the case in my project, but is handled easily using a variety of possible methods – some purely mathematical, others graphical
|
|
|
JohnnyBohnny
113 posts
|
You could move the entire root, as long as the hud moves along with it.
|