1 decade ago
by Xavunis
Hello.
I'm trying to make an ice entity but when I create X number of them stop working.
Can someone help me please?
This is the entity code:
ig.module(
'game.entities.hielo'
)
.requires(
'impact.entity'
)
.defines(function(){
EntityHielo = ig.Entity.extend({
size: {x: 16, y: 16},
checkAgainst: ig.Entity.TYPE.A,
_wmScalable: true,
_wmDrawBox: true,
_wmBoxColor: 'rgba(0, 132, 255, 0.7)',
update: function ()
{
if(ig.game.getEntitiesByType( 'EntityPlayer' )[0])
{
if(!this.check(ig.game.getEntitiesByType( 'EntityPlayer' )[0]))
{
ig.game.getEntitiesByType( 'EntityPlayer' )[0].friction = {x: 600, y: 0};
ig.game.getEntitiesByType( 'EntityPlayer' )[0].accelGround = 400;
}
}
},
check: function (other)
{
other.friction = {x: 20, y: 0};
other.accelGround = 100;
}
});
});
1 decade ago
by mimik
Can it bee this? your getting the first entity of the type Player.
ig.game.getEntitiesByType( 'EntityPlayer' )[0]
[0] == first
1 decade ago
by Xavunis
I only have one player entity, so It must be the first I think.
The problem is when I create more of X ice entities... They stop working for some reason.
I would look at moving the check onto the player entitiy, so if its in contact with an ice entity change the velocity friction etc.
Also if you are using a background map you could just detect the ice map tiles , again within the player entity and perform the same operation.
First, cache your player reference:
update: function ()
{
var player = g.game.getEntitiesByType( 'EntityPlayer' )[0];
if(player)
{
if(!player)
{
player.friction = {x: 600, y: 0};
player.accelGround = 400;
}
}
},
Every
.getEntitiesByType()
call searches through the entire ig.game.entities collection, which is a performance hit.
Second, use
.touches() vs.
.check()
:
if( this.touches( player ) ){
//on ice, so set ice friction
}
else{
//not on ice, so set standard friction
}
1 decade ago
by Xavunis
I tried but now only works the last ice entity I make.
This is the new code:
ig.module(
'game.entities.hielo'
)
.requires(
'impact.entity'
)
.defines(function(){
EntityHielo = ig.Entity.extend({
size: {x: 16, y: 16},
checkAgainst: ig.Entity.TYPE.A,
_wmScalable: true,
_wmDrawBox: true,
_wmBoxColor: 'rgba(0, 132, 255, 0.7)',
update: function ()
{
var player = ig.game.getEntitiesByType( 'EntityPlayer' )[0];
if(player)
{
if(this.touches(player)){
//on ice, so set ice friction
player.friction = {x: 20, y: 0};
player.accelGround = 100;
}
else{
//not on ice, so set standard friction
player.friction = {x: 600, y: 0};
player.accelGround = 400;
}
}
}
});
});
1 decade ago
by drhayes
Let's say you have a bunch of these on a level:
Ice1, Ice2, Ice3, Ice4...
And the player is standing on Ice1.
Ice1 checks, sees that the player is in contact with itself, and sets the player's friction and accelGround properties to the ice values.
Then Ice2 checks, sees that the player is not in contact with itself, and sets the player's friction and accelGround to standard friction.
Basically, setting these values in the ice entity this way guarantees that the player will never have ice friction.
You should try moving this code to the player entity. That way, the player entity can say, "Am I touching ice right now?" and set the values itself. Otherwise, you have to coordinate among all these entities and that's kind of hard. ( =
1 decade ago
by Xavunis
Wow, thanks drhayes! I did not see that ^^
Now it works all ok. I cleaned the ice entity and added the checking code to the entity player:
var hielos = ig.game.getEntitiesByType( 'EntityHielo' );
if(hielos)
{
var i = 0;
while(i<hielos.length)
{
if(this.touches(hielos[i])){
//on ice, so set ice friction
this.friction = {x: 20, y: 0};
this.accelGround = 100;
i = hielos.length;
}
else
{
//not on ice, so set standard friction
this.friction = {x: 600, y: 0};
this.accelGround = 400;
i++;
}
}
}
If someone can make a better code to do this... I accept suggestions ^^
Thanks all for the help :)
1 decade ago
by PaulB
Is there any chance that you could post the cleaned up ice entity? Was it just simplified, without the update function?
1 decade ago
by PaulB
## var hielos = ig.game.getEntitiesByType( 'EntityHielo' );
## if(hielos)
This only works if you have ice in your map - if you don't, instead of returning null, it throws the following error:
#Uncaught TypeError: Expecting a function in instanceof check, but got #<Class>
Is there a way around this? There must be, but I figure it out! :(
if (typeof EntityHielo !== 'undefined') {
var hielo = ig.game.getEntitiesByType(EntityHielo);
// code
}
1 decade ago
by PaulB
Thanks quidmonkey, you're a star =D
Page 1 of 1
« first
« previous
next ›
last »