1 decade ago by blueicelt
I read through the forums and pulled out what I could to make my entities clickable and the code worked great. I am at point where I am making it so my entities can spin around, and when the images are spinning I am only able to click inside the original area of the entity and not where the entity image currently is.
Here is what one of my entities looks like
I tried implementing Box2D so that I could use the debugger plugin I found in the forums but that does not seem to do anything so I am not sure if I implemented it correctly.
if (ig.input.pressed('click') ) {
var found = false;
for( var i =0; i < this.entities.length; i++ ) {
if ( this.entities[i] instanceof EntityShip ) {
if (this.entities[i].inFocus()) {
this.setSelected(this.entities[i]);
found = true;
}
}
}
if ( found == false ) {
this.setSelected(null);
}
}
inFocus: function() {
return (
(this.pos.x <= (ig.input.mouse.x + ig.game.screen.x)) &&
((ig.input.mouse.x + ig.game.screen.x) <= this.pos.x + this.size.x) &&
(this.pos.y <= (ig.input.mouse.y + ig.game.screen.y)) &&
((ig.input.mouse.y + ig.game.screen.y) <= this.pos.y + this.size.y)
);
},
Here is what one of my entities looks like
EntityCarrier = EntityShip.extend({
size: {x:154, y:32},
offset: {x:0, y:0},
name: 'Aircraft Carrier',
animSheet: new ig.AnimationSheet( 'media/ships/ship-aircraftcarrier.png', 154, 32 ),
bounciness: 0,
init: function( x, y, settings ) {
this.parent( x, y, settings );
},
update: function() {
this.currentAnim.angle += .01;
if ( this.currentAnim.angle > 360 ) {
this.currentAnim.angle = 0;
}
this.parent();
}
});
I tried implementing Box2D so that I could use the debugger plugin I found in the forums but that does not seem to do anything so I am not sure if I implemented it correctly.
EntityShip = ig.Entity.extend({
size: {x:25, y:122},
checkAgainst: ig.Entity.TYPE.NONE,
collides: ig.Entity.COLLIDES.NEVER, // Collision is already handled by Box2D!
bounciness: 0,
name: '',
selected: false,
angle: 0,
type: ig.Entity.TYPE.B,
init: function( x, y, settings ) {
this.parent( x, y, settings );
this.addAnim( 'idle', 1, [0] );
},
inFocus: function() {
return (
(this.pos.x <= (ig.input.mouse.x + ig.game.screen.x)) &&
((ig.input.mouse.x + ig.game.screen.x) <= this.pos.x + this.size.x) &&
(this.pos.y <= (ig.input.mouse.y + ig.game.screen.y)) &&
((ig.input.mouse.y + ig.game.screen.y) <= this.pos.y + this.size.y)
);
},
createBody: function() {
var bodyDef = new b2.BodyDef();
bodyDef.position.Set(
(this.pos.x + this.size.x / 2) * b2.SCALE,
(this.pos.y + this.size.y / 2) * b2.SCALE
);
this.body = ig.world.CreateBody(bodyDef);
// These two lines define the shape
// e.g. b2.PolygonDef, b2.CircleDef
var shapeDef = new b2.CircleDef();
shapeDef.radius = 8 * b2.SCALE;
shapeDef.density = 1;
this.body.CreateShape(shapeDef);
this.body.SetMassFromShapes();
}
});
