Physic help! when press attack button = enemy die, when attack button not pressed = life -1

at collision i wanna make sprite have 2 function,

if the sprite play “attack” the enemy die, if sprite play “right” <---- move right, collide with enemy then life - 1

is that possible??

Hi @kali8pm,

Yes, this definitely possible. The community/staff will just need to see some of your code before we can begin helping you. There are many ways that you might accomplish this, so we need to see your approach.

When posting code, please remember to surround it with “lua” tags for clarity in the forums:

[lua] -- code [/lua]

Take care,

Brent

here my code… pls help me

[lua]

function right:touch( ) <--------------------------------function move right
  
  motionx = speed
  coba:setSequence(“kanan”) <--------------------------- animation move right
  coba:play()

 end


function serang:touch(event ) <-------------------------------------------- function when button press, animation attack play
  if event.phase == “began” then
  motionx = speed
  coba:setSequence(“tendang”) <------------------------------------------ animation attack
  coba:play()
  
 else
  if event.phase ==“ended” then
end
end
end


coba.type = “coba”  <--------- hero

tentara.type = “tentara” <--------- enemy

[/lua]

pls help me  :frowning:

So, at this time, are you asking about how to detect collisions between the hero and enemy?

Brent

@Brent : yes sir…  if hero mode is “attack” then life not -1, else if enemy mode “move right” then life -1

can u show me any template of that function??

Have you implemented physics and collision detection yet? That is the first step.

If you haven’t, here are some resources to view/read:

Setup: http://docs.coronalabs.com/guide/physics/physicsSetup/index.html

Bodies: http://docs.coronalabs.com/guide/physics/physicsBodies/index.html

Collisions: http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

http://www.youtube.com/watch?v=nRsXKmCU4D8&feature=youtu.be

https://www.youtube.com/watch?v=UyN0LBTiN-c&feature=youtu.be

https://www.youtube.com/watch?v=3n3aboHg3v4&feature=youtu.be

@Brent: i already make collusion… only life-1 function, but no “attack” function yet

here my code function remove healtbar (3 image = nyawa3,nyawa2,nyawa1)

[lua]

life =3 <— HP value

function kurangdarah(event ) <---- function remove image healtbar
 if life==3 then

  coba.isVisibile= true <— hero true
  life = life-1
  nyawa3:removeSelf() <— image of healtbar
  
  elseif life==2 then
   coba.isVisibile= true
   life = life-1
   nyawa2:removeSelf()
   elseif life==1 then
    coba.isVisibile= true
    life = life -1
    nyawa1:removeSelf()
    gameOver()

   else
    
end
end

[/lua]

here my collision code…

[lua]

function cobaCollision( self, event )
 if event.phase == “began” then

  if event.target.type == “coba” and event.other.type ==“tentara” then
   kurangdarah() <— function remove healtbar…

end

end

[/lua]

I need to know how to implement hero “attack”,

There should be a function which performs the enemy attack animation, yes?

@Brent: yes sir… i use it on widget…
the problem is… how to makes HP not -1 when i meet enemy with attack sprite mode…

i duno how to implemen that function :frowning:

Do you want to check if the player’s sprite animation sequence is equal to something (attacking or not attacking) to determine which action to take?

Brent

@Brent : yes sir… any template of that function?
i just confused a lot :frowning:

Hi @kali8pm,

During the collision, you can check which “sequence” is playing by using the “sprite.sequence” property. See here:

https://docs.coronalabs.com/api/type/SpriteObject/sequence.html
 

Brent

[lua]
local current_sequence = spriteObj.sequence
[/lua]

so i must write this??
[lua]
local attack = spriteObj.sequence
[/lua]

where i must put that code??
sry for my noobnest sir :frowning:

Hi @kali8pm,

Yes, this definitely possible. The community/staff will just need to see some of your code before we can begin helping you. There are many ways that you might accomplish this, so we need to see your approach.

When posting code, please remember to surround it with “lua” tags for clarity in the forums:

[lua] -- code [/lua]

Take care,

Brent

here my code… pls help me

[lua]

function right:touch( ) <--------------------------------function move right
  
  motionx = speed
  coba:setSequence(“kanan”) <--------------------------- animation move right
  coba:play()

 end


function serang:touch(event ) <-------------------------------------------- function when button press, animation attack play
  if event.phase == “began” then
  motionx = speed
  coba:setSequence(“tendang”) <------------------------------------------ animation attack
  coba:play()
  
 else
  if event.phase ==“ended” then
end
end
end


coba.type = “coba”  <--------- hero

tentara.type = “tentara” <--------- enemy

[/lua]

pls help me  :frowning:

So, at this time, are you asking about how to detect collisions between the hero and enemy?

Brent

@Brent : yes sir…  if hero mode is “attack” then life not -1, else if enemy mode “move right” then life -1

can u show me any template of that function??

Have you implemented physics and collision detection yet? That is the first step.

If you haven’t, here are some resources to view/read:

Setup: http://docs.coronalabs.com/guide/physics/physicsSetup/index.html

Bodies: http://docs.coronalabs.com/guide/physics/physicsBodies/index.html

Collisions: http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

http://www.youtube.com/watch?v=nRsXKmCU4D8&feature=youtu.be

https://www.youtube.com/watch?v=UyN0LBTiN-c&feature=youtu.be

https://www.youtube.com/watch?v=3n3aboHg3v4&feature=youtu.be

@Brent: i already make collusion… only life-1 function, but no “attack” function yet

here my code function remove healtbar (3 image = nyawa3,nyawa2,nyawa1)

[lua]

life =3 <— HP value

function kurangdarah(event ) <---- function remove image healtbar
 if life==3 then

  coba.isVisibile= true <— hero true
  life = life-1
  nyawa3:removeSelf() <— image of healtbar
  
  elseif life==2 then
   coba.isVisibile= true
   life = life-1
   nyawa2:removeSelf()
   elseif life==1 then
    coba.isVisibile= true
    life = life -1
    nyawa1:removeSelf()
    gameOver()

   else
    
end
end

[/lua]

here my collision code…

[lua]

function cobaCollision( self, event )
 if event.phase == “began” then

  if event.target.type == “coba” and event.other.type ==“tentara” then
   kurangdarah() <— function remove healtbar…

end

end

[/lua]

I need to know how to implement hero “attack”,

There should be a function which performs the enemy attack animation, yes?