1 decade ago
by Joncom
I want the entity to be spawned beneath my player.
I've tried setting the zIndex of the spawned entity to 0, -999... it doesn't seem to make a difference. Any tips?
Edit Just to clarify... The reason why the newly spawned entity is above my player "for a moment" is because ig.game.autosort kicks in immediately after.
1 decade ago
by Joncom
Hello Joncom. I'm you from the future... You may find this helpful! Put it in your main.js somewhere.
/*
* Same thing as spawnEntity() but adds new entity to top of array, not the end.
*
* @param type string/entityClass Name of entity sub class.
* @param x integer Pixel position on x-axis.
* @param y integer Pixel position on y-axis.
* @param settings object Initial variables.
* @return entityClass on success, undefined otherwise.
*/
spawnEntityBelow: function( type, x, y, settings ) {
var entityClass = typeof(type) === 'string'
? ig.global[type]
: type;
if( !entityClass ) {
throw("Can't spawn entity of type " + type);
}
var ent = new (entityClass)( x, y, settings || {} );
this.entities.splice( 0, 0, ent );
if( ent.name ) {
this.namedEntities[ent.name] = ent;
}
return ent;
}
And if anybody cares, the only real different between
spawnEntity()
and
spawnEntityBelow()
is I changed this line:
this.entities.push( ent );
...to this:
this.entities.splice( 0, 0, ent );
hey joncom you don't need to put it in your main. you can put it where ever you want. all you need to do to spawn an entity below you is something similar to the following:
respawn: function(){
if (this.timer.delta() > 0){
var x = (Math.floor(Math.random() * 2) == 1) ? 95 : 160;
ig.game.spawnEntity(EntityCarOncoming, x, this.player.pos.y.round() + 320);
this.timer.reset();
this.killed = false;
}
},
i take my players position and then add 320 to it since my camera is focused on my entity and it is at centered on the screen. my # ig.game.system.height # is 640 so the 320 is half of that. I put this respawn code in each entity i wanted to respawn. see below for an example class.
ig.module(
'game.entities.car-ongoing'
)
.requires(
'game.entities.car'
)
.defines(function(){
EntityCarOngoing = EntityCar.extend({
animSheet: new ig.AnimationSheet('media/ongoing.png', 60, 60),
font: new ig.Font( 'media/04b03.font.png' ), // load debug font
init: function(x, y, settings){
this.parent(x, y, settings);
this.accel.y = -this.speed;
this.vel.y = -this.speed;
this.timer = new ig.Timer(Math.floor((Math.random() * 2))); // set our timer to a random set time
},
update: function(){
this.parent();
if (this.player){
if (this.pos.y.round() > this.player.pos.y.round() + 320){
this.kill();
}else{
if (this.pos.y.round() < this.player.pos.y.round() - 1280){
this.kill();
}
}
if (this.killed){
this.respawn();
}
}
},
respawn: function(){
if (this.timer.delta() > 0){
var x = (Math.floor(Math.random() * 2) == 1) ? 296 : 227;
if (this.player.vel.y == 0){
ig.game.spawnEntity(EntityCarOngoing, x, this.player.pos.y.round() + 320);
}else{
ig.game.spawnEntity(EntityCarOngoing, x, this.player.pos.y.round() - 640);
}
this.timer.reset();
this.killed = false;
}
},
});
})
note this class is extended from a base class car.
hope that helps.
Page 1 of 1
« first
« previous
next ›
last »