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 janix2011

So other than a few issues I have ran into along the way, which got sorted out thanks to the forums and a little trial and error, my first game is coming along fairly well. Yesterday I ran into a problem and have spent many hours trying different techniques to fix it.

My problem is the doors I have in my levels. I have them set up to be fixed entities. When the key is collected and a hotkey is pressed by the door it will change to a different sprite and will be passive allowing the player to pass through. The problem I have ran into is that my entities do not return after bumping into them like they did before when I had the doors as foreground tiles with collision. The enemies and my player's projectile weapons stick to the door once they make contact, and the projectiles do not .kill themselves.

I know the problem has to do with entity types, check against, etc; but I am not sure how to go about setting this up. I can include some code if needed, but general tips on how to set up unlockable doors would be very much appreciated. Thank you.

1 decade ago by stahlmanDesign

Be more specific about how you are implementing doors, and include some code.

1 decade ago by janix2011

ig.module(
    'game.entities.door'
    )
.requires(
    'impact.entity'
    )
.defines(function(){
    EntityDoor = ig.Entity.extend({
        
      type: ig.Entity.TYPE.NONE,
      checkAgainst: ig.Entity.TYPE.NONE,
      collides: ig.Entity.COLLIDES.FIXED,
        
        size:{x:16,y:32},    
        animSheet: new ig.AnimationSheet('media/door.png',16,32),
    
        init: function( x, y, settings ) {
            this.parent(x,y,settings );
            this.addAnim( 'closed', 1, [0] );
            this.addAnim('open', 1, [1] );
        },
    
        update: function() {
			this.currentAnim = this.anims.closed;
            this.parent();
        },
		
        check: function(other) {
            if(other==ig.game.getEntitiesByType(EntityPlayer)[0] && other.key == true && ig.input.pressed('sword')) {
				this.currentAnim = this.anims.open;
                collides: ig.Entity.COLLIDES.PASSIVE;
            }
        }
    });
});

My player entity is type:A, collides: passive, checkagainst: none.
My projectile weapons are type:none, collides: passive, checkagainst: B.
My enemies are type: B, collides: passive, checkagainst: A.

The door entity is placed in Weltmeister. There is a boss at the end of the level that drops a key to open the door. When the player collects the key, his key: false changes to key:true. My problem currently is my projectiles and enemies are both hitting the door and sticking to it. The projectiles are supposed to .kill on impact with collision, and the enemies need to collide with the door then return back the opposite direction.

1 decade ago by stahlmanDesign

OK, first of all, for your other entities to turn back when they hit the door, put this in them (they will bounce back from all COLLIDES.FIXED objects) :
check: function( other ) {
if (other.collides == ig.Entity.COLLIDES.FIXED){
     this.flip = !this.flip;this.currentAnim.flip.x = this.flip;
   }
}

In your door, fix your check code like this:

check: function(other) {
//I changed other.key to other.hasKey because it's more clear      
if(other==ig.game.getEntitiesByType(EntityPlayer)[0] && other.hasKey ) {
   this.currentAnim = this.anims.open;
 // this was written incorrectly
     this.collides = ig.Entity.COLLIDES.PASSIVE;
   }else{
// if you want door to close behind you
      this.currentAnim = this.anims.closed;
      this.collides = ig.Entity.COLLIDES.FIXED; 
   }
}

I had to change the door type from NONE:
type: ig.Entity.TYPE.B, // Be careful cause you could "kill" the door
      checkAgainst: ig.Entity.TYPE.A, // your player is TYPE.A
      collides: ig.Entity.COLLIDES.FIXED,

And the key too:
type: ig.Entity.TYPE.B,
      checkAgainst: ig.Entity.TYPE.A,

and then in the check of the key so it gives itself to player and disappears.

if(other==ig.game.getEntitiesByType(EntityPlayer)[0] ) {
                other.hasKey=true;
		this.kill();
            }


As for projectiles disappearing, they need to call this.kill() if their other == door in check code. Make sure their type checks against both A and B if your door is B

1 decade ago by janix2011

