Page 1 of 1

random action

PostPosted: Sun Nov 06, 2011 4:08 am
by raminkhan
okay how would I create a random action in my game.
like lets say I want my character after being shot to stop and turn around and move in negative x direction meaning like if I walk from point A to B now I want it to go back to point B. based on a random probability. how?

this is the script I am using but its not working.

int main()
{
int Direction1=1;
int Direction2=-1;

int face;

for(int Direction=1; Direction <=7; ++Direction);
{
face=1+rand() %7;
switch(face)
{
case 1:
++Direction1;
Direction=1;
Speed++;
break;
case 2:
++Direction2;
Direction=-1;
Speed++;
break;
Default:
cout<<"Program should never get here";
}
}
cout<<"face" <<setw(7)<<"Direction"<< endl;
cout<<" 1" <<setw(7)<<Direction1
<<"\n 2" <<setw(7)<<Direction2 << endl;
}

Re: random action

PostPosted: Sun Nov 06, 2011 7:27 am
by skydereign
That code won't work, purely because it is C++, but as well as you should be % by 2 (not 7). Well using the code that I told you to put in the timer event, you can do this.
Code: Select all
if(floor(rand(2))==0)
{
    Direction*=1;
}

Speed=5;

The rand function in gE returns a random double in between 0 and the number you pass it. The floor function converts it to an int, and so on the timer event, it has a 50% chance of changing direction. But, after the timer it will continue moving.