Author Topic: Enemies jump?  (Read 2785 times)

!8bit!man

  • Active Member
  • ***
  • Posts: 210
  • Karma: +0/-0
  • Hack Hack Hack!!
    • View Profile
Enemies jump?
« on: Tue, Aug 30, 2011 »
I need an enemy jumps when is near to a block or when is near to the end of a platform, how can i do that?

Cowbacca

  • Active Member
  • ***
  • Posts: 107
  • Karma: +0/-0
    • View Profile
Re: Enemies jump?
« Reply #1 on: Wed, Aug 31, 2011 »
I'd guess create a custom class for the enemy if you have not already done so.  Overwrite the update function, and include a conditional checking if there is a block in front of it to be jumped over or the edge of a platform.  If this is true, set a negative y velocity, assuming the enemy already has a positive y acceleration.

!8bit!man

  • Active Member
  • ***
  • Posts: 210
  • Karma: +0/-0
  • Hack Hack Hack!!
    • View Profile
Re: Enemies jump?
« Reply #2 on: Wed, Aug 31, 2011 »
Yes i know that.... what i need to know is: how the hell i can check if a platform is ending or if an enemy is touching a wall?

for make it jump when is near a block i can use the hitSide() function?

Cowbacca

  • Active Member
  • ***
  • Posts: 107
  • Karma: +0/-0
    • View Profile
Re: Enemies jump?
« Reply #3 on: Thu, Sep 1, 2011 »
If you're using a Tilemap, create a variable to store the tile width, and one to store the distance in pixels you want the enemy to look ahead.  In the update function of your enemy, call overlap(enemy, level, jumpCheck). Then, in your jump check function, put something like this:
Code: [Select]
public function jumpCheck(sprite:FlxSprite, level:FlxTilemap):void {
var x:int;

//check which direction player is moving
if (player.velocity.x > 0) {
//convert from pixel co-ords to tile co-ords
x = Math.floor((sprite.x + sprite.width + lookAheadConstant) / tileWidth); //this is the tile one to the right of the player
}else if (player.velocity.x < 0) {
//convert from pixel co-ords to tile co-ords
x = Math.floor((sprite.x - lookAheadConstant) / tileWidth); //this is the tile one to the left of the player
}

//convert from pixel co-ords to tile co-ords
var y:int = Math.floor((sprite.y + sprite.height - 1) / tileWidth); //this is the y value tile the player is on

//get tile type at those co-ords
var tileType:int = level.getTile(x, y);

//if this tile type is not 0, ie there is a tile there
if (tileType != 0 && player.isTouching(FlxObject.FLOOR)) {
player.velocity.y = -player.maxVelocity.y / 2; //jump
return; //exit the function
}

y += 1; //this is the y value of the tile below the player
tileType = level.getTile(x, y);

//if tile type is 0, ie there is no tile there
if (tileType == 0 && player.isTouching(FlxObject.FLOOR)) {
//jump
player.velocity.y = -player.maxVelocity.y / 2;
}
}

I don't know if that's the most elegant way of doing it but it certainly works.

!8bit!man

  • Active Member
  • ***
  • Posts: 210
  • Karma: +0/-0
  • Hack Hack Hack!!
    • View Profile
Re: Enemies jump?
« Reply #4 on: Thu, Sep 1, 2011 »
is the same thing if it's a random generated level?

and now i made my enemies jump when the platform finished, by a strange effect of a jump logic that i found on internet...is the same thing if it's a random generated level?

and now i made my enemies jump when the platform finished, by a strange effect of a jump logic that i found on internet...


Cowbacca

  • Active Member
  • ***
  • Posts: 107
  • Karma: +0/-0
    • View Profile
Re: Enemies jump?
« Reply #5 on: Thu, Sep 1, 2011 »
My method should work as long as you are using Tilemap, regardless of whether it is randomly generated or not.

camasthecat

  • Contributor
  • ****
  • Posts: 461
  • Karma: +0/-0
  • WOW! 400+ Posts!? Jeez! Do I have a life!?!?
    • View Profile
    • My site for thermalJS, an HTML5/Javascript engine kinda-like flixel:
Re: Enemies jump?
« Reply #6 on: Thu, Sep 1, 2011 »
What I do is this:
Code: [Select]
var tileX:uint = monster.x / tileWidth;
var tileY:uint = monster.y / tileHeight;

if (platform.getTile(tileX - 1, tileY) != 0 && player.x < enemy.x)
{
enemy.velocity.y = -player.maxVelocity.y/2;
}
else if (platform.getTile(tileX + 1, tileY) != 0 && player.x > enemy.x)
{
enemy.velocity.y = -player.maxVelocity.y/2;
}

if (player.x < enemy.x)
{
enemy.acceleration.x = -enemy.maxVelocity.x*4;
}

else if (player.x > enemy.x)
{
enemy.acceleration.x = enemy.maxVelocity.x*4;
}

