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

9 years ago by Arturo

Hi!

I have a small problem that Im not sure how to solve.
I search on the forum but the answers that I found doesn't seems to work :(

I have a player, and then inside the player I create an entity.
What I want to do, is to put this entity in front of the player (for example, the entity is a square and inside is the player.

I would like to make the player be follow by the square, so the player is always in the middle of the square

As I understood, on the Entity update function I wrote this:

update: function(){
                this.pos.x = ig.game.player.pos.x;
                this.pos.y = ig.game.player.pos.y;
                this.currentAnim = this.anims[this.statusAct];

                this.parent();
            }

...where I set

ig.game.player = this;

on the player class on init.

It seems that almost work... becuase the square follows the player, but it looks like it follow it with delay. like around 10px, the square stay behind (or down if jump) and when the player stop, then the square catch up and is in the middle....

any idea of what im doing wrong?

Thanks!!

:)

9 years ago by Joncom

All entities are stored in the array ig.game.entities. Every frame, ig.game.update is called, which loops through the entities array, and calls the update function of each entity. If your square entity comes before the player entity, then it will update its position to the position that player was at last frame. And then the player entity will get updated, and your square entity will be off by a little bit.

Sounds like you want your square to be updated after the player. This can be done by giving it a higher zIndex. However, zIndex also influences the draw order. So if you need the player to draw above the square for some reason, then changing this order will break that. If that's the case, then you may need to update the position of the square somewhere else, such as in main.js, after calling this.parent() in the update function.

9 years ago by Arturo

@Joncom

Thanks!
I miss understood before the position on the ig.game.entities array.

In my case the square have to be behind, so I add the update on main instead of the square and works prefect :)

Q: the positon on this ig.game.entitities is done by the order that you put it on the main? and subentitites will be always called before the father entity?

Im gonna make some test to understand better the order of updates to avoid future problems ;)

Thanks again!!





Edit: taking a closer look, there is something I dont understand about the order of the functions and entities.
Im working as well with a trigger, and using the function check for it.
I want the square to change shape to a circle when the player enter in an expecific zone... and it seems to works fine, no matter if I put the chaging animation (from square to circle) on the update from the square or the update from the main

Then, when I leave the specific zone, I want to go back to the default shape (square)... so what I do, is on the update from the player, I start always by setting the shape to square.... because I thought the square will be call later (I find out here that is the other way around).... so it change to circle only when triggers... s it check call after update but before drawing? or update is the one calling check?
Because if check is before or on the update.... why is working like this? shouldnt be always square?

Sorry, how you can see im really confure by the orders of the functions :P

9 years ago by Joncom

Q: the positon on this ig.game.entitities is done by the order that you put it on the main?
By default, entities are placed in the ig.game.entities array in the order in which they are spawned. You need to call sortEntitiesDeferred every time you wish to sort them. Or you can set autoSort to true and then it will happen automatically every frame.

and subentitites will be always called before the father entity?
The first entity in the array gets its update function called first. The second entity in the array gets its update function called next. The last entity in the array gets its update function called last.

check call after update but before drawing? or update is the one calling check?
Yes to the former. Here's a snippet from the update function in "game.js":

update: function(){
    ...
    
    // update entities
    this.updateEntities();
    this.checkEntities();

    ...
}

As we can see here, updates are executed before checks.

So if you want to change the shape of an entity from a square to a circle when touching some other entity, you can always set the shape to square during the update function and then let the check function set it back to circle if the conditions are correct.

9 years ago by Arturo

Thanks!
I see it a lot more clear now, and I got to make what I wanted to do :)

I made then updating to square by default always on update, and on check make it circle.

Now just for curiosity.... is that the most efficent way to do it on those cases? or the triggers can also set when an entity is getting out of the trigger? so I say: "you are a square once you are out" (instead of making by default always square and changing to circle only if is inside.

9 years ago by Joncom

Quote from Arturo
Now just for curiosity.... is that the most efficent way to do it on those cases? or the triggers can also set when an entity is getting out of the trigger? so I say: "you are a square once you are out" (instead of making by default always square and changing to circle only if is inside.
I think that is the simplest way to do it for now...

There might be a "cleaner" way to do it, but it would be a lot more work. I think it would be cleaner if there was an "onContactStart" event between entities, and an "onContactEnd" event. Then you could simply handle the shape change during those events. But this is not built into vanilla ImpactJS.

You could use Box2D, and it already implements events this way, but that might be overkill...

9 years ago by Arturo

I see. Well, for now im happy with the results in that way. If I see in the future this became a problem I know where to start looking to change the way I check the contacts.

Thanks for all the info and help!

:)

9 years ago by Joncom

No problem!
Page 1 of 1
« first « previous next › last »