1 decade ago
by dmen
I'm making a game where you 'shoot' buttons at snowmen... and I need to be able to have the buttons stick to the snowmen. The snowmen entities move and such... so once a button hits I essentially want the button to become part of the snowman. I don't see any entity.attachEntity() method... is there something like this?
No there isn't. You could add grab functionality to your snowmen that is triggered when a button overlaps (check: function(other)) or collides (collideWith: function(other, axis) with them. Then (untested):
// snowman.js
children: [],
grab: function(other)
{
// Prevent further checks and collisions
other.type = ig.Entity.TYPE.NONE;
other.collides = ig.Entity.COLLIDES.NEVER;
// Other will no longer respond to game physics
other.update = (function(){}).bind(other);
// Maintain some kind of offset
other.parentOffset = {
x: other.pos.x - this.pos.x,
y: other.pos.y - this.pos.y
};
// Add to our grabbed buttons
this.children.push(other);
},
update: function()
{
this.parent();
for (var i=0; i<this.children.length; i++)
{
var c = this.children[i];
c.pos.x = this.pos.x + c.parentOffset.x;
c.pos.y = this.pos.y + c.parentOffset.y;
}
}
1 decade ago
by dmen
Perfect, I'll give that a go. Thanks again!
Page 1 of 1
« first
« previous
next ›
last »