Author Topic: Improving my 'Jump on Enemy's Head' Script  (Read 2984 times)


  • Visitor
  • *
  • Posts: 6
    • View Profile
Improving my 'Jump on Enemy's Head' Script
« on: 2006-09-19, 05:22:32 PM »
I'm playing a bit with a game setup to allow me to hurt the player if they touch an enemy, or destroy the same enemy if they jump on it's head. The behavior I wanted to implement had the following characteristics:

- If the player touches an enemy and they are not airborne, they are hurt.
- If the player lands on an enemy after jumping, the enemy is terminated.
- If the player lands on an enemy after falling, the enemy is terminated.

The first thing I did was to create an invisible player inventory item I named 'JumpAttack'. This allowed me to set up simple collision definitions in the Dev Kit... if the player sprite encountered an enemy sprite without JumpAttack in their inventory, they were hurt. If they had JumpAttack in their inventory, the enemy sprite was killed. The player is given an initial quanity of 0 JumpAttack and may have a maximum of 1.

My next step was to implement script that would automatically provide the player with the JumpAttack item if they were falling (which would mean they had either jumped or dropped off a ledge). My solution to this was to add JumpAttack to the player's inventory whenever their downward vertical velocity was active, and remove it otherwise. This was accomplished with the following script (assume this is inserted into a larger batch, I just posted the pertinent sub).

Code: [Select]
Sub Player_OnAfterMoveSprites()

     'The code below detects whether the player is currently falling, which
     'determines whether or not they will kill an enemy if they contact it.
     'If at any time the player's DY value is greater than 0 they are
     'assumed to be falling (in vertical motion down) and will kill any enemy
     'they land on.

     If ProjectObj.GamePlayer.PlayerSprite.DY > 0 Then
          ProjectObj.GamePlayer.InvQuantityOwned(2) = 1
          ProjectObj.GamePlayer.InvQuantityOwned(2) = 0
     End If

End Sub

In the code sample above, Index 2 refers to the JumpAttack item.

This got most of the results I needed (I can terminate enemies if I land on them after a jump or fall) with a few problems.

- If the player is falling yet is in contact with any solid tile (say sliding down the side of a raised wall) they are not properly credited with the JumpAttack item, even though they are moving downward.
- If the enemy is directly below the player (there is no vertical space between the player and the enemy, such as the enemy standing 32 and the player standing on a 32 wall), and the player drops on the enemy, the JumpAttack item is not added quickly enough to prevent damage (I'm assuming there is no room for the player to gain any sort of vertical velocity in this case).

Anyone have any ideas on how to improve this? I'd like to come up with a good method for killing an enemy whenever a PlayerSprite collides with it while in any downward motion--this is a start, but still has problems whenever the player is touching the side of another tile (even if falling).


  • Visitor
  • *
  • Posts: 6
    • View Profile
Re: Improving my 'Jump on Enemy's Head' Script
« Reply #1 on: 2006-09-19, 06:28:39 PM »
Eh, nevermind... I figured out what the problem was. I left a Map Interaction in the level from a previous try at this that was setting the JumpAttack number to 0. Took it out and it works fine now!


  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger -
    • View Profile
    • Email
Re: Improving my 'Jump on Enemy's Head' Script
« Reply #2 on: 2006-09-20, 06:50:38 AM »
Glad you got something that works, but I suspect your results might improve even more if you script the collision between the the player and the enemy instead of trying to maintain inventory to allow the collision to be handled by the built-in collision handling.  Here's are the general steps I would have taken to handle this behavior:
1. Create a collision definition between the player and enemy that does nothing significant (this informs GameDev that it needs to test for these collisions)
2. Create a script function "Player_OnSpritesCollide" to handle sprite collisions in script.
3. In the function, check the CollDefIdx (last) parameter of the OnSpritesCollide event to see if the collision definition index is the one that checks for players colliding with enemies.
4. If so, use the ClsASprIdx and ClsBSprIdx to locate the sprites that collided on the layer and retrieve their positions.
5. If the player sprite was higher than the enemy sprite, activate a function that kills the enemy sprite, otherwise activate a function that hurts the player.


  • Contributor
  • Regular
  • **
  • Posts: 54
    • View Profile
    • Eastfist - Chongchen Saelee
Re: Improving my 'Jump on Enemy's Head' Script
« Reply #3 on: 2006-10-06, 01:55:59 PM »
Here's my own convoluted "Mario" test demo (my main goal was to get the stomp looking right), so my other Mario-type implementations will be buggy. My VB script is heavily commented for anyone interested (I apologize in advance if it's not exactly optimized).

Here's the link:

"What's in a game?"  Juliet says to insult Romeo.