Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm extremely new to javascript, and I'm trying to make a very simple canvas game. I want to add multiple enemies, but I want them to appear randomly, not all be hardcoded in. How would I create new objects/enemies every 5 seconds? Sorry for the probably very simple question, I have to learn this somehow, right?
Create enemies array and push to it:
var enemies = [];
setInterval(function(){
enemies.push({
x: Math.ceil(Math.random() * canvasWidth),
y: Math.ceil(Math.random() * canvasHeight)
});
}, 5000);
To do this, you can add a "set interval" function to JavaScript. This will allow you to run some code every five seconds.
setInterval(function(){
// your code to be executed every 5 seconds goes here.;
},5000);
You can also have random objects or enemies by using a random number generator. For example:
setInterval(function(){
//Math.random() returns a random number between 0 and 1.
intRandomNumber = Math.random() * 10;
//Math.round() returns the nearest whole number
intRandomNumber = Math.round(intRandomNumber);
if (intRandomNumber == 1){
//create some monster
}
else if (intRandomNumber == 2){
//create some object
}
},5000);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am currently working on a game in which a timer will be needed, but the one made in js has a very long delay and on each device it is different, which in the case of a ranking where milliseconds count is unacceptable.
I want to create a stopwatch that will be synchronized with the server time and will have the smallest local delay and will show the elapsed time with an accuracy of 0.01 seconds. Any idea how i can do this?
My code look like this:
var stopper = setInterval(myTimer, 10);
let time = 0;
let run = true;
function myTimer() {
if(run) {
time += 0.01;
//update time display
}
}
You can keep something startTime on the server and use on the client to calculate passed time.
If you need to keep the time and update necessarily then try to use websocket
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to generate random numbers between 0 and 9 in JavaScript without using the builtin function math.random(). I need to find out a solution for this problem.
You could just use the mouse position and the time as random noise:
// Expose the current mouse position
let clientX = 0;
window.onmousemove = e => clientX = e.clientX;
// Generate random values and show them:
setInterval(() => {
document.body.textContent = (+new Date + clientX) % 10;
}, 100);
(Note that this is not random enough to be used in cryptographic / security relevant code)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
First, thank you for the time you spend to help me.
Well my question is this: I have a form to make, depending on how you set the user agent to different results.
eg
1 Coin = 3 points,
I want that when the user put more than 10 coins, each coin is worth 4 points instead of 3, something like this:
10 coins = 40 points
Therefore it is a value that goes up depending on the amount
<10 Coins = 3 points
10 Coins = 4 points
50 coins = 5 Points
I have this code that it does is to add up to a fixed value, but I want that value is variable,
var conversions = {
'Monedas': {
'Puntos': 1,
}
};
How could did?
A greeting and thanks a lot.
Check this code:
function Coin()
{
var self = this;
self.points = 0;
function integerDivision(x, y){
return (x-x%y)/y
}
self.AddCoins = function(amount)
{
self.points = self.points+amount*(3+integerDivision(amount, 10));
}
}
var coin = new Coin();
coin.AddCoins(10);
alert(coin.points);
coin.AddCoins(20);
alert(coin.points);
Ok, first of all, your language attribute on your script tag should not be equal to javascript. Instead, use type="text/javascript"
Then, if I understand your question, you could use Math.floor for what you need.
var conversions = {
'Monedas': {
'Coins': ###, // I assume that you'll have a value of coins here
'Puntos': 1
},
};
var factor = Math.floor(conversions.monedas.coins / 10) + 2;
Then, you could set Puntos to factor times monedas.
conversions.monedas.puntos *= factor;
If you need to find the value of the from the form's inputs, you could use jQuery's .val or getElementsByClassName, or getElementById and then use .innerHTML to find the value, I think.
Also, what language are you coming from? Portuguese?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I wish to print elements of the array below
var array = [1,2,3,4,5];
However, only 3 digits must be printed at a time, e.g,
step 1: should print 1,2,3
step 2: should print 2,3,4
step 3: should print 3,4,5
step 4: should print 1,2,3
step 5: should print 2,3,4
and the loop continues .....
I wish to display the thumbnails of this slider in a manner that they will fit a small screen.
http://www.tympanus.net/Tutorials/FullscreenSlideshowAudio
That means only a few thumbnails must appear at a time, while the rest are invisible for a short while.
Thanks..
As you wanted to display 3 elements from the array each time and when the last element is displayed you want to repeat the process again you are suppose to use setInterval() to call the function after a specified time continuously and reset the index to 0 immediately after the last element is displayed. Else increment the initial or counter value. Try this way,
var array = [1,2,3,4,5];
var initVal = 0;
setInterval(function(){
console.log(array[initVal] + "," + array[initVal+1] + "," + array[initVal+2]);
initVal = initVal == 2 ? 0 : initVal+1;
}, 500);
jsFiddle
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
basically, what I am looking for is an animation for digits, a great example would be on this site:
http://www.arkanbuilds.com/
If you scroll down you'll see their 'Builds completed' etc counting up.
How do I do this? I have NO knowledge of Javascript neither jQuery if those are necessary, so help would be appreciated.
Also a small sub-question that links to this question; how can I make a number increase monthly / yearly etc, like on that website, the 'months online'.
I would appreciate the answers.
// this interval will repeat itself every 50 milliseconds
var handler = setInterval(function() {
// get the element that holds the number
var numberContainer = document.getElementById("number");
// get the number. it starts with 0
var number = parseInt(numberContainer.innerText, 10);
// increase it by 1, or 2 (number += 2;)
number++;
// update the number in the element
numberContainer.innerText = number;
// set a limit. if the number is greater than/equal to 50
// stop the interval
if (number >= 50) {
clearInterval(handler);
}
}, 50);
<div id="number">0</div>