Jump to content

Creating an "intelligent" enemy


Jendrik
 Share

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;	}};
Link to comment
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.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...