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 sleenee

I have the most weird bug. I have been looking at this with utmost amazement as I cannot find what could be wrong.

I have an NPC that randomly changes course (top-down view so 4 directions possible) and it does so : it goes to the left, the top or down the screen. However it refuses to go to the right, I really have no clue why. I changed the random number that corresponds to moving to the right, I messed around with a few things and in all cases the only thing he doesn’t want to do is walk to the right. I made two different weltmeister maps and tested the movement in both but it makes no difference (to make sure it’s not some weird map bug).

The code for the character movement AI:

	 charactermovement: function(){
		  
	        var randomdirection=  Math.floor(Math.random()*5)+1;
	                
			if( randomdirection == 1  ) {
				this.vel.x = -64;
				this.vel.y = 0;
				this.currentAnim = this.anims.walkleft;
				this.formerpressed = 'left';
				
			}
			else if( randomdirection == 3  ) {
				this.vel.x = 64;
				this.vel.y = 0;
				this.currentAnim = this.anims.walkright;
				this.formerpressed = 'right';
			}
			else if( randomdirection == 2  ) {
				this.vel.y = -64;
				this.vel.x = 0;
				this.currentAnim = this.anims.walkup;
			
				this.formerpressed = 'up';
			}	
			else if( randomdirection == 4 ) {
				this.vel.y = 64;
				this.vel.x = 0;
				this.currentAnim = this.anims.walkdown;
				
				this.formerpressed = 'down';			
			}
			else 
			{ 
				this.vel.y = 0;
				this.vel.x = 0;
				if(this.formerpressed == 'left')
				{			
					this.currentAnim = this.anims.lookleft;
				}
				else if (this.formerpressed == 'right')
				{			
					this.currentAnim = this.anims.lookright;
				}
				else if (this.formerpressed == 'up')
				{			
					this.currentAnim = this.anims.lookup;
				}
				else if (this.formerpressed == 'down')
				{
					this.currentAnim = this.anims.lookdown;	
				}
			}
	    },

It's usefull code, so everyone who needs the same functionality, be free to use it. I just don't get the walking to the right fixed.

if someone knows what might be wrong, be my guest.

Sleenee

1 decade ago by fugufish

code looks good, but maybe an error somewhere else? eg. error in defining the right movement in your init()

could you supply more code? from the point of defining the movements onwards should be enough.

the random number function is not faulty. It's basic Javascript, should work flawlessly. ImpactJS is built on solid javascript functions.

1 decade ago by sleenee

These should be all the relevant parts on my civilian entity

