热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

似乎不能在javascript中添加两个整数?-Cannotseemtoaddtwointegersinjavascript?

ImbuildingJohnConwaysgameoflifewithHTML5canvas.IthasagameOfLifeobjectwhichcontainsa

Im building John Conways game of life with HTML5 canvas. It has a gameOfLife object which contains an array with all the cells and their states (dead/alive). Here is the code that builds a cell:

我正在用HTML5 canvas构建John Conways的生活游戏。它有一个gameOfLife对象,它包含一个包含所有细胞及其状态(死/活)的数组。下面是构建单元格的代码:

function cell(x,y){
    this.x = x;
    this.y = y;
    this.isAlive = false;
}

I am exploring ways of checking a cells surrounding cells states. As I understand, one way is to iterate through the array and find a cell with coordinates that match as being around the currently checked cell.

我正在探索检查细胞周围状态的方法。正如我所理解的,一种方法是遍历数组并找到与当前检查单元格周围的坐标匹配的单元格。

I was thinking of going about a different way. By adding and subtracting the number of cells (with small variations of +1 and -1) on the Y (and the X) axis from the index of the cell being evaluated, you should be able to come up with the index of any top left, left, bottom left, top right, right, bottom right cell.

我想换一种方式。通过增加和减少细胞的数量(用小的变化+ 1和- 1)在Y轴(X)指数的细胞被评估,您应该能够想出任何左上角的指数,左,左下角,右上角,右下角单元格。

I haven't been able to test this idea though, as it doesn't let me get the desired index:

我还没能测试这个想法,因为它没有让我得到想要的指数:

So, in my update loop:

所以,在我的更新循环中:

//I know that there is a cell at the index of exampleIndex + cellsY
exampleIndex = 200;

game.getLivingNeighbours(exampleIndex);


function getLivingNeighbours(i){

    console.log(i) //Logs an integer
    console.log(grid.cellsY) //Logs an integer
    console.log(game.cells[i + grid.cellsY]); //Undefined?!

}

1 个解决方案

#1


5  

There can be two reasons:

有两个原因:

  1. In Javascript variables are of loose-type that's why its good to parse int before arithmetic operation.

    在Javascript中,变量是松类型的,这就是为什么在算术运算之前解析int是很好的。

    try:

    试一试:

    console.log(game.cells[parseInt(i,10) + parseInt(grid.cellsY,10)]);
    
  2. You are trying to access an array, you need to check whether parseInt(i,10) + parseInt(grid.cellsY,10) index exist in your array or not.

    您正在尝试访问一个数组,您需要检查parseInt(i,10) + parseInt(grid.cellsY,10)索引是否存在于数组中。


推荐阅读
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Ihavethefollowingonhtml我在html上有以下内容<html><head><scriptsrc..3003_Tes ... [详细]
  • 本文记录了在vue cli 3.x中移除console的一些采坑经验,通过使用uglifyjs-webpack-plugin插件,在vue.config.js中进行相关配置,包括设置minimizer、UglifyJsPlugin和compress等参数,最终成功移除了console。同时,还包括了一些可能出现的报错情况和解决方法。 ... [详细]
  • 本文讨论了微软的STL容器类是否线程安全。根据MSDN的回答,STL容器类包括vector、deque、list、queue、stack、priority_queue、valarray、map、hash_map、multimap、hash_multimap、set、hash_set、multiset、hash_multiset、basic_string和bitset。对于单个对象来说,多个线程同时读取是安全的。但如果一个线程正在写入一个对象,那么所有的读写操作都需要进行同步。 ... [详细]
  • 本文介绍了在满足特定条件时如何在输入字段中使用默认值的方法和相应的代码。当输入字段填充100或更多的金额时,使用50作为默认值;当输入字段填充有-20或更多(负数)时,使用-10作为默认值。文章还提供了相关的JavaScript和Jquery代码,用于动态地根据条件使用默认值。 ... [详细]
  • 本文介绍了使用FormData对象上传文件同时附带其他参数的方法。通过创建一个表单,将文件和参数添加到FormData对象中,然后使用ajax发送POST请求进行文件上传。在发送请求时,需要设置processData为false,告诉jquery不要处理发送的数据;同时设置contentType为false,告诉jquery不要设置content-Type请求头。 ... [详细]
  • Introduction(简介)Forbeingapowerfulobject-orientedprogramminglanguage,Cisuseda ... [详细]
  • IhaveawebapplicationthatusesanActiveXCOMcomponent,forexample:我有一个使用ActiveXCOM组件的Web应用程 ... [详细]
  • [JavaScript] 多数前端工程师都没注意到的一个关于console.log()的坑
    [JavaScript]多数前端工程师都没注意到的一个关于console.log()的坑请阅读以下代码并猜测结果:functiontest(){le ... [详细]
  • JavaScript - let和var区别
    前提ES5只有函数作用域和全局作用域,var属于ES5。let属于ES6,新增块级作用域。目的是可以写更安全的代码。Theletstatementdeclaresablocks ... [详细]
  • Android Studio Bumblebee | 2021.1.1(大黄蜂版本使用介绍)
    本文介绍了Android Studio Bumblebee | 2021.1.1(大黄蜂版本)的使用方法和相关知识,包括Gradle的介绍、设备管理器的配置、无线调试、新版本问题等内容。同时还提供了更新版本的下载地址和启动页面截图。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • 树莓派语音控制的配置方法和步骤
    本文介绍了在树莓派上实现语音控制的配置方法和步骤。首先感谢博主Eoman的帮助,文章参考了他的内容。树莓派的配置需要通过sudo raspi-config进行,然后使用Eoman的控制方法,即安装wiringPi库并编写控制引脚的脚本。具体的安装步骤和脚本编写方法在文章中详细介绍。 ... [详细]
author-avatar
mobiledu2502905163
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有