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 Gamma

A few months ago I asked the question of how to make the player get chased by an enemy, specifically on a sidescroller where the player is bound to x and y coordinates excluding angles whatsoever. One of the users, gave me this piece of code which worked perfectly until the player died which gave me some errors, details below. Note the users code had been modified a bit. original post can be found here: Enemy Sensing Players....

Players init function:

ig.game.player = this;

Enemies update function:

    var player = ig.game.getEntitiesByType( EntityPlayer )[0];
    var xdir = this.flip ? -1 : 1;
    
         
    if ( this.distanceTo( ig.game.player) < 300 ) {
        var target = 0;
        
        target = player.pos.x;
        //ig.log ( this.target );
        
        }
        if ( target < this.pos.x ) {
            
            this.flip = true;
            this.vel.x = this.speed * xdir;
            
        } else if ( target > this.pos.x ) {
            
            this.flip = false;
            this.vel.x = this.speed * xdir;
            
        } else {
            
            this.vel.x = this.speed*xdir;
            
        }

Now here's where the problem comes in. The enemy perfectly chases the player just fine, no errors occur at this time, but here's a list of problems that occur.

1.
After the player is killed by the enemy, the console takes out an error and freezes the game. The error states:

"Uncaught TypeError: Cannot read property 'pos' of undefined"

And the error points to the "chasing" code ( the code above in the enemies update function) specifically where it says:

target = player.pos.x; 

2
When the player does not kill the enemy and while the enemy is still chasing the player, the player decides to go a bit further away from the enemy and as the enemy gets closer to a specific spot in which it must return it starts going crazy by staying the same spot moving back and forth rapidly, and I think it has to do with the method below:


// 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.currentAnim.flip.x = this.flip;
    	this.parent();
    },

Can anyone please assist me in figuring out how to make enemies chase a player efficiently without any errors on a sidescroller. I appreciate the help I got last time from Silverspectro. If there isn't a fix or something better, please post some code below I would appreciate it greatly. My game is almost done, this is pretty much the last aspect that I need implemented on the game that had been bugging me all week. Thank you all in advance.

1 decade ago by wands

Shouldn't you check that when target is null enemy will do something else like stop or continue to move forward?

1 decade ago by Gamma

I see what you mean. Since the player is the target, and if he is dead..the target supposed to be null, and the enemy has nothing to do so the game freezes because....the target is dead. Where would I implement this though? Can you show me?

Would it look something like this?

var player = ig.game.getEntitiesByType( EntityPlayer )[0];
    var xdir = this.flip ? -1 : 1;
    
         
    if ( this.distanceTo( ig.game.player) < 300 ) {
        var target = 0;
        
        target = player.pos.x;
        //ig.log ( this.target );
        
        }
        if ( target < this.pos.x ) {
            
            this.flip = true;
            this.vel.x = this.speed * xdir;
            
        } else if ( target > this.pos.x ) {
            
            this.flip = false;
            this.vel.x = this.speed * xdir;
            
        } else if(target === null){
               
                //Do something if player is dead...?

       }else {
            
            this.vel.x = this.speed*xdir;
            
        }


If not can you or someone help me fix this?

1 decade ago by wands

I'll try to help. See if this code works.

 var player = ig.game.getEntitiesByType( EntityPlayer )[0];
    var xdir = this.flip ? -1 : 1;
    
if(player !=null){  //stop checking player when it's dead    
    if ( this.distanceTo( ig.game.player) < 300 ) {
        var target = 0;
        
        target = player.pos.x;
        //ig.log ( this.target );
        
        }
        if ( target < this.pos.x ) {
            
            this.flip = true;
            this.vel.x = this.speed * xdir;
            
        } else if ( target > this.pos.x ) {
            
            this.flip = false;
            this.vel.x = this.speed * xdir;
            
        } else {
            
            this.vel.x = this.speed*xdir;
            
        }
}

1 decade ago by Gamma

It worked!!!! I appreciate your help greatly wands. Thank you so much. I appreciate it. This is awesome.

1 decade ago by Gamma

I also figured out the function in which the enemy goes back from falling of the edge. All I had to reduce is the distance that it detected the player. 300 was way too high.

1 decade ago by wands

Glad that work out for you. What kind of game are you making?

1 decade ago by Gamma

A unique sidescroller with a story behind it. A little horror, not too much. A very exciting adventure, but in a familiar scene. I'll send you a beta of the game when it's ready. The game is called: "Cosmidroid". You can follow me on twitter here: @somnibyte and I'll direct message you beta links of the game (don't have a beta at the moment, there will be one in a few weeks). Thank you for your help. I appreciate it greatly. This game has been in the making for a couple years. You see, in 2010, I got the idea for the game, but I only had some concept art, then a year later, I tried to search for engines, and I had no luck. Around early 2011, I experimented with Java, and the game failed since I had no knowledge of physics or handling images, so then I moved on to C++, which still made no sense to me in terms of how to use it for a game. Then, finally, in 2012 I stumbled upon impact, and in early 2012 I started working on my game through highschool, and now into college It's moving along smoothly. The story, I cannot tell you at the moment, until I release a beta, but the title should give you a small hint.

Have a good day my friend. Thanks for your help.

1 decade ago by wands

Cool!
Page 1 of 1
« first « previous next › last »