Motion tween for camera movement


(Elias) #1

Anyone tried using motion tween to have the camera follow an object?


(azrafe7) #2

Sure. What’s the problem?


(Jacob Albano) #3

Using a traditional tween for following objects around is difficult because the tween has to be reset each time the object moves. A better approach* is to use an approach function.

// CameraFollower class

// how much of the distance to move each frame
// 0.1 is 1/10 of the way, etc.
private static const amount:Number = 0.2;

public override function update():void
{
    moveTowards(target.x, target.y, FP.distance(X, Y, x, y) * amount);
    FP.camera.x = x;
    FP.camera.y = y;
}

*No pun intended :stuck_out_tongue:


(Elias) #4

Well i tried with the tween and the player was flickering all the time. When i tested the motion tween with another object, it did fine, but not with the camera. I never thought for the camera as an object, so maybe using moveTowards should do the trick.


(Jacob Albano) #5

Just to be clear, this code should go in an Entity class that you add to your world. The camera class itself doesn’t have a moveTowards() method.


(Elias) #6

Sure thing, but the player who’s followed by the camera still flickering like crazy, even after the camera stops moving.


(Ultima2876) #7

Can you show an example swf?


(Elias) #8

Here, take a look

Project1.swf (54.7 KB)


(Alex Larioza) #9

Looks to me like its flipping back and forth past the player. Can you post your code?


(Elias) #10

Yep! it’s same as the one Jacob posted. The player’s point is a static var and updates normaly with the player possition.

public class Box extends Entity {

	public function Box()
	{
		
	}
	
	override public function added():void 
	{
		x = 80;
		y = 80;
		
		super.added();
	}
	
	override public function update():void 
	{
		moveTowards(Player.point.x - 100 , Player.point.y - 100, 10, null, false);
		FP.camera.x = x;
                FP.camera.y = y;
		
		super.update();
	}
}

(azrafe7) #11

O.o That code should work.

There’s probably some problem when you set Player.point or a wrong order of updates.

What if you just drop the point and do:

moveTowards(player.x - 100 , player.y - 100, 10, null, false);

Any changes?


(Elias) #12

Well, it wont work because player’s x and player’ y is not static as the point. Gonna have a function that adds the x and y of my player straight to the camera entity


(azrafe7) #13

Wait… if you can access Player.point, it means that you can access Player (which is Entity right?). And since x and y are public they’re accessible too.


(Elias) #14

Player and the camera are on different classes but both entities. Just tried your suggestion and i managed to access players x&y by putting both camera and player as variables in the Gameworld. Still no changes. As long as i am moving, the player starts flickering.


(azrafe7) #15

Mmmhh… Can you share the project (full code of the swf above) so that we can look into it?

EDIT: My test (FPCameraFollow.zip (3.3 KB))


(Elias) #16

Thanks for the code Azrafe. :smile: The issue was FP.Elapsed i put in my code. Everything else works fine, but how to make it work with FP.Elapsed? I had put it in the player movement and at the moveForward() of the camera. Did i miss something?