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 Mike

Sorry if this is a pretty dumb question, I'm new with Impact and very new to javascript in general.

I'm making a baseball game.

I made the ball it's own entity, and I need to check on the Catcher entity what direction that ball is moving on the X-Axis (Left or Right) so the catcher can position himself accordingly.

This is what I have in the catcher's update function, obviously it doesn't work correctly.

update: function() {
var ball = ig.game.getEntitiesByType( EntityBall );
var accel = this.accelGround;



if ( ball.accel.y > 0 ){
if (ball.accel.x > 0){
this.accel.x = accel;
}
}

this.parent();
},

1 decade ago by Mike

Tried this as well, didn't work.

Comes up with Uncaught TypeError: Cannot read property 'x' of undefined

if ( ball.pos.x != this.pos.x ){
if (ball.pos.x > this.pos.x){
while (ball.pos.x > this.pos.x){
this.accel.x = accel;
}
}else{
while (ball.pos.x < this.pos.x){
this.accel.x = -accel;
}
}
}

1 decade ago by stahlmanDesign

Try adding [0] at the end:

var ball = ig.game.getEntitiesByType( EntityBall )[0];

It's because there could be more than one ball and you want to tell it which one specifically, meaning the one that is the first one in the array returned by ig.game.getEntitiesByType

1 decade ago by Mike

That worked, thanks!
Page 1 of 1
« first « previous next › last »