SOLVED: Altering Objects Hit Box.

**UPDATE: So to do this:

  • You have to pre-declare your object,
  • Build your object,
  • Use the object:removeSelf() when you want to change the hitbox and immediately re-build the object again with a changed hitbox.
    (See post #2)**

Hi there,
I have an object and i want to alter it’s Hit box.
Think of it as: a man with full body hitbox, then he crouches, i only want half of the full body hitbox.

 local playerbody = display.newImage("Images/Robot.png", \_W, \_H);  
 playerbody:setReferencePoint(display.CenterReferencePoint);  
 playerbody.x = \_W/2-50; playerbody.y = \_H/2;  
 physics.addBody(playerbody, {density = 1.5, friction=1.0, shape={-19, 64, -19, 0, 19, 0, 19, 64 }});  

I can’t seem to change the ‘shape={}’ i’ve attempted variables, but i dont know how to update the object.

I can’t use physics.removeBody() because i’m only a trial user.

I have tried using removeSelf and recreating the image with a smaller bounding box and it works but also crashes the system because playerbody.x & .y are not longer affliated with it and are still being continuosly checked.

Has anyone got a work around for this?
How to update it or change it?

Thanks in advance.
Matt [import]uid: 91798 topic_id: 15664 reply_id: 315664[/import]

Yeah.

Do what you suggested, removing the player and stuff, but predeclare your player :

local playerbody --Predeclare it  
  
 playerbody = display.newImage("Images/Robot.png", \_W, \_H);  
 playerbody:setReferencePoint(display.CenterReferencePoint);  
 playerbody.x = \_W/2-50; playerbody.y = \_H/2;  
 physics.addBody(playerbody, {density = 1.5, friction=1.0, shape={-19, 64, -19, 0, 19, 0, 19, 64 }});  
  
--Remove the body   
playerBody:removeSelf()  
  
--Recreate it later :  
playerbody = display.newImage("Images/Robot.png", \_W, \_H);  
 playerbody:setReferencePoint(display.CenterReferencePoint);  
 playerbody.x = \_W/2-50; playerbody.y = \_H/2;  
 physics.addBody(playerbody, {density = 1.5, friction=1.0, shape={-19, 64, -19, 0, 19, 0, 19, 64 }});  

By predeclaring it, you have access to the variable later, and can re-use it :slight_smile:
[import]uid: 84637 topic_id: 15664 reply_id: 57853[/import]

Oh My Goodness!

I’m astonished it’s as simple as that!
Kind of annoying actually =P

It’s so simple, i thought you hadn’t even written any new code in your code snippet.

Thankyou thankyou thankyou!
Now i can contine on!.. But i need a rest now, been working on that all day.

Thankyou again Dan, Sir (?_?)
[import]uid: 91798 topic_id: 15664 reply_id: 57860[/import]

Glad to help, sometimes the seemingly most complicated problems are the easiest to solve, just takes a different set of eyes or a break from banging your head against the desk :smiley:

Good luck with your game :slight_smile: [import]uid: 84637 topic_id: 15664 reply_id: 57864[/import]