【问题标题】:Select a new number from array every second randomly每秒随机从数组中选择一个新数字
【发布时间】:2020-06-21 14:22:39
【问题描述】:
var canvas = document.createElement("canvas");
        b = canvas.getContext("2d");
        canvas.id = "canvas"
        
        canvas.width = 900;
        canvas.height = 600;
        
        document.body.appendChild(canvas);
        
        
        
        var posX =  430;
        posY = 300;
        
        var myArray = [-3.5,0,3.5];
        
        var dX = myArray[Math.floor(Math.random()*myArray.length)];
        var dY = myArray[Math.floor(Math.random()*myArray.length)];
      
       
        
        setInterval(function (){
                b.fillStyle = "steelblue";
                b.fillRect(0, 0, canvas.width, canvas.height);
                posX += dX;
                posY += dY;
            
                if (posX > 875){
                    dX = 0;
                    posX = 875;
                }
                if (posX < 5){
                    dx = 0;
                    posX = 5;
                }
                if (posY > 575){
                        dY = 0;
                        posY = 575;
                }
                if (posY < 5){
                        dY = 0;
                        posY = 5;
                }
        
        b.fillStyle = "snow";
        b.fillRect(posX, posY, 20, 20);
        }, 20)

这是我的全部代码。我想随机移动背景上的立方体。现在它只向一个随机方向移动。但我希望它每秒都改变这个方向。因为 dX 和 dY 必须每秒都在变化。

【问题讨论】:

标签: javascript arrays random


【解决方案1】:

这样做: 如果您对代码有任何疑问。随意写评论。

const canvas = document.createElement('canvas')
canvas.width = 100
canvas.height = 100
canvas.style.backgroundColor = 'steelblue'
document.body.appendChild(canvas)
const ctx = canvas.getContext('2d')

const RADIUS = 5
const SPEED = 0.6
let pos = [canvas.width / 2, canvas.height / 2]
let direction = [0, 0]

setInterval(function() {
    pos[0] += direction[0] * SPEED
    pos[1] += direction[1] * SPEED

    if(pos[0] <= RADIUS || pos[0] >= canvas.width - RADIUS ||
    pos[1] <= RADIUS || pos[1] >= canvas.height - RADIUS) {
        pos = [canvas.width / 2, canvas.height / 2] 
    }

    ctx.clearRect(0, 0, canvas.width, canvas.height)
    ctx.fillStyle = "snow"
    ctx.fillRect(pos[0] - RADIUS, pos[1] - RADIUS, 2 * RADIUS, 2 * RADIUS)
}, 20)

setInterval(function() {
    const randomAngle = Math.random() * 2 * Math.PI
    direction = [Math.cos(randomAngle), Math.sin(randomAngle)]
}, 1000)

【讨论】:

  • 一个问题:现在小方块碰到墙壁时会跳到中心。这是为什么呢?
  • 所以在 if 检查if(pos[0] &lt;= RADIUS || pos[0] &gt;= canvas.width - RADIUS || pos[1] &lt;= RADIUS || pos[1] &gt;= canvas.height - RADIUS) 中,我检查该框是否跳出。如果是这样,使用pos = [canvas.width / 2, canvas.height / 2] 传送到中间
  • 谢谢。假设我想给小方块一些更多的属性,比如一段时间后消失,一段时间后克隆。当您将正方形定义为变量时,我找不到代码中的确切位置。有什么方法可以给它一个 ID 以便我可以调用它的函数?(如克隆和消失
  • 对不起,我是一个尝试学习编码的初学者:/
  • 也许我可以使用这样的东西:var ctx_1 = ctx.cloneNode(true); ?
【解决方案2】:

使用setInterval()https://javascript.info/settimeout-setinterval

var myArray = [-3.5,0,3.5];
var dX, dY

setInterval(() => {
  dX = myArray[Math.floor(Math.random()*myArray.length)];
  dY = myArray[Math.floor(Math.random()*myArray.length)];
}, 1000)

【讨论】:

  • 我也试过了。但是我的飞机消失了,没有给我任何错误信息。
  • 哦,您想制作一款游戏,因此最好关闭该问题并打开一个新的 CLEAN 问题,在其中描述您想要做什么以及您的问题是什么。
  • 在这种情况下,您需要添加 var dX = myArray[Math.floor(Math.random()*myArray.length)]; var dY = myArray[Math.floor(Math.random()*myArray.length)];在你已经拥有的 setInterval 内。
【解决方案3】:

我个人会选择使用Array.prototype 添加一个方法,该方法将获取数组的随机元素,然后使用setInterval 每隔一秒更新值:

Array.prototype.getRandom = function() {
  let index = Math.floor(Math.random() * this.length);
  return this[index];
}

var myArray = [-3.5,0,3.5];
var dX, dY;

function updateDerivatives() {
  dX = myArray.getRandom(),
  dY = myArray.getRandom();
  console.log("Updated values:", { dX, dY });
}

setInterval(updateDerivatives, 1000);

【讨论】:

    【解决方案4】:
    var myArray = [-3.5,0,3.5];
    
    var dX;
    var dY;
    
    setInterval(() => { // you could also use setInterval(function(){...},...)
        dX = myArray[Math.floor(Math.random()*myArray.length)];
        dY = myArray[Math.floor(Math.random()*myArray.length)];
    }, 1000) // 1000 milliseconds
    

    【讨论】:

    • 我试过了。但是现在飞机已经消失了。但我没有收到任何错误消息
    猜你喜欢
    • 2010-12-10
    • 1970-01-01
    • 2020-10-04
    • 2016-02-26
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    相关资源
    最近更新 更多