Centering an actor / View following actor move

Non-platform specific questions.

Centering an actor / View following actor move

Postby mcavalcanti » Mon Aug 15, 2011 3:08 pm

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;
Mario Cavalcanti
----------------------------
Code is poetry.
User avatar
mcavalcanti
 
Posts: 54
Joined: Fri Jul 22, 2011 1:09 am
Location: Rio de Janeiro, Brazil
Score: 1 Give a positive score

Re: Centering an actor / View following actor move

Postby Game A Gogo » Mon Aug 15, 2011 3:52 pm

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.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Centering an actor / View following actor move

Postby mcavalcanti » Mon Aug 15, 2011 4:02 pm

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.
Mario Cavalcanti
----------------------------
Code is poetry.
User avatar
mcavalcanti
 
Posts: 54
Joined: Fri Jul 22, 2011 1:09 am
Location: Rio de Janeiro, Brazil
Score: 1 Give a positive score

Re: Centering an actor / View following actor move

Postby Game A Gogo » Mon Aug 15, 2011 4:15 pm

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.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Centering an actor / View following actor move

Postby mcavalcanti » Mon Aug 15, 2011 4:38 pm

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?
Mario Cavalcanti
----------------------------
Code is poetry.
User avatar
mcavalcanti
 
Posts: 54
Joined: Fri Jul 22, 2011 1:09 am
Location: Rio de Janeiro, Brazil
Score: 1 Give a positive score

Re: Centering an actor / View following actor move

Postby Game A Gogo » Mon Aug 15, 2011 8:59 pm

Could you send me the GED with the data folder? As everything you describe should be working properly....
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Centering an actor / View following actor move

Postby foleyjo » Tue Aug 16, 2011 9:15 am

can't you just make the tank the parent of view?
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Centering an actor / View following actor move

Postby skydereign » Tue Aug 16, 2011 9:30 am

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Centering an actor / View following actor move

Postby mcavalcanti » Tue Aug 16, 2011 10:21 am

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!
Mario Cavalcanti
----------------------------
Code is poetry.
User avatar
mcavalcanti
 
Posts: 54
Joined: Fri Jul 22, 2011 1:09 am
Location: Rio de Janeiro, Brazil
Score: 1 Give a positive score

Re: Centering an actor / View following actor move

Postby foleyjo » Tue Aug 16, 2011 11:13 am

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
KISS -Keep It Simple Stoopid
foleyjo
 
Posts: 275
Joined: Mon Jul 09, 2007 1:15 pm
Score: 15 Give a positive score

Re: Centering an actor / View following actor move

Postby Game A Gogo » Tue Aug 16, 2011 2:42 pm

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
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Centering an actor / View following actor move

Postby mcavalcanti » Tue Aug 16, 2011 4:24 pm

No problem! :D
Again, thanks for the reply. Best.
Mario Cavalcanti
----------------------------
Code is poetry.
User avatar
mcavalcanti
 
Posts: 54
Joined: Fri Jul 22, 2011 1:09 am
Location: Rio de Janeiro, Brazil
Score: 1 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest