Page 1 of 1

background follow view and view follow player without parent

PostPosted: Wed Jul 01, 2009 2:56 am
by J Maker
ok, sorry, another topic where i can't find wat im looking for, but i remember someone posting a code that lets the view follow the player but smoother than parenting and another code someone posted that has the background actor follow the view. any codes that work are welcome or at least the link. thx :D

Re: background follow view and view follow player without parent

PostPosted: Wed Jul 01, 2009 3:30 am
by DST
The trick is to do it from the player's script, not view's. This will keep it instantaneous and smooth.

player>draw actor>
Code: Select all
view.x=x-320;
view.y=y-240;
background.x=player.x;
background.y=player.y;

Re: background follow view and view follow player without parent

PostPosted: Wed Jul 01, 2009 6:01 pm
by J Maker
thx man! never really thought of it that way.

Re: background follow view and view follow player without parent

PostPosted: Wed Jul 01, 2009 6:18 pm
by J Maker
the code does great except for one little thing. the background is off. my background is the same size as the view and is aligned perfect in the GE editor, but when i hit game mode, the code makes it move to the bittim right, like this:

Re: background follow view and view follow player without parent

PostPosted: Wed Jul 01, 2009 8:56 pm
by skydereign
This is probably happening due to the discrepancy of xy coordinates of view and other actors. I can't give you direct code, as I don't know your dimensions. If your dimensions are not 640x480, that may be why. If not, can you explain more?

Re: background follow view and view follow player without parent

PostPosted: Wed Jul 01, 2009 10:16 pm
by Game A Gogo
Code: Select all
view.x=x-view.width/2;
view.y=y-view.height/2;
background.x=player.x;
background.y=player.y;

Re: background follow view and view follow player without parent

PostPosted: Thu Jul 02, 2009 1:34 am
by J Maker
skydereign wrote:This is probably happening due to the discrepancy of xy coordinates of view and other actors. I can't give you direct code, as I don't know your dimensions. If your dimensions are not 640x480, that may be why. If not, can you explain more?
my demensions are wierd. they r custom thats prolly why. they are: 485x365 in order to fit the picture. game a gogo, i will try ur code and report back

Re: background follow view and view follow player without parent

PostPosted: Thu Jul 02, 2009 1:38 am
by J Maker
Game A Gogo, u rock man! the code works like a charm!

Re: background follow view and view follow player without parent

PostPosted: Thu Jul 02, 2009 12:10 pm
by Game A Gogo
Universal codes that fits for everyone makes J Maker happy c:

Re: background follow view and view follow player without parent

PostPosted: Thu Jul 02, 2009 3:42 pm
by DST
x=x-(width/2);

This is standard notation for centering objects in a script, used in many languages.

x=x-320;

This is magic number notation for a 640 x480 game.

Why use the magic number? Because width/2 doesn't change. You already know the answer to that equation.

So if your game plays for 10 minutes, at 30 fps, you will compute width/2 no less than 18,000 times. Its not a big deal, but its good to get in the habit of not wasting cpu, even if its only a little bit.

Re: background follow view and view follow player without parent

PostPosted: Thu Jul 02, 2009 10:21 pm
by Game A Gogo
true that, although since this variable changes from game to game, it's good to ask which value their's would be :P

Re: background follow view and view follow player without parent

PostPosted: Mon Jul 06, 2009 2:35 am
by J Maker
Game A Gogo wrote:Universal codes that fits for everyone makes J Maker happy c:
totaly! :D thx man