Objects Are Shaking When Camera Is Moving


(Zouhair Elamrani Abou Elassad) #1

Hi, i’m moving my player through an already prepared objects on the stage, when i move my player the camera moves as well, but i’ve noticed that when the camera is moving the other objects are shaking even though they are fixed and not moving.

This is my player class :

public class Player extends PhysicsEntity
{
	
	[Embed(source="../../assets/images/beat.png")]
	protected static const PLAYER_ART:Class;
	
	public var sprPlayer:Spritemap = new Spritemap(PLAYER_ART, 400, 300);
	
	private static const moveSpeed:uint = 5;
	private static const jumpForce:uint = 40;
	
	private var grav:Number = 8;
	private var playerGravity:Number = 1;
	private var maxJump:Number = -10
	private var _playerVelocity:Number = 8;
	
	
	public static var flapped:Boolean = false;
	
	public function Player(x:Number, y:Number) 
	{
		
		this.x = x;
		this.y = y;
		
		sprPlayer.add("fly", [0, 1, 2], 20, true);
		sprPlayer.add("fall", [4], 20, true);
		
		sprPlayer.scale = .2;
		
		name = "player";
	}

	override public function added():void 
	{
	
		graphic = sprPlayer;
		sprPlayer.play("fly");
		
		initPlayermask();
		
		Input.define('fly', Key.SPACE, Key.UP);
		
		
		super.added();
	}
	
	private function initPlayermask():void 
	{
		
		try {
			mask = Globals.generateMask(sprPlayer);
		}
		catch (e:Error) {
			trace("error player "+e.message);
		}
	}
	
	override public function update():void 
	{
					
		if (Globals.playerAlive) {
			this.y += grav;
			grav += Globals.playerGravity;
			
			if (Input.pressed(Key.UP) || Input.mousePressed) {

					velocity.x = _playerVelocity;
					
					y -= Globals.playerJump;
					grav = Globals.playerMaxJump;
					
					
			}
			
		}
		else {
			trace("game active false player");
			sprPlayer.play("fall");
			active = false;
		}
		
		super.update();
	}	
	
} 

This a link to the project, it’s a simple one :

Simple Project


The Best Way To Handle A Game With Multiple Animated Objects!
(Martí Angelats i Ribera) #2

Do you have this set?

nonMovingEntity.graphic.relative = false;

(Ben Pettengill) #3

I couldn’t find your code that moves the camera, but I know that sprites can jitter if the camera position is not a whole number. If you round the position of the camera using Math.floor that might help


(Martí Angelats i Ribera) #4

The code is derectly in the Entity and the defferent graphic classes (there’s a parameter for graphics called camera wich is a Point). Here the part of the code wich calculates the x and y of a graphic (well every graphic type have its own but this is the most common one):

_point.x = point.x + x - camera.x * scrollX;
_point.y = point.y + y - camera.y * scrollY;
target.copyPixels(_source, _sourceRect, _point, null, null, true);

So that point can be decimal and it’s directly used to copy the pixels to the screen buffer.

Just as a note, if you set the graphic to relative = false that graphic won’t move with the camera. (in the Entity class there’s a line that sets the camera to (0, 0) if it’s not relative.


(Zouhair Elamrani Abou Elassad) #5

There is a function : updateCamera in MyWorld, the camera changes with the player : FP.screen.camera = _player.x - 100


(Zouhair Elamrani Abou Elassad) #6

I understood the fact the setting relative to false in the entity won’t make it move with the camera, but i can’t see the use of the parameter point, do i have to use Math.floor


(Zouhair Elamrani Abou Elassad) #7

When i changed the function that i call from my world that add the objects on the screen to this, no object was added to the screen :

private function addLevel(nextLevel:Number, levelIndex:String):void 
	{	
		var level:Level = new Level(nextLevel, levelIndex, LEVEL);
		level.graphic.relative = false;
		add(level);	
	}

And when i add this in the add method in the Level entity, all the objects take the same place :

level.graphic.relative = false;

(Zouhair Elamrani Abou Elassad) #8

Actually i changed it to FP.camera.x = Math.floor(_player - 100), but still have the same issue


(Zouhair Elamrani Abou Elassad) #9

I also noticed that when i click on the mouse for the player to move, the objects shake as well, could it have something to do with the fact that the camera movement depends on the player movement as well !