Jump to content

Modify hitbox to just check bottom half or top half of a sprite


Scalemail Ted
 Share

Recommended Posts

I'm attempting to learn Phaser 3 by building a very simple Platformer game. The game is tile based, such that all of my tiles are 32 pixel by 32 pixel. The initial state of my scene is loaded from a 2d array, that I parse into the level during the create phase.

My issue is that my hazard tiles, (floor spikes and ceiling spikes) should have collisions only on the bottom half or top half of the sprite. Is there as way to modify the default hitbox of a sprite and redefine it to only be half the height from either the bottom-to-center or top-to-center?  

Edited by Scalemail Ted
typo
Link to comment
Share on other sites

I found a solution that works, but it is NOT the most elegant of solutions. Here is my current level parsing function, which works for half collisions on floor-spikes and ceiling-spikes:

 

setTile(x, y, tileID){
  if (tileID == "#" ){
    this.platforms.create(x*32 + 16, y*32 + 16, 'brick');
  } 
  else if (tileID == "@" ) {
    this.player = this.physics.add.sprite(x*32+16, y*32+2, 'hero');
    this.player.setCollideWorldBounds(true);
  }
  else if (tileID == "A" ){
    let spike = this.hazards.create(x*32+16, y*32+16, 'spike-floor');
    spike.body.height = 16;
    spike.body.customSeparateY = true;
    spike.body.y += 16;
		
  }
  else if (tileID == "V" ) {
    let spike = this.hazards.create(x*32+16, y*32+16, 'spike-ceiling');
    spike.body.height = 16;	
  } 
}

 

Is there a cleaner way of achieving/expressing the same functionality?

Edited by Scalemail Ted
tab spacing was off in sample code
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...