Phaser 3 - 如何检测所有组件是否都在休眠?
Phaser 3 - How to detect if all components are sleeping?
我是 Phaser Framework 的新手,我想尝试从自上而下的角度制作一些 2D 台球游戏的原型。我现在遇到的问题是检测所有球是否在重新启动前停止移动。
我使用 Physics.Matter
,这里是目前 create
时的源代码:
this.matter.world.setBounds(0, 0, 720, 1280, 32, false, false, false, true);
this.add.image(400, 300, 'sky');
var ball = this.matter.add.image(360, 1000, 'ball');
ball.setCircle();
ball.setVelocity(-5, -20);
ball.setBounce(0.5);
for (var i = 1; i < 10; i++) {
var target = this.matter.add.image(Phaser.Math.Between(400,450), Phaser.Math.Between(400,450), 'target');
target.setCircle();
target.setVelocity(0, 0);
target.setBounce(0.7);
target.setFriction(0, 0.01);
target.setSleepEvents(true, true);
}
this.matter.world.on('sleepstart', function() {console.log('sleepstart');});
this.matter.world.on('sleepend', function() {console.log('sleepend');});
这会检测每个 target
是否都睡了,但我需要检测它们是否都停止了移动。我数不清有多少人睡过,因为有时当 target
进入睡眠状态时,其他 body 可能会反弹并再次唤醒它。
有没有办法全局检测它们?
编辑:作为后备计划,我添加了一个基本的 JS 函数,每当 update
被调用时都会被调用,并计算睡觉的身体,看起来 不应该 做个有道的:
var isActive = false;
// Some commands here that changes isActive = true
function onupdate() {
if (isActive) {
var bodyCount = this.matter.world.getAllBodies().filter(o => o.isSleeping === true).length;
console.log(bodyCount);
if (bodyCount >= 11) {
isActive = false;
}
}
}
我会把你想要跟踪的所有对象放入一个移相器组 (https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.Group.html) 并迭代组中的项目,看看是否都有 属性 isSleeping
设置为 true
;
Warning: I can't say how performant this solution is, youre use case. If it is too slow, I would setup a counter variable, and count it down / up on sleepstart
and sleepend
. And when the counter is 0
all are sleeping.
这是一个工作演示,我会怎么做:
(解释在代码中,作为注释)
// fix to prevent 'Warnings' in Whosebug console
console.warn = _ => _
var config = {
type: Phaser.AUTO,
width: 400,
height: 100,
scene: { create },
physics: {
default: 'matter',
matter: {
debug: true,
setBounds: {
x: 0,
y: 0,
width: 400,
height: 100
},
enableSleeping: true
}
}
};
function create(){
// create the Phaser Group
this.targets = this.add.group();
for (var i = 1; i < 10; i++) {
var target = this.matter.add.image(200, 0, 10, 10, 'target');
target.setCircle();
target.setBounce(0.7);
target.setFriction(0, 0.01);
target.setSleepEvents(true, true);
// Add Item to the Group
this.targets.add(target);
}
this.matter.world.on('sleepstart', function(event, item){
// Check all targets are sleeping
if(!this.targets.getChildren().some( target => !target.body.isSleeping)){
console.log('all are sleeping');
}
}, this); // <- pass the scene as context
this.matter.world.on('sleepend', function() {console.log('sleepend');});
}
var game = new Phaser.Game(config);
<script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>
我是 Phaser Framework 的新手,我想尝试从自上而下的角度制作一些 2D 台球游戏的原型。我现在遇到的问题是检测所有球是否在重新启动前停止移动。
我使用 Physics.Matter
,这里是目前 create
时的源代码:
this.matter.world.setBounds(0, 0, 720, 1280, 32, false, false, false, true);
this.add.image(400, 300, 'sky');
var ball = this.matter.add.image(360, 1000, 'ball');
ball.setCircle();
ball.setVelocity(-5, -20);
ball.setBounce(0.5);
for (var i = 1; i < 10; i++) {
var target = this.matter.add.image(Phaser.Math.Between(400,450), Phaser.Math.Between(400,450), 'target');
target.setCircle();
target.setVelocity(0, 0);
target.setBounce(0.7);
target.setFriction(0, 0.01);
target.setSleepEvents(true, true);
}
this.matter.world.on('sleepstart', function() {console.log('sleepstart');});
this.matter.world.on('sleepend', function() {console.log('sleepend');});
这会检测每个 target
是否都睡了,但我需要检测它们是否都停止了移动。我数不清有多少人睡过,因为有时当 target
进入睡眠状态时,其他 body 可能会反弹并再次唤醒它。
有没有办法全局检测它们?
编辑:作为后备计划,我添加了一个基本的 JS 函数,每当 update
被调用时都会被调用,并计算睡觉的身体,看起来 不应该 做个有道的:
var isActive = false;
// Some commands here that changes isActive = true
function onupdate() {
if (isActive) {
var bodyCount = this.matter.world.getAllBodies().filter(o => o.isSleeping === true).length;
console.log(bodyCount);
if (bodyCount >= 11) {
isActive = false;
}
}
}
我会把你想要跟踪的所有对象放入一个移相器组 (https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.Group.html) 并迭代组中的项目,看看是否都有 属性 isSleeping
设置为 true
;
Warning: I can't say how performant this solution is, youre use case. If it is too slow, I would setup a counter variable, and count it down / up on
sleepstart
andsleepend
. And when the counter is0
all are sleeping.
这是一个工作演示,我会怎么做:
(解释在代码中,作为注释)
// fix to prevent 'Warnings' in Whosebug console
console.warn = _ => _
var config = {
type: Phaser.AUTO,
width: 400,
height: 100,
scene: { create },
physics: {
default: 'matter',
matter: {
debug: true,
setBounds: {
x: 0,
y: 0,
width: 400,
height: 100
},
enableSleeping: true
}
}
};
function create(){
// create the Phaser Group
this.targets = this.add.group();
for (var i = 1; i < 10; i++) {
var target = this.matter.add.image(200, 0, 10, 10, 'target');
target.setCircle();
target.setBounce(0.7);
target.setFriction(0, 0.01);
target.setSleepEvents(true, true);
// Add Item to the Group
this.targets.add(target);
}
this.matter.world.on('sleepstart', function(event, item){
// Check all targets are sleeping
if(!this.targets.getChildren().some( target => !target.body.isSleeping)){
console.log('all are sleeping');
}
}, this); // <- pass the scene as context
this.matter.world.on('sleepend', function() {console.log('sleepend');});
}
var game = new Phaser.Game(config);
<script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>