Jump to content

How to make a bat mob?


elsimate
 Share

Recommended Posts

Hi guys, I was wondering how one would make a bat enemy which is in its 'idle' sprite when "hanging" from a roof (.json layer), and swoops down in a U curve every x seconds, changin its direction so it goes back to its primary position after the following x seconds. I use a global gravity, and i read somewhere that body.gravity.y "overrides" the global gravity, but that wasnt quite so in my case. I have a 1000 global gravity, so i had to put the bat gravity to -1200 to get it to stick to the roof... and then the rest of my code didnt work.. i think its the timer fault.

//preloadthis.load.spritesheet('bat1', 'assets/bat1.png', 28, 14);this.load.image('bat0', 'assets/bat0.png');//createthis.bat = this.add.sprite (300,33,'bat0');this.bat.enableBody = true;this.physics.arcade.enable(this.bat);this.bat.body.gravity.y = -200;this.bat.animations.add('fly', [0, 1, 2, 1], 10, true);this.flyingTimer = 0;//updateif (this.game.time.now > this.flyingTimer && this.bat.body.touching.up) {		var direction = 'right';	var flyingTimer = 0;	if (this.direction != 'left') 	{		this.bat.body.velocity.x = 100;		this.bat.body.velocity.y = 500;		this.direction = 'left';	}	else {		this.bat.body.velocity.x = -100;		this.bat.body.velocity.y = 500;		this.direction = 'right';	}this.flyingTimer = this.game.time.now + 3000;}

I was trying to make 1 bat work, and thne figure out how to put several of those into groups .. pls help.

Link to comment
Share on other sites

well... it works kinda.. if i put in a collide, obviously, or the guy will go through the roof... then i changed

if (this.game.time.now > this.flyingTimer && this.bat.body.touching.up) {forthis.batcol = this.physics.arcade.collide(this.bat, this.layer);if (this.game.time.now > this.flyingTimer && this.batcol) {

because if i invoke a function via the collide line , like in an overlap, it magically wont work... but if i assign the boolean i get with collide to another variable, it suddenly works... now i just have to figure out how to properly time it, make it not slide around the top, animate it, and the biggest challenge - make it in bulk with a group or something.. pls help

Link to comment
Share on other sites

I would suggest that you disable gravity for the bat and control all of it's movements directly with a script.

This way you have absolute control over its position at all times, and can make it circle around or swoop as much as you want before returning to where it started.

If you have three or more bats and let them all run the same movement script but start them one after another instead of all at the same time... you'll get a nice flocking effect.

The script could be something like:

[

{ velocityx: 0, velocityy: 0, delay: 1000},

{ velocityx: 0, velocityy: 1, delay: 200},

{ velocityx: 1, velocityy: 2, delay: 200},

{ velocityx: 2, velocityy: 2, delay: 200},

{ velocityx: 2, velocityy: 1, delay: 200},

{ velocityx: 1, velocityy: 0, delay: 200},

{ velocityx: 0, velocityy: -1, delay: 200},

{ velocityx: -1, velocityy: -1, delay: 200},

etc... make all the velocityx add up to zero, and the velocityy too... then it will return to where it started if all the delays are equal

];

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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