Page 1 of 1

Which Side Is Open?

PostPosted: Wed May 25, 2011 11:11 am
by jimmynewguy
I have this "control" actor and when a certain time limit goes by I want him to keep picking random sides until one is open. But for some reason it only seems to work some times? I have this code:

Code: Select all
SPOT:
i = rand(5);
switch(i)
{
case 1:
if(CollisionFree("Event Actor", x+25,y))
CreateActor("virus", "virus", "(none)", "(none)", 0, 0, false);
else
goto SPOT;
break;
case 2:
if(CollisionFree("Event Actor", x+2,y+25))
CreateActor("virus", "virus", "(none)", "(none)", 0, 0, false);
else
goto SPOT;
break;
case 3:
if(CollisionFree("Event Actor", x-25,y))
CreateActor("virus", "virus", "(none)", "(none)", 0, 0, false);
else
goto SPOT;
break;
case 4:
if(CollisionFree("Event Actor", x+2,y-25))
CreateActor("virus", "virus", "(none)", "(none)", 0, 0, false);
else
goto SPOT;
break;
}


which is GOD AWFUL. I really should have done a loop, but that's only a minor problem. When the actors are created they don't really fit to the 25 X 25 grid they should... hence the +/- 2's in there at random spots (which still don't help enough)

Does anyone have any idea why this would be/how to fix it? The actor created is a square 25 X 25 and so is the control actor, and it's positioned on the grid. The created actors also use this code and the closer to the middle it is the more it seems to be messed up...? Oh well, cheers and thanks for at least reading this.

Re: Which Side Is Open?

PostPosted: Wed May 25, 2011 11:21 am
by skydereign
First off, rand(5) will return a random number between 0 and 5. That means... if set to an int, your range is 0-4. So... you are skipping 0 in your switch statement. I'd use rand(4)+1 as the rand statement, or just use 0-3 for your case statements. For the +/-2, I wouldn't suggest that, even if that was fixing it. Not sure what is causing the problem, but removing them seems to work fine for me. Remembering of course CollisionFree means it is actually collision free. Also.... http://xkcd.com/292/

Re: Which Side Is Open?

PostPosted: Wed May 25, 2011 7:29 pm
by jimmynewguy
Thanks sky, I just found this game and wanted to continue working on it. I coded this quite a while back and I think I'm just going to start over completely. I'll watch my back for dinosaurs though...