Javascript 移相器——如何检查精灵是否在移动?
我试图在update()函数中检查球精灵是否在被抛出后停止移动。我尝试了以下方法:Javascript 移相器——如何检查精灵是否在移动?,javascript,html,phaser-framework,Javascript,Html,Phaser Framework,我试图在update()函数中检查球精灵是否在被抛出后停止移动。我尝试了以下方法: if (ball.body.velocity.x < 1 && ball.body.velocity.y < 1) { alert("ball not moving"); } if(ball.body.velocity.x
if (ball.body.velocity.x < 1 && ball.body.velocity.y < 1) {
alert("ball not moving");
}
if(ball.body.velocity.x<1&&ball.body.velocity.y<1){
警惕(“球不动”);
}
但是,即使两种情况的评估结果都不是真的,也会触发警报。(意思是球还在移动……)
这是我的密码
game.js
window.onload = function() {
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('table', 'assets/img/table.png');
game.load.image('cup', 'assets/img/cup.png');
game.load.image('ball', 'assets/img/ball.png');
}
var table;
var cups;
var cup;
var ball;
var ballZ = 100;
var ballAscending = false;
var ballThrown = false;
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
table = game.add.sprite(game.world.centerX, game.world.centerY, 'table');
table.anchor.setTo(0.5,0.5);
var cupW = game.cache.getImage('cup').width;
var cupH = game.cache.getImage('cup').height;
cups = game.add.group(game,game.world,'cups',false,true,Phaser.Physics.ARCADE);
cup = cups.create(game.world.centerX, cupH / 2, 'cup');
cup = cups.create(game.world.centerX - cupW, cupH / 2, 'cup');
cup = cups.create(game.world.centerX + cupW, cupH / 2, 'cup');
cup = cups.create(game.world.centerX - cupW / 2, cupH + (cupH / 2), 'cup');
cup = cups.create(game.world.centerX + cupW / 2, cupH + (cupH / 2), 'cup');
cup = cups.create(game.world.centerX, (cupH * 2) + (cupH / 2), 'cup');
cup = cups.create(game.world.centerX, game.world.height - (cupH / 2), 'cup');
cup = cups.create(game.world.centerX - cupW, game.world.height - (cupH / 2), 'cup');
cup = cups.create(game.world.centerX + cupW, game.world.height - (cupH / 2), 'cup');
cup = cups.create(game.world.centerX - cupW / 2, game.world.height - (cupH / 2) - cupH, 'cup');
cup = cups.create(game.world.centerX + cupW / 2, game.world.height - (cupH / 2) - cupH, 'cup');
cup = cups.create(game.world.centerX, game.world.height - (cupH / 2) - (cupH * 2), 'cup');
ball = game.add.sprite(game.world.centerX, game.world.centerY + (cupH*4),'ball');
ball.anchor.setTo(0.5,0.5);
ball.inputEnabled = true;
game.physics.enable([ball, cups],Phaser.Physics.ARCADE);
cups.forEach(function(item) {
item.anchor.setTo(0.5);
item.body.immovable = true;
},this);
ball.body.bounce.set(0.50);
ball.body.drag.set(100);
game.stage.backgroundColor = "#d3d3d3";
game.input.onDown.add(onDown,this);
game.input.onUp.add(throwBall,this);
}
var clickTime;
function onDown() {
clickTime = game.time.time;
}
function throwBall() {
var delta = game.time.time - clickTime;
game.physics.arcade.velocityFromAngle(ball.angle, delta, ball.body.velocity);
ballThrown = true;
}
function update() {
if (ballThrown) {
game.physics.arcade.collide(cups,ball);
if (ballAscending) {
ballZ = ballZ + 1;
if (ballZ > 100) {
ballAscending = false;
}
} else {
ballZ = ballZ - 1;
if (ballZ < 1) {
ballAscending = true;
}
}
ball.scale.set((ballZ + 100) / 100);
if (ball.body.velocity.x < 1 && ball.body.velocity.y < 1) {
alert("ball stopped moving");
}
}
ball.rotation = game.physics.arcade.angleToPointer(ball);
}
function render() {
game.debug.spriteInfo(ball,32,32);
}
}
window.onload=function(){
var game=new Phaser.game(800600,Phaser.AUTO,,{preload:preload,create:create,update:update,render:render});
函数预加载(){
game.load.image('table','assets/img/table.png');
game.load.image('cup','assets/img/cup.png');
game.load.image('ball','assets/img/ball.png');
}
var表;
var杯;
var杯;
var球;
var-ballZ=100;
var压载=假;
var=false;
函数create(){
游戏。物理。启动系统(相位器。物理。拱廊);
table=game.add.sprite(game.world.centerX,game.world.centerY,'table');
表.锚定值设定值(0.5,0.5);
var cupW=game.cache.getImage('cup')。宽度;
var cupH=game.cache.getImage('cup').height;
cups=game.add.group(game,game.world,'cups',false,true,Phaser.Physics.ARCADE);
cup=cups.create(game.world.centerX,cupH/2,“cup”);
cup=cups.create(game.world.centerX-cupW,cupH/2,'cup');
cup=cups.create(game.world.centerX+cupW,cupH/2,'cup');
cup=cups.create(game.world.centerX-cupW/2,cupH+(cupH/2),“cup”);
cup=cups.create(game.world.centerX+cupW/2,cupH+(cupH/2),“cup”);
cup=cups.create(game.world.centerX,(cupH*2)+(cupH/2),“cup”);
cup=cups.create(game.world.centerX,game.world.height-(cupH/2),“cup”);
cup=cups.create(game.world.centerX-cupW,game.world.height-(cupH/2),“cup”);
cup=cups.create(game.world.centerX+cupW,game.world.height-(cupH/2),“cup”);
cup=cups.create(game.world.centerX-cupW/2,game.world.height-(cupH/2)-cupH,“cup”);
cup=cups.create(game.world.centerX+cupW/2,game.world.height-(cupH/2)-cupH,“cup”);
cup=cups.create(game.world.centerX,game.world.height-(cupH/2)-(cupH*2),“cup”);
ball=game.add.sprite(game.world.centerX,game.world.centerY+(cupH*4),“ball”);
球.锚.设为(0.5,0.5);
ball.inputenable=true;
游戏。物理。启用([球,杯],移相器。物理。街机);
cups.forEach(功能(项目){
项目锚定设置为(0.5);
item.body.immovable=真;
},这个);
球。身体。弹跳。设置(0.50);
球.体.拖.集(100);
game.stage.backgroundColor=“#d3”;
game.input.onDown.add(onDown,this);
game.input.onUp.add(throwBall,this);
}
var点击时间;
函数onDown(){
单击时间=game.time.time;
}
函数throwBall(){
var delta=game.time.time-点击时间;
游戏。物理。拱廊。速度浪漫(球。角度,三角洲,球。身体。速度);
ballshown=true;
}
函数更新(){
如果(投球){
游戏。物理。街机。碰撞(杯子,球);
if(民谣){
鲍尔兹=鲍尔兹+1;
如果(鲍尔兹>100){
民谣=假;
}
}否则{
ballZ=ballZ-1;
if(ballZ<1){
民谣=真实;
}
}
球刻度盘组((ballZ+100)/100);
if(ball.body.velocity.x<1&&ball.body.velocity.y<1){
警惕(“球停止移动”);
}
}
ball.rotation=游戏、物理、街机、angleToPointer(球);
}
函数render(){
游戏.调试.精灵信息(球,32,32);
}
}
index.html
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8" />
<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<script src="js/phaser.min.js"></script>
<script src="js/game.js"></script>
</body>
</html>
我发现了问题,我的if语句条件应该是
if (Math.abs(ball.body.velocity.x) < 1 && Math.abs(ball.body.velocity.y) < 1) {
alert("ball stopped moving");
}
if(Math.abs(ball.body.velocity.x)<1&&Math.abs(ball.body.velocity.y)<1){
警惕(“球停止移动”);
}
这是可行的,但如果有人知道一种更干净的方法来实现这一点,请随意分享。对不起,我迟到了四个月,但你可以使用Phaser.Point“equals”方法来检查身体的速度是否等于(0,0)点
如果0.5
if (Phaser.Point.equals(ball.body.velocity,new Phaser.Point(0,0) ) ){
console.log("You ain't goin' anywhere!");
}