移相器:精灵重叠时显示文本 [英] Phaser: show text when sprites overlap

查看:69
本文介绍了移相器:精灵重叠时显示文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一种方法,当我的两个精灵重叠时,将文本添加到视口的左上角.其中一个是物品,另一个是我的角色.单击键时,我已经可以检测到重叠,甚至拾取"该项目(杀死精灵).但是,我想要一个文本,上面写着单击"E"来选择剑!"之类的文字!在碰撞功能处于活动状态时出现,当我通过拾起精灵杀死精灵时,文字将消失.

I am trying to work out a way to add a text to the top left corner of the viewport when two of my sprites overlap. One of them is an item and the other is my character. I can already detect for overlap and even "pick" the item (kill the sprite) when I click a key. However I would like that a text saying something like "Click "E" to pick the sword!" appeared while the collision function is active and when I kill the sprite by picking it up the text would vanish.

我尝试将文本包含在碰撞函数本身中,但我想以此方式可以多次渲染文本(fps下降很多),我只想创建一次并根据我的目的将其删除.我的代码:

I tried including the text in the collision function itself but I suppose this way I am rendering the text multiple times (the fps drops a lot) and I just want to create it once and remove it according to my purposes. My code:

function collisionHandler(dude,the_sword) {
pickObject.onDown.add(function () {
    the_sword.kill();
}, this);
}

game.physics.isoArcade.overlap(dude, the_sword, collisionHandler, null, this);

// message saying to pick // Where to put this?
var style = { font: "30px Arial", fill: "#ff0044"};
var pick_message = this.game.add.text(0,20,"Click 'E' to pick up the sword!",style);
pick_message.fixedToCamera = true;

关于如何执行此操作的任何想法?

Any idea on how to do this?

推荐答案

在您的创建"功能中:

var style = { font: "30px Arial", fill: "#ff0044"};
var pick_message = this.game.add.text(0,20,"Click 'E' to pick up the sword!",style);
pick_message.fixedToCamera = true;
pick_message.visible = false;

然后:

function collisionHandler(dude,the_sword) {
pick_message.visible = true;
pickObject.onDown.add(function () {
    the_sword.kill();
    pick_message.visible = false;
}, this);
}

game.physics.isoArcade.overlap(dude, the_sword, collisionHandler, null, this);

类似的事情应该起作用.如果要执行其他操作,例如打开门,则可以使用:

Something like that should work. If you want to perform other action, like opening the door, you can use:

pick_message.setText("Click 'Q' to open the door!");

您不必每次都创建新文本,可以将其用于不同的目的.

You don't have to create new text evertime, you can use one for different purposes.

这篇关于移相器:精灵重叠时显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