Page 1 of 1

Centering an actor / View following actor move

PostPosted: Mon Aug 15, 2011 3:08 pm
by mcavalcanti
Hello. I had a tank at the center of the screen. When I use arrows, tank and screen must move together. But if the tank collide with a tree, for example, screen position/moving must stop to. So, tank always must be on the center of screen. Please, how to do this?

It's not working. When tank collide, view remains moving.
View >> Draw Actor:
y=tank.y;x=tank.x;

Re: Centering an actor / View following actor move

PostPosted: Mon Aug 15, 2011 3:52 pm
by Game A Gogo
To always have an actor in the center of the screen, you're code is right but needs an offset.
View -> Draw Actor:
Code: Select all
x=tank.x+view.width*.5;
y=tank.y+view.height*.5;


Since the view is not an actual animation actor (Anything but a Canvas, Filled Region, Wire Frame Region, view and Text Actor), it's 0,0 coordinate does NOT represent it's center, but rather it's top left corner, so you need to offset the position by half of the view's width and half of the view's height.

Re: Centering an actor / View following actor move

PostPosted: Mon Aug 15, 2011 4:02 pm
by mcavalcanti
Hi! Thanks to reply, but it is not working. :(
The tank appears outside the screen. I can't see it.

Anyway, what about the collide issue? Will the screen stop moving when the tank collides to a wall?

Best.

Re: Centering an actor / View following actor move

PostPosted: Mon Aug 15, 2011 4:15 pm
by Game A Gogo
Something is not right. Do you have other codes that modifies the view's position?

The script I provided will make the tank actor ALWAYS center. So make sure that tank is not a child (Has a parent) and so for the view.

If you actors are indeed children and that is a must to keep your game working, then using this code might be better:

Code: Select all
xscreen=tank.xscreen+view.width*.5;
yscreen=tank.yscreen+view.height*.5;


xscreen and yscreen are the position based on the screen coordinates. So even if your actors have parents or not, this coordinate will not change.

Re: Centering an actor / View following actor move

PostPosted: Mon Aug 15, 2011 4:38 pm
by mcavalcanti
No, no other code to modify view' position :(
Still appearing outside the screen.

I'm putting your script in 'view->draw actor'. In 'tank->draw actor', i'm putting this:
char * key = GetKeyState();
if(key[KEY_UP]==1) { y-=speed; }
if(key[KEY_DOWN]==1) { y+=speed; }
if(key[KEY_RIGHT]==1) { x+=speed; }
if(key[KEY_LEFT]==1) { x-=speed; }

This is right, isn't it?

Re: Centering an actor / View following actor move

PostPosted: Mon Aug 15, 2011 8:59 pm
by Game A Gogo
Could you send me the GED with the data folder? As everything you describe should be working properly....

Re: Centering an actor / View following actor move

PostPosted: Tue Aug 16, 2011 9:15 am
by foleyjo
can't you just make the tank the parent of view?

Re: Centering an actor / View following actor move

PostPosted: Tue Aug 16, 2011 9:30 am
by skydereign
To address what the problem is, you are using + when you should be using -. Since the tank is supposed to be in the middle, then the view's position should be the tanks position minus half the view.
view -> Draw Actor -> Script Editor
Code: Select all
x=tank.x-width*0.5;
y=tank.y-height*0.5;


And foleyjo, generally you don't want to use parenting like that. It does make the movement a tad easier since you don't need to write any code, but it can cause all sorts of other problems. For one, parenting actors will cause child actors to have a different coordinate grid, and also any states, colors, zdepth, transparency, and the like will be linked. Since you can avoid all of these problems from writing two easy lines of code, you should. A quick example where you'd get problems... You parent the view to the player. In your level you have parts that appear in front of the player, and some that appear behind (to create depth and so on). If you also have a hud that you parent to the view or the player, the graphics that should appear in front of the player, also appear in front of the hud, covering your health display and so on.

Re: Centering an actor / View following actor move

PostPosted: Tue Aug 16, 2011 10:21 am
by mcavalcanti
Game a Gogo, I was attaching the GED when skydereing showed his solution. LOL. :D
Hey, now it's working perfectly! Thanks, skydereing. And thank you too, Game a Gogo, for the attention. Best!

Re: Centering an actor / View following actor move

PostPosted: Tue Aug 16, 2011 11:13 am
by foleyjo
skydereign wrote:And foleyjo, generally you don't want to use parenting like that. It does make the movement a tad easier since you don't need to write any code, but it can cause all sorts of other problems.


That makes sense. I have used parenting for things in the past but the things I was doing were not affected by these problems so I wasn't aware they existed.
I'll avoid it in the future

Re: Centering an actor / View following actor move

PostPosted: Tue Aug 16, 2011 2:42 pm
by Game A Gogo
Oh wow... I'm terribly sorry for such a little mistake I haven't even noticed... This is like getting hit in the face for me xD Thank you skydereign, I should learn from my errors! And sorry again mcavalcanti for mis-informing you

Re: Centering an actor / View following actor move

PostPosted: Tue Aug 16, 2011 4:24 pm
by mcavalcanti
No problem! :D
Again, thanks for the reply. Best.