ig.module(
	'game.entities.civilian'
)
.requires(
	'impact.entity',
	'game.menu'
)
.defines(function(){

EntityCivilian = ig.Entity.extend({
	size: {x:32, y:48},
		type: ig.Entity.TYPE.B,
		checkAgainst: ig.Entity.TYPE.B,
		collides: ig.Entity.COLLIDES.PASSIVE,
		friction: {x:0, y:0},
		formerpressed: "down",
	
	animSheet: new ig.AnimationSheet('media/SOT/Characters/003-Fighter03.png', 32, 48 ),	
	
	flip: false,
	
	init: function( x, y, settings ) {
			
		// Add the animations
		this.addAnim( 'idle', 1, [0] );
		this.addAnim( 'walkleft', 0.1, [4,5,6,7] );
		this.addAnim( 'walkright', 0.1, [8,9,10,11] );
		this.addAnim( 'walkup', 0.1, [12,13,14,15] );
		this.addAnim( 'walkdown', 0.1, [0,1,2,3] );
		this.addAnim( 'lookleft', 1, [4] );	
		this.addAnim( 'lookright', 1, [8] );
		this.addAnim( 'lookup', 1, [12] );	
		this.addAnim( 'lookdown', 1, [0] );	

	    this.parent( x, y, settings );
	    //MOVEMENT CODE
	    movementtimer = new ig.Timer();
	    //MOVEMENT CODE 


	//END INIT	 
	},
 charactermovement: function(){
		  
	        var randomdirection=  Math.floor(Math.random()*5)+1;
	                
			if( randomdirection == 1  ) {
				this.vel.x = -64;
				this.vel.y = 0;
				this.currentAnim = this.anims.walkleft;
				this.formerpressed = 'left';
				
			}
			else if( randomdirection == 3  ) {
				this.vel.x = 64;
				this.vel.y = 0;
				this.currentAnim = this.anims.walkright;
				this.formerpressed = 'right';
			}
			else if( randomdirection == 2  ) {
				this.vel.y = -64;
				this.vel.x = 0;
				this.currentAnim = this.anims.walkup;
			
				this.formerpressed = 'up';
			}	
			else if( randomdirection == 4 ) {
				this.vel.y = 64;
				this.vel.x = 0;
				this.currentAnim = this.anims.walkdown;
				
				this.formerpressed = 'down';			
			}
			else 
			{ 
				this.vel.y = 0;
				this.vel.x = 0;
				if(this.formerpressed == 'left')
				{			
					this.currentAnim = this.anims.lookleft;
				}
				else if (this.formerpressed == 'right')
				{			
					this.currentAnim = this.anims.lookright;
				}
				else if (this.formerpressed == 'up')
				{			
					this.currentAnim = this.anims.lookup;
				}
				else if (this.formerpressed == 'down')
				{
					this.currentAnim = this.anims.lookdown;	
				}
			}
	    },
	    //NO MOVEMENT
	    nomovement: function(formerpressed){
			 this.vel.y = 0;
			 this.vel.x = 0;
	    	 if(this.formerpressed == 'left')
	 			{			
	 				this.currentAnim = this.anims.lookleft;
	 			
	 			}
	 			else if (this.formerpressed == 'right')
	 			{			
	 				this.currentAnim = this.anims.lookright;
	 			
	 			}
	 			else if (this.formerpressed == 'up')
	 			{			
	 				this.currentAnim = this.anims.lookup;
	 			
	 			}
	 			else if (this.formerpressed == 'down')
	 			{
	 				this.currentAnim = this.anims.lookdown;
	 			} 
	    },
			//END MOVEMENT CODE 
 
	update: function() {
 if (movementtimer.delta() > 5 && this.vel.y == 0 && this.vel.x == 0  )
        {
         	this.charactermovement();  	
        }
        if (movementtimer.delta() > 6 && this.vel.y > 0 || this.vel.x > 0 )
        {   
     	   this.nomovement(this.formerpressed);
     	   movementtimer = new ig.Timer();
        }
       //END MOVEMENT CODE
    
		this.parent();
	},
draw: function() {
	this.parent();
	}
});

});


It's so strange because the only difference between moving to the left and the right is changing the x-velocity to 64 instead of -64. How could anything else interfere with this?

thanks for anyone who finds the bug

Sleenee

1 decade ago by sleenee

Ok this is going to sound ridiculous.
The problem was simple operant prioritization
if (movementtimer.delta() > 6 && this.vel.y > 0 || this.vel.x > 0 )
        {   
            this.nomovement(this.formerpressed);
            movementtimer = new ig.Timer();
        }


Needs extra brackets so the || is resolved before the &&.
if (movementtimer.delta() > 6 && (this.vel.y > 0 || this.vel.x > 0) )
        {   
            this.nomovement(this.formerpressed);
            movementtimer = new ig.Timer();
        }


Yup, fairly stupid.

Btw my citizens are still hitting walls and stuff. What do you guys use in order to avoid hitting static objects and the edge of the level? The trace method on the collisionmap? And if so, does anyone have a simple implementation example?

1 decade ago by Kxen

From the Docs:
So if you want an entity to opt out of static collision, the easiest way to do this, is to overwrite the .handleMovemenTrace() method for this entity with an empty function.

1 decade ago by dominic

The docs were actually wrong. If you overwrite handleMovementTrace with an empty function, the entity wont move at all. I just updated the collision article:
So if you want an entity to opt out of static collision, the easiest way to do this, is to overwrite the .handleMovemenTrace() method for this entity with the following:

handleMovementTrace: function( res ) {
    // This completely ignores the trace result (res) and always
    // moves the entity according to its velocity
    this.pos.x += this.vel.x * ig.system.tick;
    this.pos.y += this.vel.y * ig.system.tick;
},

Sorry about that.

1 decade ago by sleenee

Hey dominic,

You code you put here is just in case you want to IGNORE static collision right?
Not for making an NPC react to static collision.

Just checking, otherwise I need a paradigm-shift in my head. Still learning to use the engine you see.
Page 1 of 1
« first « previous next › last »