Anyone tried using motion tween to have the camera follow an object?
Motion tween for camera movement
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
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.
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.
Sure thing, but the player who’s followed by the camera still flickering like crazy, even after the camera stops moving.
Looks to me like its flipping back and forth past the player. Can you post your code?
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();
}
}
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?
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
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.
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.
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))
Thanks for the code Azrafe. 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?