phaser-framework - Phaser 3 场景 input.on() 触摸事件未触发
问题描述
我在角度组件(离子项目)的构造函数中有以下设置。
let config = {
type: Phaser.AUTO,
width: '100vw',
height: '100vh',
transparent:true,
disableContextMenu: true,
parent: 'phaser-example',
physics: {
default: 'arcade',
arcade: {
gravity: { y: 200 }
}
},
scene: {
preload: function() {
that.preload(this);
},
create: function() {
that.create(this);
},
update: function() {
that.update(this);
}
}
};
this.game = new Phaser.Game(config);
我在 create 方法中为单击/触摸事件设置了一个侦听器,如下所示:
create(scene){
scene.input.on('pointerup', this.moveSourceSprite, scene);
}
但是,永远不会调用 moveSourceSprite。当我在外面收听指针并单击游戏时,会调用 moveSourceSprite。
我想,由于某种原因,场景没有听整个游戏区域。
我对 Phaser 是全新的并且停留在这一点上。如果你能帮助我,我将不胜感激。
谢谢,
道格
解决方案
var myScene1 = {
key:'scene1',
preload: function(){
this.load.image('alien1', 'sprites/phaser-alien.png');;
},
create: function(){
this.add.image(100, 100, 'alien1');
this.input.on('pointerup', ()=>{game.scene.start('scene2'); game.scene.stop('scene1')}, myScene1);
},
}
var myScene2 = {
key:'scene2',
preload: function(){
this.load.image('alien2', 'sprites/alien2.png');;
},
create: function(){
this.add.image(100, 100, 'alien2');
this.input.on('pointerup', ()=>{game.scene.start('scene1'); game.scene.stop('scene2')}, myScene2);
},
}
var config = {
type: Phaser.AUTO,
width: 600,
height: 600,
loader: {
baseURL: 'https://raw.githubusercontent.com/nazimboudeffa/assets/master/',
crossOrigin: 'anonymous'
},
parent: 'phaser-example',
scene: [myScene1, myScene2]
};
var game = new Phaser.Game(config);
game.scene.start('scene1');
<script src="//cdn.jsdelivr.net/npm/phaser@3.19.0/dist/phaser.js"></script>
推荐阅读
- angular - Angular 在 app.module 与 child.module 中导入 FormsModule
- sql-server - 对 SQL Server 中的序列函数感到困惑
- c - 为什么 printf 说明符格式 %n 不起作用?
- python - 使用归并排序原理
- javascript - 如何使用 ajax 在 wordpress 文件中调用 php 文件?
- python - Tensorflow:如何加载预训练的 ResNet 模型
- svg - 如何在 svg 文档中以编程方式创建 svg 元素?
- java - Java 编译器会在编译期间计算常量表达式吗?
- arrays - 在 loadrunner 中传递 c 数组
- java - 如何摆脱这个错误'将 findViewById 转换为 EditText 是多余的'?