random action

Talk about making games.

random action

Postby raminkhan » Sun Nov 06, 2011 4:08 am

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;
}
raminkhan
 
Posts: 63
Joined: Mon Sep 12, 2011 3:35 pm
Score: 0 Give a positive score

Re: random action

Postby skydereign » Sun Nov 06, 2011 7:27 am

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


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest