1 decade ago by littlefoot
Hi,
I've made quite a bit of progress (for me as a total beginner, anyway :D) since my last post here. I am now working on collisions with enemy entities and have some trouble getting the player entity to bounce back from the enemy entity upon collision. Here are the basics:
Right now: If the player entity collides with the enemy entity from the side, the player entity loses 1 life. If the player entity collides with the enemy entity while they're falling (as in after a jump, for example), the enemy entity dies and the player gains 1 point to the total score.
Both the player and enemy entities are set to PASSIVE.
Right now if the player collides with the enemy from the side their life drains so quickly that they are killed seemingly instantly as the two continue to collide. What I want to do is make the player be forced back about 30px when they first collide with the enemy. I tried playing around with bounce and setting the enemy collision to ACTIVE, and 'collideWith', but can't seem to get this working.
In addition, the player also needs to collide with other entities in the game (right now it can 'eat' another entity to gain points, which works) so I don't need all entities to push the player back like this, just this enemy entity (and in the future another enemy entity, but that isn't built in yet).
Here is my enemy entity file:
And here is my player entity file:
Can anyone point me in the right direction here? I've been trying to figure this out for two days so far. Thanks in advance for any help/advice!
I've made quite a bit of progress (for me as a total beginner, anyway :D) since my last post here. I am now working on collisions with enemy entities and have some trouble getting the player entity to bounce back from the enemy entity upon collision. Here are the basics:
Right now: If the player entity collides with the enemy entity from the side, the player entity loses 1 life. If the player entity collides with the enemy entity while they're falling (as in after a jump, for example), the enemy entity dies and the player gains 1 point to the total score.
Both the player and enemy entities are set to PASSIVE.
Right now if the player collides with the enemy from the side their life drains so quickly that they are killed seemingly instantly as the two continue to collide. What I want to do is make the player be forced back about 30px when they first collide with the enemy. I tried playing around with bounce and setting the enemy collision to ACTIVE, and 'collideWith', but can't seem to get this working.
In addition, the player also needs to collide with other entities in the game (right now it can 'eat' another entity to gain points, which works) so I don't need all entities to push the player back like this, just this enemy entity (and in the future another enemy entity, but that isn't built in yet).
Here is my enemy entity file:
ig.module( 'game.entities.bug' ) .requires( 'impact.entity' ) .defines(function(){ EntityBug = ig.Entity.extend({ size: {x: 39, y:24}, health:5, friction: {x: 150, y: 0}, speed: 100, flip: false, type: ig.Entity.TYPE.B, // Player enemy group checkAgainst: ig.Entity.TYPE.A, collides: ig.Entity.COLLIDES.PASSIVE, animSheet: new ig.AnimationSheet( 'media/bug.png', 39, 24 ), init: function( x, y, settings ) { this.parent( x, y, settings ); // Add the animations this.addAnim( 'idle', 1, [0,1] ); }, check: function( other ) { if( other.vel.y > 0 ) { this.kill(); ig.game.score += 1; console.log ( ig.game.playerhealth ); } else { other.receiveDamage( 1, this ); ig.game.playerhealth -= 1; console.log( ig.game.playerhealth ); } }, update: function() { // near an edge? return! if( !ig.game.collisionMap.getTile( this.pos.x + (this.flip ? +4 : this.size.x -4), this.pos.y + this.size.y+1 ) ) { this.flip = !this.flip; } var xdir = this.flip ? -1 : 1; this.vel.x = this.speed * xdir; this.parent(); }, }); });
And here is my player entity file:
ig.module( 'game.entities.red-player' ) .requires( 'impact.entity' ) .defines(function(){ EntityRedPlayer = ig.Entity.extend({ size: {x: 88, y:48}, offset: {x: 0, y: 0}, flip: false, maxVel: {x: 300, y: 280}, friction: {x: 300, y: 0}, accelGround: 700, accelAir: 400, jump: 900, crouch: 20*0.7, stand:48, crouched:false, health: 3, type: ig.Entity.TYPE.A, // Player friendly group checkAgainst: ig.Entity.TYPE.NONE, collides: ig.Entity.COLLIDES.PASSIVE, animSheet: new ig.AnimationSheet( 'media/redanim.png', 88, 48 ), init: function( x, y, settings ) { this.parent( x, y, settings ); // Add the animations this.addAnim( 'idle', 1, [0] ); this.addAnim( 'run', 0.07, [0,1,2,3,4] ); this.addAnim( 'jump', 1, [5] ); this.addAnim( 'fall', 0.4, [6] ); }, update: function() { // move left or right var accel = this.standing ? this.accelGround : this.accelAir; if( ig.input.state('left') ) { this.accel.x = -accel; this.flip = true; } else if( ig.input.state('right') ) { this.accel.x = accel; this.flip = false; } else { this.accel.x = 0; } // jump if( this.standing && ig.input.pressed('jump') ) { this.vel.y = -this.jump; this.currentAnim = this.anims.jump; } // set the current animation, based on the player's speed if( this.vel.y < 0 ) { this.currentAnim = this.anims.jump; } else if( this.vel.y > 0 ) { this.currentAnim = this.anims.fall; } else if( this.vel.x != 0 ) { this.currentAnim = this.anims.run; } else { this.currentAnim = this.anims.idle; } this.currentAnim.flip.x = this.flip; // move! this.parent(); } }); });
Can anyone point me in the right direction here? I've been trying to figure this out for two days so far. Thanks in advance for any help/advice!