Hi,
I want to make an entity stop moving forward when it walks into a trigger. But I also want to show which version of the entity it was in the console.
I have added a key (name) and a value to each entity. But when the trigger is activated instead of the name I am getting "undefined". How do I get the value from the entity?
check: function(other) {
if (other instanceof EntityPlayer) {
var player = ig.game.getEntitiesByType( EntityPlayer )[0];
player.speed = 0;
console.log(this.name);
1 decade ago
by lazer
First, you don't need to set var player. You know "other" is the player entity because you checked for that with the if statement, so you can just keep referring to "other".
Second, is the name meant to be assigned to the trigger or to the player? I'm assuming trigger. Where are you setting it exactly?
I added name as a key to the player entity and then gave the player a value in Weltmeister.
What I'd like is when the player goes into the trigger it sends the "name" of the player to the console along with setting the speed to 0. The speed part works, and I'll move the code to other instead of defining player, but console.log(this.name); doesn't give me the name I set in Weltmeister.
Can I send the name I set in Weltmeister to the console?
Thanks
1 decade ago
by Joncom
Make sure that your
init
in player looks something like this:
init: function(x, y, settings) {
// Here is where Weltmeister values are merged into player.
this.parent(x, y, settings);
/* All of your other init stuff. */
}
Lazer and Joncom, thank you for your help! You rock.
As it turns out I just needed to assign the value to a variable first. Go figure.
New code for those who might be interested.
check: function(other) {
if (other instanceof EntityPlayer) {
player.speed = 0;
var playername = other.name;
console.log(playername);
}
1 decade ago
by Joncom
Quote from lucastimmons
I just needed to assign the value to a variable first. Go figure.
That's not the reason it works now. It works now because originally you were calling:
console.log(this.name);
And now you are basically calling:
console.log(other.name);
1 decade ago
by lazer
Yeah, what Joncom said. Also, I'm confused about how you're using player.speed. I don't see player being set as a local variable before you use it. In theory you should just be using other.speed and player.speed should not work (as far as I can see).
Page 1 of 1
« first
« previous
next ›
last »