Implimenting a weapon with a character

I am hoping to learn from others experiences here. I am trying to make a game that has my character constantly moving forward and he will occasionally come by enemies. What is the best way to handle a weapon for the character and how it collides with the enemy?

Should I make the weapon follow the player constantly and literally do a swing motion when the player hits the attack button? I also tried having a invisible rectangle in front of the player, and when the “Attack” button is pressed then when something collides with the invisible rectangle the enemy is destroyed. Issue with this is if the player hits “attack” while an enemy is already collided with the invisible rectangle, the “began” phase of collision has already occured. so that doesnt work out.

Just wanting to know how others have handled this, or any thoughts or opinions, thanks! [import]uid: 19620 topic_id: 21706 reply_id: 321706[/import]

The issue with the phases thing could be avoided by using a flag; eg, set inRange = true on began phase and inRange = false on end phase.

I have used a similar system previously, animating a sprite to look like it is hitting and applying slight backwards force to the enemy “hit”.

Peach :slight_smile: [import]uid: 52491 topic_id: 21706 reply_id: 86131[/import]

oh i understand, doy! should have thought of that one. thanks! Also how would you handle having the weapon follow the player? would you attach it as a physics object with a joint? or use some other method? [import]uid: 19620 topic_id: 21706 reply_id: 86143[/import]

Well I had my player as a sprite and one of the frames made it look like he was attacking although the weapon wasn’t a physical object.

I just knew “OK, this enemy is in range and I pressed attack, so he loses life and gets knocked back a little!” - nothing based on the weapon but visuals.

Would that work for you? If not there would be other ways of handling it, that’s just what worked for me.

Peach :slight_smile: [import]uid: 52491 topic_id: 21706 reply_id: 86268[/import]

Yea i guess i was just wondering if you had a seperate second object thats in front of the player that acts as a “weapon” range sensor? Because i would want to have a player body that senses as well, if it gets hit then i die, then my weapon sensor if attack is touched while an enemy is in range then the enemy dies. Anyways what i am doing now is i attached a dynamic body to the front of my player using a weld joint, this acts as the weapon sensor, it seems to work pretty well. [import]uid: 19620 topic_id: 21706 reply_id: 86325[/import]

Apologies, that’s what I get for commenting after being up playing with Corona all night :wink:

Yes, I used the same (or a similar) method for the front sensor.

Peach :slight_smile: [import]uid: 52491 topic_id: 21706 reply_id: 86419[/import]