It works well. :D

!8bit!man

  • Active Member
  • ***
  • Posts: 210
  • Karma: +0/-0
  • Hack Hack Hack!!
    • View Profile
Re: Enemies jump?
« Reply #7 on: Thu, Sep 1, 2011 »
Just a question: i should add that code in my enemy class?

Cowbacca

  • Active Member
  • ***
  • Posts: 107
  • Karma: +0/-0
    • View Profile
Re: Enemies jump?
« Reply #8 on: Fri, Sep 2, 2011 »
The code which camas posted goes in your playstate's update.  However, it does not check for end of platforms and also makes the enemy move horizontally towards the player.

!8bit!man

  • Active Member
  • ***
  • Posts: 210
  • Karma: +0/-0
  • Hack Hack Hack!!
    • View Profile
Re: Enemies jump?
« Reply #9 on: Fri, Sep 2, 2011 »
That's enough.... thanks ;)

camasthecat

  • Contributor
  • ****
  • Posts: 461
  • Karma: +0/-0
  • WOW! 400+ Posts!? Jeez! Do I have a life!?!?
    • View Profile
    • My site for thermalJS, an HTML5/Javascript engine kinda-like flixel:
Re: Enemies jump?
« Reply #10 on: Sat, Sep 3, 2011 »
Yeah, I haven't made the enemies that smart yet...

So far I have made the enemy see something in front of it, and only "See" the player so far.
When I say "see", I mean he only knows where I am if I am close to him.
XD

!8bit!man

  • Active Member
  • ***
  • Posts: 210
  • Karma: +0/-0
  • Hack Hack Hack!!
    • View Profile
Re: Enemies jump?
« Reply #11 on: Sun, Sep 4, 2011 »
but, what does "monster.x" means in your case?
and how can i get tileWidth and tileHeight values?
« Last Edit: Sun, Sep 4, 2011 by !8bit!man »

camasthecat

  • Contributor
  • ****
  • Posts: 461
  • Karma: +0/-0
  • WOW! 400+ Posts!? Jeez! Do I have a life!?!?
    • View Profile
    • My site for thermalJS, an HTML5/Javascript engine kinda-like flixel:
Re: Enemies jump?
« Reply #12 on: Mon, Sep 5, 2011 »
Oh. Oops. monster.x is the x position of my enemy.
I named it monster at first, but I then changed it to enemy. Sorry about that.
The tileWidth is the width of the individual tiles;
you just do:
public var tileWidth:Number = 25;
The 25 should be changed to what your tiles width is.
Do the same for the tileHeight.

!8bit!man

  • Active Member
  • ***
  • Posts: 210
  • Karma: +0/-0
  • Hack Hack Hack!!
    • View Profile
Re: Enemies jump?
« Reply #13 on: Wed, Sep 7, 2011 »
ehy, but platform as i see in another thread, is the name of my map, right?
well, i'm using a random generated level wich have a variable called block(FlxGroup) that "contains" all the single tiles the game put randomly. does it work in the same way?

camasthecat

  • Contributor
  • ****
  • Posts: 461
  • Karma: +0/-0
  • WOW! 400+ Posts!? Jeez! Do I have a life!?!?
    • View Profile
    • My site for thermalJS, an HTML5/Javascript engine kinda-like flixel:
Re: Enemies jump?
« Reply #14 on: Wed, Sep 7, 2011 »
Is it a FlxTilemap? If so, then I think yes. If not, I'm guessing no.  :-\

!8bit!man

  • Active Member
  • ***
  • Posts: 210
  • Karma: +0/-0
  • Hack Hack Hack!!
    • View Profile
Re: Enemies jump?
« Reply #15 on: Thu, Sep 8, 2011 »
no it's a FlxGroup.... can i place the random generated blocks in a tilemap too?

camasthecat

  • Contributor
  • ****
  • Posts: 461
  • Karma: +0/-0
  • WOW! 400+ Posts!? Jeez! Do I have a life!?!?
    • View Profile
    • My site for thermalJS, an HTML5/Javascript engine kinda-like flixel:
Re: Enemies jump?
« Reply #16 on: Thu, Sep 8, 2011 »
Hmm... Mode doesn't use FlxTilemap, It uses something called FlxTileBlock or something... don't know what that is...

I think getTile is made especially for FlxTilemap though. :(

I'll look into it.

EDIT: I'm very sure that getTile only works with FlxTilemap. Sorry. :(
BUT I think the getTile works with FlxTileBlock...
Mode uses FlxTileBlock, so I think it would be best to look there!
« Last Edit: Thu, Sep 8, 2011 by camasthecat »

!8bit!man

  • Active Member
  • ***
  • Posts: 210
  • Karma: +0/-0
  • Hack Hack Hack!!
    • View Profile
Re: Enemies jump?
« Reply #17 on: Fri, Sep 9, 2011 »
i'll try... thanks for your help