Add a life to my game

Hello, excuse my google language.

I am creating a platform game based on the game “Sticker-Knight”, I try to add a life when hero collides with the object life but I can not do it, please can someone guide me ?, this is the code:

game.lua --> here I charge the module of the lives to show them to me:

shield = heartBar.new() shield.x = 48 shield.y = display.screenOriginY + shield.contentHeight / 2 + 16 hero.shield = shield

heartBar.lua --> this is the module that creates lives:

local M = {} function M.new( options ) -- Default options for instance options = options or {} local image = options.image local max = options.max or 3 local spacing = options.spacing or 8 local w, h = options.width or 64, options.height or 64 -- Create display group to hold visuals local group = display.newGroup() local hearts = {} for i = 1, max do hearts[i] = display.newImageRect( "scene/game/img/shield.png", w, h ) hearts[i].x = (i-1) \* ( (w/2) + spacing ) hearts[i].y = 0 group:insert( hearts[i] ) end group.count = max function group:damage( amount ) group.count = math.min( max, math.max( 0, group.count - ( amount or 1 ) ) ) for i = 1, max do if i \<= group.count then hearts[i].alpha = 1 else hearts[i].alpha = 0.2 end end return group.count end function group:heal( amount ) self:damage( -( amount or 1 ) ) end function group:finalize() -- On remove, cleanup instance end group:addEventListener( "finalize" ) -- Return instance return group end return M

life.lua --> here the code of the collision function:

local composer = require( "composer" ) local fx = require( "com.ponywolf.ponyfx" ) local heartBar = require( "scene.game.lib.heartBar" ) -- Define module local M = {} function M.new( instance, options ) if not instance then error( "ERROR: Expected display object" ) end -- Get scene and sounds local scene = composer.getScene( composer.getSceneName( "current" ) ) local sounds = scene.sounds ------------------------------------------------------------------------------- function instance:collision( event ) local phase, other = event.phase, event.other if phase == "began" and other.type == "hero" then audio.play( sounds.coin ) scene.score:add( 500 ) display.remove( self ) --"here I want to execute the code to add a life to the counter" end end ---------------------------------------------------------------------------------------- instance.\_y = instance.y physics.addBody( instance, "static", { isSensor = true } ) --transition.from( instance, { y = instance.\_y - 16, transition = easing.outBounce, time = 500, iterations = -1 } ) instance:addEventListener( "collision" ) return instance end return M

Greetings and thanks in advance, I hope you have explained me well

Hi @quinohp,

Try

other.shield:heal( 1 )

or 

other.shield:heal()

Both lines do the same.

Have a nice day:)

ldurniat

Hi ldurniat, thanks for responding, I’ve tried the two options but it does not work, only the audio and the score work:

 function instance:collision( event ) local phase, other = event.phase, event.other if phase == "began" and other.type == "hero" then audio.play( sounds.coin ) scene.score:add( 500 ) display.remove( self ) other.shield:heal(1) end end

Good day.

You don’t get more lives than you pass (max parameter, default 3) for heartBar object. So you need lose some lives before you can get it back.

Solved ldurniat, I have not fallen into the parameter “max” that limited my lives in 3.

Thanks for helping me, I am very grateful

regards

Hi @quinohp,

Try

other.shield:heal( 1 )

or 

other.shield:heal()

Both lines do the same.

Have a nice day:)

ldurniat

Hi ldurniat, thanks for responding, I’ve tried the two options but it does not work, only the audio and the score work:

 function instance:collision( event ) local phase, other = event.phase, event.other if phase == "began" and other.type == "hero" then audio.play( sounds.coin ) scene.score:add( 500 ) display.remove( self ) other.shield:heal(1) end end

Good day.

You don’t get more lives than you pass (max parameter, default 3) for heartBar object. So you need lose some lives before you can get it back.

Solved ldurniat, I have not fallen into the parameter “max” that limited my lives in 3.

Thanks for helping me, I am very grateful

regards