Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by geradr

Hey everyone !
I'm feeling stumped on this one
Pretty sure the code should go like this

this.pos.x = entity2.pos.x + 20;
this.pos.y = entity2.pos.y + 20;

But I can't figure out where it should go
I'm planning on making a mirror image of the player
then move on to a delayed movement mirror player
Hopefully
Thanks in advance

1 decade ago by BFresh

I do something similar with entities that are only meant as shadows and effects (they have no collision while the entity they follow around does).

Throw that in your update() function for your following entity and you should be fine. You will need to get the other entity as an object to access it's position properties, so a getEntityByName() on entity2 should work fine if you give it a name.

You will also have to deal with flipping to get the x/y right if your followed entity flips its animation back and forth.

1 decade ago by geradr

Ah thanks, that was a prompt reply and exactly what I was looking for
( I was actually dumping the code under handleMovementTrace() )

But now I have another problem, D :
It only seems to work under the ig.Game.extend({
and not under ig.Entity.extend({

google chrome gives out
Uncaught TypeError: Object [object Object] has no method 'getEntitiesByType' or
Uncaught TypeError: Object [object Object] has no method 'getEntityByName'
even if the code is almost the same

So in short it works, but only if it is in the main.js file
Which is not so bad but it annoys me when things are not organised
Here's what I have so far

update: function() {	
		// screen follows the player
		var player = this.getEntitiesByType( EntityPlayer )[0];
		
		if( player ) {
			this.screen.x = player.pos.x - ig.system.width/2+10;
			this.screen.y = player.pos.y - ig.system.height/2;
		}
		
		// continued from EntityThing
		var thing = this.getEntitiesByType( EntityThing )[0];
		
		if( player, thing ) {
			thing.pos.x = player.pos.x+2;
			thing.pos.y = player.pos.y+8;
			
			if( player.flip == true ) {
				thing.currentAnim.flip.x = false;
			}
			else {
				thing.currentAnim.flip.x = true;
			}
		}
		
		// update all entities and BackgroundMaps
		this.parent();
	},

Any suggestions are welcome

1 decade ago by BFresh

I think now you are just facing a scope issue, ig.game is what contains the get Entity by name or type methods, so from within an entity, you would need to do ig.game.getEntityByName and it should work.

this.Whatever works for only properties and methods for the object you are working with, in this case a particular entity.

From within an entity, ig.game.Whatever would get you access to the methods you need when you need them.

DISCLAIMER! - A professional programmer may be able to explain scope of objects better ;)

1 decade ago by geradr

AMAZING !
I'm not sure how you knew but it works now !
I'll pop the code here in case anyone needs it

update: function() {		
	var player = ig.game.getEntitiesByType( EntityPlayer )[0];
	if( player ) {
		this.pos.x = player.pos.x+2;
		this.pos.y = player.pos.y+10;
	}
		
	this.parent();
}

it's so organised now I can just cry
for some reason ig.game.getEntityByName( EntityPlayer );
doesn't work but that's insignificant

and it's ok, even if a pro explains it I probably will still not get it
pretty much 90% of what I type is like greek to me
I get around mainly by trial and error, lots and lots of it

1 decade ago by BFresh

for getEntitybyName() to work, you need to give the entity you want to get a name. Give it a name: 'Anything', up at the top of the entity (this can be overwritten from Weltmeister) and then if you do a ig.game.getEntitybyName('Anything'), you should get the entity.

1 decade ago by Ash_Blue

Could ig.game.getEntitiesByType( EntityPlayer )[0]; be placed in the documentation? Seems like a really useful tip since a lot of people will want to alter their player data.
Page 1 of 1
« first « previous next › last »