【问题标题】:I need these objects in this array to move at random speeds.我需要这个数组中的这些对象以随机速度移动。
【发布时间】:2018-10-18 12:14:44
【问题描述】:

这是我的 github 的链接。 https://github.com/inventive-dev217/arcade-game 。我需要我的 allEnemies 数组中的项目以随机速度移动。现在,它们都以相同的速度移动。任何如何实现这一点的想法将不胜感激。

【问题讨论】:

  • 请在问题中包含您要询问的代码,而不是在有人为您回答问题后无疑会过时的链接中,从而使该问题在未来对其他人无用 - 关键字你的答案是random - 如果只有javascript有某种随机数生成器
  • 不过,说句公道话,仅在 github 上你就可以看到数千个这段代码的化身

标签: javascript oop


【解决方案1】:

嗨,我不是 JS 方面的专家,但这样的事情应该可以工作:

var Enemy = function(x, y) {
// Variables applied to each of our instances go here,
// we've provided one for you to get started
// The image/sprite for our enemies, this uses
// a helper we've provided to easily load images
this.sprite = 'images/enemy-bug.png';
this.x = x;
this.y = y;
this.speed = Math.floor(Math.random() * MaxSpeedValue) + MinSpeedValue; 

}

【讨论】:

    猜你喜欢
    • 2021-02-16
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 2020-05-21
    相关资源
    最近更新 更多