前提
オブジェクトの呼び出しはできるが代入はできない
実現したいこと
- オブジェクトを let で代入したい
発生している問題・エラーメッセージ
/workspace/Rouge-like:118
let width = oldChunk.w - splitX;
^
エラーメッセージ
/workspace/Rouge-like:118
let width = oldChunk.w - splitX;
^
TypeError: Cannot read properties of undefined (reading 'w')
at splitDangeon (/workspace/Rouge-like:118:34)
at splitDangeon (/workspace/Rouge-like:156:24)
at splitDangeon (/workspace/Rouge-like:156:24)
at debugChunk (/workspace/Rouge-like:191:9)
at /workspace/Rouge-like:195:5
at Object.<anonymous> (/workspace/Rouge-like:197:3)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
該当のソースコード
javascript
(function(){
'use strict';
const CHUNK_RANGE = 8; const CHUNK_SPLIT_MIN = 4; var tiles = []; let getRandom = (min, max) => { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min) + min); }; let setTile = (x, y, tileId) => { try{ tiles[x + ', ' + y] = tileId; }catch{ //ignored } }; let getTile = (x, y) => { try{ return tiles[x + ', ' + y]; }catch{ return false; } }; let initTiles = (xChunkLoop, yChunkLoop) => { for(let x = 0; x < CHUNK_RANGE * xChunkLoop; x++) { for(let y = 0; y < CHUNK_RANGE * yChunkLoop; y++) { setTile(x, y, 0); } } }; let generatePass = (xPos, yPos, direction, length = 0) => { let i = 0; switch(direction){ case 0: for(; i < length; i++) setTile(xPos, yPos++, 1); break; case 1: for(; i < length; i++) setTile(xPos++, yPos, 1); break; case 2: for(; i < length; i++) setTile(xPos, yPos--, 1); break; case 3: for(; i < length; i++) setTile(xPos--, yPos, 1); break; } }; let generateRoom = (xPos, yPos, width, height) => { width = width / 2 | 0; height = height / 2 | 0; for(let x = -width + xPos; x <= width + xPos; x++) { for(let y = -height + yPos; y <= height + yPos; y++) { setTile(x, y, 1); } } }; var chunks = []; let initChunk = (xChunkLoop, yChunkLoop) => { let chunk = { x:0, y:0, w:xChunkLoop, h:yChunkLoop }; chunks = [chunk]; return chunks; }; let splitDangeon = (oldChunk, count = 0) => { if(Math.random() > 0.5) {//縦 let splitX = CHUNK_SPLIT_MIN; for(let i = 0; Math.random() > i; i += 0.1) splitX++; let width = oldChunk.w - splitX; if(width > CHUNK_SPLIT_MIN){ chunks[count + 1] = {//new chunk x:oldChunk.x, y:oldChunk.y, w:splitX, h:oldChunk.h }; chunks[count] = {//old chunk x:splitX, y:oldChunk.y, w:width, h:oldChunk.h }; } } else {//横 let splitY = CHUNK_SPLIT_MIN; for(let i = 0; Math.random() > i; i += 0.1) splitY++; let height = oldChunk.h - splitY; if(height > CHUNK_SPLIT_MIN){ chunks[count + 1] = {//new chunk x:oldChunk.x, y:oldChunk.y, w:oldChunk.w, h:splitY }; chunks[count] = {//old chunk x:oldChunk.x, y:splitY, w:oldChunk.w, h:height }; } } count++; if(count < 10) splitDangeon(chunks[count], count); }; let generateDangeon = () => { }; let debug = (xChunkLoop, yChunkLoop) => { initTiles(xChunkLoop, yChunkLoop); generateRoom(10, 41, 5, 11); let map = []; for(let x = 0; x < xChunkLoop * CHUNK_RANGE; x++){ if(x % (xChunkLoop * CHUNK_RANGE) != 0) { map.push('\n'); } for(let y = 0; y < yChunkLoop * CHUNK_RANGE; y++){ switch(tiles[x + ', ' + y]) { case 0: map.push('■'); break; case 1: map.push('□'); break; } } } console.log(map.join('')); console.log('\n'); }; let debugChunk = (xChunkLoop, yChunkLoop) => { let chunk = initChunk(xChunkLoop, yChunkLoop); splitDangeon(chunk[0]); console.log(chunks); }; debugChunk(32, 32)
})();
試したこと
oldchunk.w や oldchunk.h 自体の呼び出しはできるが
let で代入はできない。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2022/09/11 07:39
退会済みユーザー
2022/09/11 08:00
退会済みユーザー
2022/09/11 09:00
退会済みユーザー
2022/09/11 11:50
退会済みユーザー
2022/09/11 13:42
退会済みユーザー
2022/09/12 14:11
退会済みユーザー
2022/09/12 21:25