Jump to content

Make something to appear at random


omer_ahmed_tabarik

Recommended Posts

Hello!I am into developing a game. I started from scratch and did not know much of scripting but I started learning now. For the game that I'm making I want to make somethings appear at random when the sprite moves on the map.I figured this one out in PHP but in this language I can't.Please click here to see that map where the sprite moves, I uploaded and made one for just to know how to appear them, this one is just not from the game.Map test linkPlease help me. Thanks in advance.

Link to comment
Share on other sites

Ok, thanks for trying me to help. I tried using that but could not. In php there is likefor example for my game I would use$chance=rand(1,5)if (chance=2 && fight_chance=="Thief")something like this but by using the command you mentioned, I'm having problems, please help me on that.

Link to comment
Share on other sites

We do need to know your problem to fix it, but I think I understand it. The JS equivalent to your PHP is...

var chance=Math.floor(Math.random() * 5) + 1;if (chance==2 && fight_chance=="Thief")

Math.random() - a number (could be a fraction) greater than or equal to 0 and less than 1 (0 <= x < 1)Math.random() * 5 - a number (could be a fraction) greater than or equal to 0 and less than 5 (0 <= x < 5)Math.floor(Math.random() * 5) - an integer (not a fraction) greater than or equal to 0 and less than 5 (0 <= x < 5)Math.floor(Math.random() * 5) + 1 - an integer (not a fraction) greater than or equal to 1 and less than 6 (1 <= x < 6)

Link to comment
Share on other sites

Ok, seriously that was quite helpful and I think I was able to convert it myself. Now can we make it choose something randomly, like a string?for examplerand(theif,ogre)while their and ogre have their own types, like in an array?Thanks for the help and one last thing. Can I do this I mean, round off the fraction like if it is 0.423424324234 to something like 0 or 1?Got this one :)

Link to comment
Share on other sites

Ok, seriously that was quite helpful and I think I was able to convert it myself. Now can we make it choose something randomly, like a string?for examplerand(theif,ogre)while their and ogre have their own types, like in an array?Thanks for the help and one last thing. Can I do this I mean, round off the fraction like if it is 0.423424324234 to something like 0 or 1?Got this one :)
Like you said, put them in a numeric array. Then, choose a number with the method suggested above, and select the string at this index.
Link to comment
Share on other sites

I tried, but did not work for me. Another query has arisen now.Can I set a start point, other than 0. I mean like the number is between 0 to 6 or 0 to 1000. If I want it to be between 8 and 15, let's just say, than how can I do that?

Link to comment
Share on other sites

Set both the range and the offset to 8.

var chance=Math.floor(Math.random() * 8) + 8;

(8 <= x < 16)Or you could simplify the algorithm so it's PHP-esque:

function rand(start, end){	return Math.floor(Math.random() * (end - start + 1)) + start;}

rand(8, 15) = Math.floor(Math.random() * (15 - 8 + 1)) + 8 = Math.floor(Math.random() * (7 + 1)) + 8 = Math.floor(Math.random() * 8) + 8

Link to comment
Share on other sites

  • 2 weeks later...

Thanks for the help. I just have one more question regarding it.Well, the sprite on the map moves. So, I want to change the chance everytime. It means, that if it moves up, down, left and right, than for every move there should be a chance.I tried doing it with two functions but is not working at the moment, please help me.First I made em appear randomly, but I noticed it needed refreshing everytime. I dug a little deeper and tried eval function on chance, does not seem to work at the moment.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...