Thank you stahlman for your help. I will incorporate those changes.

1 decade ago by janix2011

I have incorporated the changes you suggested, as well as tweaking some of my code that conflicted with the changes. Nearly everything is working perfectly now, but I am still having a problem with my enemies not flipping and coming back after hitting the door. Here is what I have for one of my enemies:

ig.module(
   'game.entities.eye'
)
.requires (
   'impact.entity'
)
.defines(function() {

EntityEye = ig.Entity.extend({
      
   size: {x:16, y:16},
   maxVel: {x: 70, y: 0},
   health: 20,
   flip: true,
   shootTimer: null,
   friction: {x:300, y:300},
   collides: ig.Entity.COLLIDES.PASSIVE,
   type: ig.Entity.TYPE.B,
   checkAgainst: ig.Entity.TYPE.A,
   check: function( other ) {
      if( other instanceof EntityPlayer ) {
         other.currentAnim = other.anims.flinch;
         other.receiveDamage(10, this);
         if( other.pos.x > this.pos.x ) {
            // Flinch right
            other.vel.y = -150;
            other.vel.x = 150;
                
         }
         else {
            // Flinch left
            other.vel.y = -150;
            other.vel.x = -150;
         }
      }
      if (other.collides == ig.Entity.COLLIDES.FIXED){
         this.flip = !this.flip;
         this.currentAnim.flip.x = this.flip;
      }
   },
   animSheet: new ig.AnimationSheet('media/eye.png',16,16),
    
   init: function(x,y,settings){
         this.parent(x,y,settings );
         this.addAnim('run',.8,[0,1]);
	 this.shootTimer = new ig.Timer(.5);
   },
    
   update: function() {
         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.accel.x = 70 * xdir;
         
         var player = ig.game.getEntitiesByType(EntityPlayer)[0];
         if(player && this.distanceTo(player) < 200 && this.shootTimer.delta() > 0) {
	    ig.game.spawnEntity( EntityBeam, this.pos.x + 10, this.pos.y, {flip:this.flip});
	    this.shootTimer.set(2);
         }
         this.currentAnim.flip.x = this.flip; 
         this.parent();
    },
    
    handleMovementTrace: function( res ) {
         this.parent( res );
         if( res.collision.x ) {
            this.flip = !this.flip;
         }
    },
});

   EntityBeam = ig.Entity.extend({
      size: {x:8, y:4},
      animSheet: new ig.AnimationSheet('media/beam.png',8 ,4),
      maxVel: {x:120, y:0 },
      type: ig.Entity.TYPE.NONE,
      checkAgainst: ig.Entity.TYPE.BOTH,
      collides: ig.Entity.COLLIDES.PASSIVE,
      check: function(other) {
      if (other instanceof EntityPlayer) {
         other.receiveDamage(10, this );
         if( other.pos.x > this.pos.x ) {
            // Flinch right
            other.vel.y = -150;
            other.vel.x = 150;
                
         }
         else {
            // Flinch left
            other.vel.y = -150;
            other.vel.x = -150;
         }
      }
         this.kill();
      },
      
      init: function( x, y, settings ) {
         this.parent( x + (settings.flip ? -20 : 5) , y+8, settings );
         this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
         this.addAnim( 'idle', 0.2, [0] );
      },
   
      handleMovementTrace: function( res ) {
         this.parent(res);
         if(res.collision.x || res.collision.y) {
         this.kill();
         }
      }  
   });

});

1 decade ago by stahlmanDesign

Easy fix, you just need your eye to check against BOTH instead of A. This is because your door is of type B, but you also want the eye to affect the player of type A.

Be sure to remove your trailing comma after the handleMovementTrace in eye:


handleMovementTrace: function( res ) {
         this.parent( res );
         if( res.collision.x ) {
            this.flip = !this.flip;
         }
    },
});

It works fine in some browsers, but won't work in others (i.e., IE)

1 decade ago by janix2011

Very nice. Completely forgot about adding B because of the doors. Thank you very much for the help stahlman. =)
Page 1 of 1
« first « previous next › last »