Sign in to follow this  
Jendrik

Creating an "intelligent" enemy

Recommended Posts

Hi all!

I´m trying to get an enemy who flees from the player. But I have no idea how to get him jump.

The following code makes that he ran and stop on an hindernis. But he don´t want to jump... Any ideas?

window.wolf.state.play = {	preload: function(){		console.log("loading play state");	},		create: function(){		console.log("starting play state");				this.bg = mt.create("bg");		this.unten = mt.create("unten");		this.wolf = mt.create("wolf");		this.schaaf = mt.create("schaaf");		this.hindernis = mt.create("hindernis");		//this.points = mt.create("points");				this.cursors = this.game.input.keyboard.createCursorKeys();				this.game.camera.follow(this.wolf);	},		update: function(){				this.game.physics.arcade.collide(this.wolf, this.unten);		this.game.physics.arcade.collide(this.wolf, this.hindernis);		this.game.physics.arcade.collide(this.schaaf, this.unten);		this.game.physics.arcade.collide(this.schaaf, this.hindernis);				if (this.cursors.left.isDown) {        	this.wolf.body.velocity.x = -200;		} else if (this.cursors.right.isDown) {			this.wolf.body.velocity.x = 200;		} else {			this.wolf.body.velocity.x = 0;		} if (this.cursors.up.isDown) {			this.wolf.body.velocity.y = -300;		}								if(this.game.physics.arcade.distanceBetween(this.schaaf, this.hindernis) < 50){		   console.log("hüpf!");		   this.schaafJump();		   this.schaaf.body.velocity.x = 180;		}else{		   this.schaaf.body.velocity.x = 180;		}	},		schaafJump: function(){		this.schaaf.body.velocity.x = 0;		this.schaaf.body.velocity.y = -100;	}};

Share this post


Link to post
Share on other sites

The easiest way is to check for a collision with the hindernis object and then call the jump method.  You can change:

this.game.physics.arcade.collide(this.schaaf, this.hindernis);

to:

this.game.physics.arcade.collide(this.schaaf, this.hindernis, function(schaaf, hindernis){     schaaf.body.velocity.x = 0;     schaaf.body.velocity.y = -100;});

This should work.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.