Doubt about random
fgb01

I would like to know how to use random on x-axis for example.
Thank you very much.

All 4 Comments
krumza

I assume that you are doing top-down shooter.

let's based on this assumption will make the code

So The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1); that is, from 0 (inclusive) up to but not including 1 (exclusive) (please read here )

Open your app.js and insert function:

App = function()// - IT YOUR CODE
{
...
//INSERT ONLY THIS FUNC!!!
//function must be inside App

this.getRandomArbitrary = function(min, max) {
   return Math.floor(Math.random() * (max - min)) + min;
}
...

}

Then find some object whitch always on your scene and open it onUpdate function. Then insert :

if(this.falltimer>=10){
    var x = wade.app.getRandomArbitrary(-wade.getScreenWidth()/2,wade.getScreenWidth()/2);
    var y = -wade.getScreenHeight()/2;    
    var tiro = wade.getSceneObject("Tiro").clone();
    tiro.setPosition(x, y-50);
    wade.addSceneObject(tiro);
    tiro.setVelocity(0,250);
    this.falltimer=0;
} else {
    (isNaN(this.falltimer))? this.falltimer=10 : this.falltimer++;
}

And then you get something like:

fgb01

Good Morning,
This example I understood and worked well ...
But I went to see the documentation that you passed and has a function that does not repeat the numbers but when using it is giving this error:
"Uncaught TypeError: wade.getRandomInt is not a function (T, 5:21)"

krumza

Sorry, mate. I give you link not to wade documentation but javascript documentation

It not wade - just custom function and if you want use it do same as in example - include function in wade.app :

if you wanna use this

function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min;
}

1.method:

App = function(){
//place in app
this.getRandomInt=function(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min;
}
//to call later you can do wade.app.getRandomInt(min,max)
}

2.method var global

//place before app
var getRandomInt =function(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min)) + min;
}



App = function(){

//to call later you can do getRandomInt(min,max) or window.getRandomInt(min,max)
}

 

fgb01

Very good, my friend...
It worked perfectly and I understood ...
You who help us when we need are the best ...
Thanks a lot for the help...

Post a reply
Add Attachment
Submit Reply
Login to Reply