第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

每秒隨機(jī)從數(shù)組中選擇一個(gè)新數(shù)字

每秒隨機(jī)從數(shù)組中選擇一個(gè)新數(shù)字

慕尼黑8549860 2022-10-13 10:48:33
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)這就是我的全部代碼。我想隨機(jī)移動(dòng)背景上的立方體?,F(xiàn)在它只向一個(gè)隨機(jī)方向移動(dòng)。但我希望它每秒都改變這個(gè)方向。因?yàn)?dX 和 dY 必須每秒改變一次。
查看完整描述

4 回答

?
慕的地10843

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊

這樣做:如果您對(duì)代碼有任何疑問。隨意寫評(píng)論。


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)


查看完整回答
反對(duì) 回復(fù) 2022-10-13
?
守著一只汪

TA貢獻(xiàn)1872條經(jīng)驗(yàn) 獲得超4個(gè)贊

我個(gè)人會(huì)選擇使用Array.prototype添加一個(gè)方法來獲取數(shù)組的隨機(jī)元素,然后使用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);


查看完整回答
反對(duì) 回復(fù) 2022-10-13
?
江戶川亂折騰

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊

使用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)


查看完整回答
反對(duì) 回復(fù) 2022-10-13
?
藍(lán)山帝景

TA貢獻(xiàn)1843條經(jīng)驗(yàn) 獲得超7個(gè)贊

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


查看完整回答
反對(duì) 回復(fù) 2022-10-13
  • 4 回答
  • 0 關(guān)注
  • 136 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)