Answer the question
In order to leave comments, you need to log in
Array of images in JS?
I'm making a javascript game. You need to create an array of helicopters at the top of the canvas. I create an array which I call "enemy". I enter x, y, offset, margin. It turns out that only one picture is drawn. Tell me how to make 10 enemies float at the top, given by pictures. If there is any other way, please point it out. Thanks in advance.
Answer the question
In order to leave comments, you need to log in
Post the code for example on JSFiddle . I tried to run it, but nothing happened and the browser started to hang terribly. Therefore, I decided to write a simple example, but I was sucked in and spent several hours developing it. Here is the result and the code . You can see how objects are drawn from an array, but first I want to say.
Firstly, in any game there should be a main function, for me this is gameLoop, for you it looks like updateGameField. It calls the basic methods of drawing and processing physics. Sometimes, by the way, they make 1 method for drawing, for example, at 60fps, and the second for processing physics at 200fps if you need accurate miscalculations (fast bullet flights, etc.).
Secondly, here is tm2 = setInterval (updateGameField, 10)
should not be, I generally think it would be better if this function were not added to javascript; many do not understand its reverse side. You generally set 10ms and this is 100 fps! If your machine at some point does not have time to process at least 1 time, then the function will be called again and will simultaneously execute 2, 3, 10 functions until everything freezes for you, javascript will not wait, it will simply call it on cd is therefore for the game function to be used as a last resort at the end of the function. But a long time ago there was a great optimized feature that will try to maintain 60 fps of your game. I recommend only using it.
Thirdly, I recommend reading articles about game development, development issues, Delta-time and frame rate independence, collisions, etc.setTimeout(updateGameField,1000/60/*60фпс*/);
requestAnimationFrame(gameLoop);
All this is necessary in almost every game, so you need to understand this in order to be able to write at least something.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question