[英]How do I generate a random X value for each “projectile” in my falling objects game using Javascript?
I am coding a game that is currently in its very early stages for a project to try to learn more about coding. 我正在编写一个游戏,该游戏目前正处于初期阶段,供一个项目尝试了解有关编码的更多信息。 In my game, objects generate randomly (green squares), and the player (red square), avoids them.
在我的游戏中,物体随机产生(绿色方块),而玩家(红色方块)避开它们。 I am having trouble trying to get the green squares to generate from a random position on the x-axis.
我在尝试使绿色方块从x轴上的随机位置生成时遇到麻烦。 I already have a formula to generate a random number for X, but after it selects a number randomly, all the "projectiles" generate there, rather than all generating from a different area.
我已经有了一个为X生成随机数的公式,但是在它随机选择一个数字之后,所有的“弹丸”都在该位置生成,而不是全部从不同的区域生成。 How would I get all the "projectiles" to generate from different positions on the x-axis randomly?
我如何才能从x轴上的不同位置随机生成所有“弹丸”?
var randomX = Math.floor(Math.random() * 480) + 15;
function updateGameArea() {
var x, y;
for (i = 0; i < projectiles.length; i += 1) {
if (player.crashWith(projectiles[i])) {
gameArea.stop();
return;
}
}
gameArea.clear();
gameArea.frameNo += 1;
if (gameArea.frameNo == 1 || everyinterval(150)) {
x = randomX;
y = gameArea.canvas.height;
projectiles.push(new component(40, 40, "green", x, y));
}
for (i = 0; i < projectiles.length; i += 1) {
projectiles[i].y += -1; // the shape is using its coordinates to build downward from its y position
projectiles[i].update();
}
player.newPos();
player.update();
}
function everyinterval(n) {
if ((gameArea.frameNo / n) % 1 == 0) {return true;}
return false;
Expected : Green squares generate in random positions on the x- axis every 3 seconds and move upwards 预期 :每3秒就会在x轴上的随机位置生成一个绿色方块,并向上移动
Actual : Green squares all generate from the same random position on the X-axis. 实际 :绿色方块均从X轴上的相同随机位置生成。
You should reset X every time you're adding a new projectile: 每次添加新的射弹时,都应重置X:
if (gameArea.frameNo == 1 || everyinterval(150)) {
randomX = Math.floor(Math.random() * 480) + 15;
x = randomX;
y = gameArea.canvas.height;
projectiles.push(new component(40, 40, "green", x, y));
}
Otherwise, the randomX value stays constant as the value originally evaluated on line 1 when the interpreter reached it. 否则,randomX值将保持不变,即解释器到达该值时最初在第1行计算的值。
Here's your problem: 这是您的问题:
var randomX = Math.floor(Math.random() * 480) + 15;
// Generates a random number and stores it to randomX
// Called using 'randomX'
You need to turn it into a function if you want it to run each time: 如果您希望每次运行它,都需要将其转换为一个函数:
var randomX = function() { Math.floor(Math.random() * 480) + 15 };
// Returns a new number each time
// Called using 'randomX()'
Both shivashriganesh mahato and natelouisdev have, essentially responded to how to fix the issue but since you are learning coding here is a tip. 从本质上讲,shivashriganesh mahato和natelouisdev都对如何解决此问题做出了响应,但是由于您正在学习编码,因此这里是一个提示。 When you code, the code will run in a particular order.
当您编码时,代码将以特定顺序运行。 If you want something to be reassigned repeatedly, in this case a randomized number being used, and you want it to occur only after an event, you need to make sure that it gets trigger within each event.
如果要重复分配某些东西(在这种情况下使用的是随机数),并且希望仅在事件发生后才出现,则需要确保在每个事件中都触发它。
natelouisdev has a good approach because, by using it as a function, you can call your randomizer more cleanly in your code and make it reassign the value of x each time. natelouisdev有一个很好的方法,因为通过将其用作函数,您可以在代码中更清晰地调用随机化器,并使它每次都重新分配x的值。
Since you are building a game, it is also a good idea to compartmentalize your code. 由于您正在开发游戏,因此划分代码也是一个好主意。 It'll make it easier to keep your ideas in order for each event trigger.
这样可以更轻松地将您的想法保持在每次事件触发时的状态。
Example: 例:
function gameActive(){} - Defines what is normal gameplay. 函数gameActive(){}-定义什么是正常的游戏玩法。 everything that occurs during normal gameplay should be managed here.
正常游戏过程中发生的一切都应在此处进行管理。
function gameArea(){} - Defines game canvas that function more for UI than for gameplay (scores, lifes, size of screen, etc) function gameArea(){}-定义游戏画布,该画布对UI的作用大于对游戏性的作用(得分,寿命,屏幕尺寸等)
Had you created individual functions you'd realize you only need a randomized 'x' value upon regular play thus you'd assign it within the gameActive() function and not as a global variable. 如果创建了单个函数,您会意识到在常规游戏中只需要一个随机的'x'值,因此您可以在gameActive()函数中分配它,而不是将其作为全局变量。 Then you'd call the gameActive() function as many times as needed within a time interval to ensure a unique value is created each time.
然后,您将在一个时间间隔内根据需要多次调用gameActive()函数,以确保每次都创建一个唯一值。
-Side note: Don't litter unnecessary global variables. 旁注:不要乱扔不必要的全局变量。 It'll make a mess off of your code when debugging.
调试时,这会使您的代码变得一团糟。 -
-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.