【问题标题】:Get coordinate with NaN at x axis在 x 轴上获取与 NaN 的坐标
【发布时间】:2022-08-03 22:53:39
【问题描述】:

我得到以下代码块,每 1.5 生成一个新敌人。

使用扫描运算符将每个新敌人添加到数组中

我做了建议的替换。

我做了一个小改动以便能够复制

const enemies$ = rxjs.from([0,1])
    .pipe(
        rxjs.scan( (enemyArray) => {
            const enemy = {
                x: Math.floor(Math.random() * 100),
                y: -30
            }
            console.log(enemy)
            enemyArray.push(enemy);
            console.log(enemyArray); //debug.
            return enemyArray;
        }, [])
    );

    enemies$.subscribe(
      (enemies) => console.log(enemies) 
    );

第一个元素(敌人)在控制台中的结果如下

{x: 312, y: -30}

但是当将敌人添加到enemyArray 时,控制台中会显示以下结果

(1)[{...}]
0: {x: NaN, y: 515}
1: {x: NaN, y: 65}
length: 2
[[Prototype]]: Array(0)

  • 你为什么试图从一个数字的字符串中解析一个数字? Math.round(Math.random() * canvas.width)
  • canvas.width 有什么价值?
  • 画布是全局变量。 const canvas = document.createElement(\'canvas\');
  • 我的问题是,canvas.width 有什么价值?不是canvas
  • canvas.width = window.innerWidth; canvas.height = window.innerHeight;

标签: rxjs


【解决方案1】:

parseInt() 将字符串作为第一个参数,所以它应该是

parseInt(String(Math.random() * 100), 10)

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2020-02-29
  • 1970-01-01
  • 2016-03-15
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 2015-05-23
  • 1970-01-01
相关资源
最近更新 更多