1 decade ago
by paulh
So i have an entity that i want to, when touched by the player, move forwards at a defined angle where the angle and direction is the centerpoint of an entity im really struggling with the basics i think :-(
//how do you get the position of an entity type entityJello?
var myx = ig.game.entityJello.pos.x;
var myy = ig.game.entityJello.pos.y;
//how do you get an angle between to entities?
thisangle = angle to entityJello;
//how do you make the angle become the angle of animation
this.anims.idle.angle = thisangle;
//How do you make an entity move along the angle it is rotated?
this.vel.x = thisangle;
I don't have a lot of time right now to really go into it but maybe I can give you a few pointers.
Impact has some methods to determine angles and whatnot (
http://impactjs.com/documentation/class-reference/entity#angleto). So get the angle between the entities and probably convert that to radians (
http://impactjs.com/documentation/class-reference/ig-core#number-torad) and apply it to your animation angle.
As far as moving goes you'll have to do a bit of trig. Something like this...
// in entity update()
var xf = Math.cos(this.currentAnim.angle)
var yf = Math.sin(this.currentAnim.angle)
this.pos.x += xf * this.speed * ig.system.tick;
this.pos.y += yf * this.speed * ig.system.tick;
Actually, maybe that's overly complex. Moving in a straight line you can maybe just use standard Impact movement.
shrugs
1 decade ago
by paulh
Hi
thanks for the reply, ive read hundreds of posts and the documentation, cant seem to get my head around it though :(
The other thing i cant work out is how to count the total number of a specific entity type on load....
After loading a level, to get the total number of a specific entity, do this in your game's init():
//other load stuff
this.loadLevel( level );
var total = ig.game.getEntitiesByType( type ).length;
//do stuff with total
1 decade ago
by paulh
Many thanks quid monkey, any ideas with the others, especially the get angle and distance to entity and how you use that data?
thx again
What Graphikos said above. To move at an angle, the basic formula is:
pos.x = vel.x * cos(angle) * deltaTime;
pos.y = vel.y * sin(angle) * deltaTime;
You can grab the
.angleTo() and
.distanceTo() another Entity through
.check() or
collideWith(). For example:
collideWith: function( other, axis ){
var angle = this.angleTo( other );
var distance = this.distanceTo( other );
//do stuff with angle & distance
}
1 decade ago
by paulh
Thanks but im trying to get the distance without the collision, hence the :
var myx = ig.game.entityJello.pos.x;
var myy = ig.game.entityJello.pos.y;
Im guessing its something like that?
Yup. You'll either have to define ig.game.entityJello to reference EntityJello, or you can do this:
var jello = ig.game.getEntitiesByType( EntityJello );
var mx = jello[0].pos.x;
var my = jello[0].pos.y;
1 decade ago
by paulh
Thanks again quidmonkey
The other thing is where i can store or define these variables in such a way as they are available to all entitites.
For example, I have the count of EntityYellow from the main.js when i load a level and i have a counter n Entity Yellow which counts up when an entity is killed. How cna i only declare the entittYellow coutn s it is available to all my entities after load?
I guess im getting toward a entityStoreGameVariables which i can send data to and then draw from it form any entity.class?
thx again for your help!
You can create a global reference in an Entity's init(). For example:
EntityJello = ig.Entity.extend({
init: function( x, y, settings ){
this.parent( x, y, settings );
this.addAnim( 'idle', 1, [0] );
//other init code
ig.game.jello = this;
}
//other code
});
You can then access EntityJello globally through ig.game.jello.
1 decade ago
by paulh
Many thanks again, i tried this, but it didnt seem to work when sending ig.game.globalvar to console i get "undefined"
##
ig.module(
'game.stats'
)
.requires(
'impact.entity'
)
.defines(function(){
EntityStats = ig.Entity.extend({
init: function( ) {
ig.game.globalvar = 14;
}
});
});
Did you spawn an EntityStats?
1 decade ago
by paulh
This community is awesome, so many thanks!
P.
1 decade ago
by paulh
so i got the entity working, reacting and rotating when struck by the player however the animation rotation is not the same as the current animation, how would i make:
1. The angle of rotation on an animation be the same as the direction the entity moves.
2. The angle of rotation be adjusted after a collision.
ig.module('game.entities.yellow')
.requires('impact.entity')
.defines(function(){
EntityYellow = ig.Entity.extend({
type: ig.Entity.TYPE.B,
checkAgainst: ig.Entity.TYPE.A,
collides: ig.Entity.COLLIDES.PASSIVE,
size: {x:12, y:14},
maxVel: {x: 100, y: 100},
bounciness: 3, //A factor, indicating with which force the entity will bounce back after a collision. With a .bounciness set to 1, the entity will bounce back with the same speed it has hit the other entity/collision map. Default 0
offset: {x: 10, y: 10},//offset the collision box
speed:220,
animSheet: new ig.AnimationSheet( 'media/320x480/hats.png', 32, 32 ),
init: function( x, y, settings ) {
this.timer = new ig.Timer(1);//http://www.pointofimpactjs.com/tutorials/view/7/using-impact-timers
this.parent( x, y, settings );
this.addAnim( 'idle', 1, [40] );
},
check: function( player ) { //when this entity collides with player entity
var angle = this.angleTo( player );
var angled = (angle *180/Math.PI) +180; //radian to angle and invert
var angling = angled *Math.PI/180; //back to radians
this.vel.x = Math.cos(angling) * this.speed;
this.vel.y = Math.sin(angling) * this.speed;
this.anims.idle.angle = angled;
this.timer.reset();
},
update: function(){
if (this.timer.delta() > 0) { //after timer length stop moving
this.vel.x = 0;
this.vel.y = 0;
//console.log ( angly);
}
this.parent();
}
});
});
Page 1 of 1
« first
« previous
next ›
last »