Trouble with WalkStraight [Solved]

Started by Valentine, Mon 13/08/2007 23:47:08

Previous topic - Next topic

Valentine

Hi, I'm trying to get a background character to walk in one of my game scenes, but I'm having trouble setting the points he goes through. I want him to walk through four points, then back to the first, but when testing, he just goes straight to the last point.

This is the code I'm using:

cGuard.WalkStraight(200, 200);
cGuard.WalkStraight(300, 200);
cGuard.WalkStraight(300, 300);
cGuard.WalkStraight(200, 300);

Now I'm pretty sure I need to utilise the Moving property to check whether the characters got to each position before going on to the next, but I'm not sure how to put it all together into a working script.

Thanks to anyone who can help me.


Valentine

#2
Ahhh.. Thanks for that.

I saw that in some three year old code I looked at trying to figure out my problem, but must have missed it in the manual. I assumed it had been taken out. Silly me!

==EDIT==

ok, i'm having some trouble again now. I want the character to walk about in the background somewhat randomly, but as soon as I move the character into the game, the game freezes up, and I have to Alt-X out. This is the code I'm using:

  // script for Room: Repeatedly execute

int BoxControl = 0;
while (cBox.Room==1) { BoxControl=Random(3);
               if (BoxControl == 0) { cBox.WalkStraight(cBox.x+50, cBox.y+50); }
               if (BoxControl == 1) { cBox.WalkStraight(cBox.x-50, cBox.y+50); }
               if (BoxControl == 2) { cBox.WalkStraight(cBox.x+50, cBox.y-50); }
               if (BoxControl == 3) { cBox.WalkStraight(cBox.x-50, cBox.y-50); }
               }

I'll add some code later to make sure the character doesn't go outside a defined area, but at the moment, I just want to get this bit working.

I've made sure the characters on a walkable area, which it definately is (I've made the entire room walkable to make sure).

Thanks for any help.

Khris

The game freezes because it gets stuck in the while loop.
The character doesn't leave the room, so cBox.Room will always be 1.
Replace "while (cBox.Room==1)" with "if (!cBox.Moving)" and it should work.

Valentine

Thankyou very much for that :)

Just for my understanding (I can't remember reading it in the manual), what does the ! infront of the character name signify?

Khris

! is the logical not; I used it here because I want the code to be executed only if the character *isn't* moving.

strazer

That means "if (!cBox.Moving)" is the same as "if (cBox.Moving == false)" just as "if (cBox.Moving)" is the same as "if (cBox.Moving == true)".

It's shorter the ! way, but I personally write everything out to make it easier to read.

SMF spam blocked by CleanTalk