Attack Physic implementation

HI I am new to corona sdk and game dev. at the moment I am doing some learning on sticker knight example.

Wanted to ask how to implement attack physic and collision

Sticker-Knight-Platformer/blob/master/scene/game/lib/hero.lua

I have added an attack animation in the sprite and I am having trouble changing the physics body during the attack animation, the *hand* punch outside of the initially defined physic body size and it doesn’t do anything at the moment , does anyone know a good way to implement collision event while the hand is punching

I have tried moving instance anchorX during the attack animation so that it will bump into the enemy* but it get jumpy and I don’t think it’s a good idea.

working example code that I can reference would be nice

Please advice, Thank you

    – Add physics

    physics.addBody( instance, “dynamic”, { radius = 54, density = 3, bounce = 0, friction = 1.0 } )

    – Load spritesheet

    local sheetData = { width = 230, height = 200, numFrames = 20, sheetContentWidth = 920, sheetContentHeight = 1000 }

    local sheet = graphics.newImageSheet( “scene/game/img/sample.png”, sheetData )

    local sequenceData = {

        { name = “idle”, frames = { 1, 2, 3 }, time = 333, loopCount = 0 },

        { name = “attack”, frames = { 20 } }

    }

    instance = display.newSprite( parent, sheet, sequenceData )

    instance.x,instance.y = x, y

    instance:setSequence( “idle” )

    instance:play()

You can create the hand separately and track it’s collisions.

Another, perhaps simpler way would be to create a sensor in the location where the punch would occur, in relation to the character, and update it’s location as the character moves. You can accomplish this via different means, such as multi-element body, joint or simply updating the sensor’s coordinates, etc. Then, when the punching animation is at the point where you want to know if it hit something, just check for collisions.

good suggestion, I was able to get a sensor to detect event.selfElement . thanks

I encounter a problem while implementing this, it indeed able to trigger during collision but it did not trigger while it is overlapping with enemy do you have a solution ?

https://imgur.com/a/VsMMJE1

Collision events have two phases: “began” and “ended”.

In order to implement whether an attack like that, you could create a variable such as “local wouldHit = false”. When the collision event begins, you’d set it to true. When the collision event ends, you’d set it back to false. Then, when the user presses the hit/attack button, you’d only need to check if “wouldHit” is true or false.

You can create the hand separately and track it’s collisions.

Another, perhaps simpler way would be to create a sensor in the location where the punch would occur, in relation to the character, and update it’s location as the character moves. You can accomplish this via different means, such as multi-element body, joint or simply updating the sensor’s coordinates, etc. Then, when the punching animation is at the point where you want to know if it hit something, just check for collisions.

good suggestion, I was able to get a sensor to detect event.selfElement . thanks

I encounter a problem while implementing this, it indeed able to trigger during collision but it did not trigger while it is overlapping with enemy do you have a solution ?

https://imgur.com/a/VsMMJE1

Collision events have two phases: “began” and “ended”.

In order to implement whether an attack like that, you could create a variable such as “local wouldHit = false”. When the collision event begins, you’d set it to true. When the collision event ends, you’d set it back to false. Then, when the user presses the hit/attack button, you’d only need to check if “wouldHit” is true or false.