Camera and player movement


(Elias) #1

I have put a camera to follow my player wherever he goes. Not a big deal, but everytime my player moves to a direction the image moves a bit to that direction (sometimes start flickering and moving between the camera’s center and a position of the direction) but like a normal entity, it’s keep moving to the direction i choose. Anybody had something similar?


(azrafe7) #2

From the description you’re probably forgetting to reset the camera speed, but… Please show some code.


(Elias) #3

Sure… here you go.

FP.camera.x = x - FP.halfWidth;FP.camera.y = y - FP.halfHeight;


(Elias) #4

Nothing special, i just wanted to adjust my entity because it’s only 18 pixels tall. So i scaled everything to make it look biger and i needed the camera to have a better look at the Player. Then i saw that the player was flickering during the movement process.


(azrafe7) #5

That code is fine - I’ve doublechecked by testing it -, so the problem must lie somewhere else. Have you tried to create a simple new project just testing the camera following to see if it shows any issue?


(Jacob Albano) #6

You’re probably setting it to two separate values at different times in the same frame.


(Elias) #7

Two seperate values? I dont kinda get it. I also have my object to accelerate with a function i made, and i use a point variable to make it move by using “moveBy”.


(azrafe7) #8

Man, not to sound harsh, but we need to see more of your code for helping you out. Really.

Til now we’ve just put out some educated guesses (or “we were shooting in the dark” if you prefer), but without more insights it would be difficult to point you in any direction.


(Elias) #9

You are right… my mistake. Here is everything i got in the player’s class.

public class Player extends Entity { [Embed(source = “…/gfx/player.png”)] private const PLAYER_PNG:Class;

	public const ACCEL:Number = 200;
	public const GRAV:Number = 100;
	public const MAXX:Number = 400;
	public const MAXY:Number = 100;
	
	public var $sprPlayer:Spritemap = new Spritemap(PLAYER_PNG, 8, 18);
	
	public var onFloor:Boolean;
	
	public var point:Point = new Point();
	
	public var accelX:Number = 0;
	public var accelY:Number = 0;
	public var gravity:Number = 0;
	public var spdX:Number = 0;
	public var spdY:Number = 0;
	
	public function Player()
	{
		Input.define("R", Key.RIGHT);
		Input.define("L", Key.LEFT);
		Input.define("J", Key.Z);
	}
	
	override public function added():void
	{
		super.added();
		
		setHitbox(16, 16);
		type = "player"
		graphic = new Image(PLAYER_PNG);
		layer = 3;
		
		x = FP.halfWidth - halfWidth;
		y = FP.halfHeight - halfWidth;
	}
	
	override public function update():void
	{	
		super.update();
		/**
		 * camera
		 */
		FP.camera.x = x - FP.halfWidth;
		FP.camera.y = y - FP.halfHeight;
		
		/**
		 * Movement input.
		 */
		if (Input.check("R")) accelX += ACCEL;
		if (Input.check("L")) accelX -= ACCEL;
		
		if (accelX != 0)
		{
			if (accelX > 0)
			{
				if (spdX < MAXX)
				{
					spdX += accelX;
					if (spdX > MAXX) spdX = MAXX;
				}
				else accelX = 0;
			}
			else
			{
				if (spdX > -MAXX)
				{
					spdX += accelX;
					if (spdX < -MAXX) spdX = -MAXX;
				}
				else accelX = 0;
			}
		}
		else spdX = 0;
		
		/**
		 * Collision check with the floor.
		 */
		if (collide("map", x, y + 1)) onFloor = true;
		else onFloor = false;
		
		/**
		 * Gravity.
		 */
		if (onFloor == false) accelY = 40;
		else accelY = 0;
		if (accelY <= 0 && Input.check("J")) accelY = 20;
		spdY += accelY;
		if (spdY > 500) spdY = 500;
		
		/**
		 * Jump input.
		 */
		if (onFloor && Input.pressed("J"))
		{
			spdY = -600;
			onFloor = false;
		}
		
		point.x = spdX * FP.elapsed;
		point.y = spdY * FP.elapsed;
		moveBy(point.x, point.y, "map");

(azrafe7) #10

Well done! :smirk:

Just move your camera update code after moveBy() and you should be fine!


(Elias) #11

Wow, such a small detail caused so much trouble. Thanks for your help man :smile: