How to make a random character appear (SOLVED)

Started by Creator, Wed 09/09/2009 15:39:03

Previous topic - Next topic

Creator

I'm making a Wack-a-Mole type game, but I'm making sprites in the likeness of people I know, thus I have more than 1 character. I've already put a function pre-setting the coordinates for the character to appear.

Code: ags

function wackCheck() { // I'm guessing that something has to go in the parenthesis.
  spot = Random(3); // spot is defined in the global script header as it's used in rooms as well as the global script
  
    if (spot == 0) {
    Char.x = 60;
    Char.y = 287;
  }
    else if (spot == 1) {
      Char.x = 448;
      Char.y = 143;
    }
    else if (spot == 2) {
      Char.x = 224;
      Char.y = 128;
    }
    else {
      Char.x = 314;
      Char.y = 378;
    }
    Char.Transparency = 0;
}


How can I tell the function that 'Char' represents a random character?

Akatosh

You can also address characters via a numbered array. As in,

Code: ags

function wackCheck(int charnumber) { // something has to go in the parenthesis.
  spot = Random(3); // spot is defined in the global script header as it's used in rooms as well as the global script
  
    if (spot == 0) {
    character[charnumber].x = 60;
    character[charnumber].y = 287;
  }
    else if (spot == 1) {
      character[charnumber].x = 448;
      character[charnumber].y = 143;
    }
    else if (spot == 2) {
      character[charnumber].x = 224;
      character[charnumber].y = 128;
    }
    else {
      character[charnumber].x = 314;
      character[charnumber].y = 378;
    }
    character[charnumber].Transparency = 0;
}


Note that this is not the only - or most elegant - way to do this by far, but it will certainly work.

Creator

#2
Wow, that was fast. Thanks Akatosh, however I'm having a problem. Here's my code.

Code: ags

function wackCheck(int Char) {
  spot = Random(3);
  Char = Random(1); // I have two characters at the moment and need them to randomly appear
  cJess.Transparency = 100;
  cDaniel.Transparency = 100;
  
  if (spot == 0) {
  character[Char].x = 60;
  character[Char].y = 287;
  }
  else if (spot == 1) {
    character[Char].x = 448;
    character[Char].y = 143;
  }
  else if (spot == 2) {
    character[Char].x = 224;
    character[Char].y = 128;
  }
  else {
    character[Char].x = 314;
    character[Char].y = 378;
  }
  character[Char].Transparency = 0;
  SetTimer(2, 80); //Reset timer
}


I call the funtion with
Code: ags

wackCheck(0); // The 0 is there because I HAVE to specify an integer


The problem is that only the character with the ID of 0 (cJess) appears (still appears at one of the random coordinates, so that's not a problem). Otherwise neither character appears. Is this because character[Char] is getting called differently everytime the line appears?

EDIT - Never mind. Code above works perfectly. cDaniel wasn't in the room. Sorry about that.

NsMn

What happens if you replace the 1 with a higher number (at "Char=Random(1)" )?

Creator

Never mind code works. Explaination in above post.

Akatosh

#5
Well, first of all, I wouldn't put the line with Char inside the function. You can just call it like this...

Code: ags

wackCheck(Random(1));


...meaning you could remove this line...

Code: ags

Char = Random(1); // I have two characters at the moment and need them to randomly appear


Otherwise, I could see no reason why the code shouldn't work. Are you sure cDaniel is character number 1? If yes, how often have you tested the whole thing? The random number generator can screw you over on occasion.

Darn ninjas.

/EDIT: Oh, and just for the laughs - here's a vastly shortened version that should do the same.

Code: ags

function wackCheck(int Char) {
  spot = IntToFloat(Random(3));
  character[Char].x = FloatToInt(154.33*Maths.RaiseToPower(spot, 3.0) - 769.0*Maths.RaiseToPower(spot, 2.0)+ 1002.67*spot + 60.0);
  character[Char].y = FloatToInt( 22.67*Maths.RaiseToPower(spot, 3.0) -   3.5*Maths.RaiseToPower(spot, 2.0)- 163.167*spot +287.0);
  character[Char].Transparency = 0;
  character[1-Char].Transparency = 100; //only works as long as you have exactly two characters
  SetTimer(2, 80); //Reset timer
}


:=

SMF spam blocked by CleanTalk