This is my code for an enemy its within the player.js file but a different entity
the entity spawns fine
I run into this problem often and its causing the majority of my issues when I try to work around it.
EntityMob = ig.Entity.extend({
size: {x: HEX_SIZE, y: HEX_SIZE},
speed: 4,
name: "Mob",
gamename: "",
pos: {x:220, y:220},
offset: {x:33, y:33},
animSheet: new ig.AnimationSheet( 'media/mob.png', 60, 60 ),
... animations for every monster in game ...
...
update: function() {
var player = ig.game.getEntitiesByType( EntityPlayer );
if (player){
if (this.pos.x>player.pos.x-165&&
this.pos.x<player.pos.x+165&&
this.pos.y>player.pos.y-165&&
this.pos.y<player.pos.y+165){
player.messagebox = "I see you";
}else{
player.messagebox = "I do not see you";
}
}
}
1 decade ago
by Donzo
I understand the ig.game.getEntitiesByType can be taxing on d resources.
Here's what I use:
EntityPlayer = ig.Entity.extend({
// The players (collision) size is a bit smaller than the animation
// frames, so we have to move the collision box a bit (offset)
size: {x: 14, y:32},
offset: {x: 0, y: 0},
name:"ninja",
^
First, I name my entity.
Then, I reference him as such:
check: function( other ) {
other.receiveDamage( 3, this );
//Here's the reference
var player = ig.game.getEntityByName('ninja')
if (other != player) {
other.stunnedTimer.set(3);
other.stunned = true;
}
That's helpful to know.
Because of the nature of the game my entities can share a tile on the board. When this happens I want them to be offset a bit so they are easier to see. I don't see a way around calling by type when I need an array of entities like this.
I could probably avoid an error by combining them into 1 array but it still probably won't give the effect I need. Would slowing game speed down help? If so how do I do that?
var player = ig.game.getEntityByName( 'player' );
var mob = ig.game.getEntitiesByType( EntityMob );
//tracks number of entities in each square
var mobsinhex =new Array(49);
for (i in mobsinhex){
mobsinhex[i]=0;//initially =0
}
mobsinhex[24] = 1;//Accounts for the player's entity in the middle square
for(var i in mob){
if ((mob[i])&&player){
//this line numbers all squares on the board from 0 to 48
var hexnumber =((mob[i].pos.y - player.pos.y+165)*7/55)+((mob[i].pos.x-player.pos.x+165)/55)
if (hexnumber<49&&hexnumber>=0){//only runs the function if player can see them
mobsinhex[hexnumber]++;//incriment if an entity is on the square
var mobposition = mobsinhex[hexnumber]%4;//allows four positions called by switch (mobposition)
player.messagebox = mobposition;
player[0].messagebox = mobposition
switch (mobposition) {
case 1:
mob[i].offset.x = 33;
mob[i].offset.y = 33;
break;
case 2:
mob[i].offset.x = 3;
mob[i].offset.y = 33;
break;
case 3:
mob[i].offset.x = 33;
mob[i].offset.y = 3;
break;
case 0:
mob[i].offset.x = 3;
mob[i].offset.y = 3;
break;
}
}
}
}
or showing a list of entities near the player
So how can I get an array of all the players without having this issue?
1 decade ago
by dominic
.getEntitiesByType()
returns an array of entities, not a single entity. Your code from your first post should've been something like this (also note the existence of
.distanceTo :))
var players = ig.game.getEntitiesByType( EntityPlayer );
for( var i = 0; i < players.length; i++ ) {
if( this.distanceTo(players[i]) < 165 ) {
players[i].messagebox = "I see you";
}
else {
players[i].messagebox = "I don't see you";
}
}
As Donzo noted,
getEntitiesByType()
can be a bit taxing on performance, depending on the number of entities in your game. If you need this function often (multiple times per frame), you should cache your results somewhere. For instance, only call
getEntitiesByType()
when a player spawns and store the results in your game:
// After a player spawns or the game starts... or whenever you are
// certain that the players won't change for a while:
ig.game.players = ig.game.getEntitiesByType( EntityPlayer );
// Then reference this array whenever you need it:
for( var i = 0; i < ig.game.players.length; i++ ) {
...
}
@ dominic ah sweet didn't know about the distance to function that will be useful. Thanks for the global tip too I'll give that a try.
Page 1 of 1
« first
« previous
next ›
last »