我正试着用Java脚本制作我自己的世界编辑器.我正在使用一个单独的瓷砖文件夹,并将它们放在屏幕上.然后当你按下瓷砖的时候,你基本上就 Select 了它.这是有效的,但当指针从瓷砖拾取切换到瓷砖放置时,我会得到这个错误.

我已经判断了是否一切都定义好了,但我可能遗漏了一些东西

更新:我已经更改了箭头函数以使用正常的函数,但它仍然不起作用,并给出了相同的错误,现在我提供了整个文件代码

i have encountered this个 Console.log(this.map) VM10362:1未定义 未定义

These are the errors aka stack trace i think

这是我的代码

class WorldEditor extends Phaser.Scene {
    constructor() {
        super("editWorld")
        this.tileSize = 48
        this.tileIndex = 0
        this.tileFolder = 'Assets/Tiles/'
        this.tilesPerRow = 37
        this.totalTiles = 160
        this.tilePicked = false
    }

    preload() {
        // Load tile images from the folder
        for (let i = 0; i < this.totalTiles; i++) {
            this.load.image('tile (' + i + ')', this.tileFolder + 'tile (' + i + ').png')
        }
    }

    create() {
        const tileWidth = this.tileSize
        const tileHeight = this.tileSize
        const tilesPerRow = this.tilesPerRow
    
        if(!this.map){
            this.map = this.make.tilemap({ 
                tileWidth: this.tileSize, 
                tileHeight: this.tileSize,
                width: 1200,
                height: 720
            })  
        }
        
        for (let i = 0; i < this.totalTiles; i++) {
            const x = ((i % tilesPerRow) * tileWidth)/1.48
            const y = (Math.floor(i / tilesPerRow) * tileHeight)/1.48
    
            this.tile = this.add.image(x, y, 'tile (' + i + ')')
            this.tile.setOrigin(0)
            this.tile.setScale((tileWidth / this.tile.width)/1.48, (tileHeight / this.tile.height)/1.48)
        }
    
        // Set up pointer events
        this.setupPointerEvents()
        
        // Keyboard event to switch back to tile selection
        this.input.keyboard.on('keydown-F', () => {
            this.tilePicked = false
            console.log('Switched back to tile selection')
            this.setupPointerEvents() // Update pointer events after switching back
        })
    }
    
    setupPointerEvents() {
        this.input.off('pointerdown')
    
        if (!this.tilePicked) {
            const tiles = this.children.list.filter(child => child.texture && child.texture.key.startsWith('tile ('))
            tiles.forEach((tile, i) => {
                tile.setInteractive()
                tile.on('pointerdown', function() {
                    this.tileIndex = i
                    this.tilePicked = true
                    tile.setTint(0xff0000)
                    console.log('Selected tile index:', this.tileIndex)
                    this.setupPointerEvents()
                }, this)
            })
        } else {
            this.input.on('pointerdown', function(pointer) {
                this.tileX = Math.floor(pointer.x / this.tileSize)
                this.tileY = Math.floor(pointer.y / this.tileSize)
                this.placeTile(this.tileX, this.tileY)
            }, this)
        }
    }
    // Other methods...
    
    placeTile(tileX, tileY) {
        this.map.putTileAt(this.tileIndex, tileX, tileY) 
    }

    // Implement saveMap and loadMap functions to save and load level data
}

推荐答案

主要问题是没有创建任何层.创建一个层(使用平铺),应该不会有任何错误.

btw.:箭头功能和this示波器在此特定情况下是正确的(phaser development).

Mini Example that solves the issue:
(I might update it to make it look better)
Just click the white square. and check the Browser console

document.body.style = 'margin:0;';
console.clear();

class MainScene extends Phaser.Scene{
    create () {
        this.add.text(10,10, 'DEMO TEXT')
        const tileHeight = 10
        const tilesPerRow = 3
        
        // just for the demo -- START
        let graphics  = this.make.graphics();
        graphics.fillStyle(0xffffff);
        graphics.fillRect(0, 0, 100, 100);
        graphics.generateTexture('tileset', 100, 100);
        // just for the demo -- END        
        
        if(!this.map){
            this.map = this.make.tilemap({ 
                tileWidth: tileHeight, 
                tileHeight: tileHeight,
                width: config.width,
                height: config.height
            })  
            
            const tileset = this.map.addTilesetImage('tileset', null, 10, 10);
            this.layer = this.map.createBlankLayer('layer1', tileset);
        }
        
        for (let i = 0; i < 9 ; i++) {
            const x = ((i % tilesPerRow) * tileHeight)
            const y = (Math.floor(i / tilesPerRow) * tileHeight)
    
            // just for the demo
            graphics.generateTexture('tile (' + i + ')', 10, 10);
            
            this.tile = this.add.image(x, y, 'tile (' + i + ')')
            this.tile.setOrigin(0)
            this.tile.setScale(10, 10)
        }
    
        // Set up pointer events
        this.setupPointerEvents()
        
        // Keyboard event to switch back to tile selection
        this.input.keyboard.on('keydown-F', () => {
            this.tilePicked = false
            console.log('Switched back to tile selection')
            this.setupPointerEvents() // Update pointer events after switching back
        })
   
    }
    
     setupPointerEvents() {
        this.input.off('pointerdown')
    
        if (!this.tilePicked) {
            const tiles = this.children.list.filter(child => child.texture && child.texture.key && (/tile \(/).test(child.texture.key))
            tiles.forEach((tile, i) => {
                tile.setInteractive()
                tile.on('pointerdown', () => {
                    this.tileIndex = i
                    this.tilePicked = true
                    tile.setTint(0xff0000)
                    console.log('Selected tile index:', this.tileIndex)
                    this.setupPointerEvents()
                })
            })
        } else {
            this.input.on('pointerdown', (pointer) => {
                const tileX = Math.floor(pointer.x / 10)
                const tileY = Math.floor(pointer.y / 10)
                this.placeTile(tileX, tileY)
            })
        }
    }
    
    placeTile(tileX, tileY) {
        console.info('tileplaced')
        this.map.putTileAt(this.tileIndex, tileX, tileY, this.layer) 
    }
}

var config = {
    width: 536,
    height: 183,
    scene: MainScene
}; 



new Phaser.Game(config);
<script src="//cdn.jsdelivr.net/npm/phaser/dist/phaser.min.js"></script>

Javascript相关问答推荐

是否有方法在OpenWeatherMap API中获取过go 的降水数据?

我试图实现用户验证的reduxstore 和操作中出了什么问题?

窗口.getComputedStyle()在MutationObserver中不起作用

Google Apps脚本中的discord邀请API响应的日期解析问题

MongoDB中的引用

将自定义排序应用于角形数字数组

如何粗体匹配的字母时输入搜索框使用javascript?

如何通过使用vanilla JS限制字体大小增加或减少两次来改变字体大小

优化Google Sheet脚本以将下拉菜单和公式添加到多行

将核心模块导入另一个组件模块时存在多个主题

在VS代码上一次设置多个变量格式

删除加载页面时不存在的元素(JavaScript)

将范围 Select 器添加到HighChart面积图

Django模板中未加载JavaScript函数

使用Perl Selify::Remote::Driver执行Java脚本时出错

更新文本区域行号

KeyboardEvent:检测到键具有打印的表示形式

ReactJS在类组件中更新上下文

将字符串解释为数字;将其重新编码为另一个基数

如何在css中裁剪成一定Angular 的圆的一部分,而不需要复杂的多边形