Eliminate maximum lives in a game

Hi folks, I’m trying to add lives to a game, but the math.max limiter prevents me from doing so. Anyone know how to remove math.max without harming the script?

code i’m using:

-- Define module 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

With this module, I show the lives:

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

When hero collides with a life, I add it like this:

 function instance:collision( event ) local phase, other = event.phase, event.other if phase == "began" and other.type == "hero" then other.shield:heal(1) end end

The game is inspired by the famous Sticker Knight Platformer master.

regards

I’m assuming group.count equals lives as that is what math.min and math.max are acting on?

You might want to change your variable “max” to “maxLives” so it is not confusing when when you have “max” and “math.max” next to one another in a calculation.

We need to know how the variable “amount” is calculated or its value

[lua]

– we don’t know what “amount” is

group.count = math.min( max, math.max( 0, group.count - ( amount or 1 ) ) )

[/lua]

It looks to me like that whole math.min() computation is to determine the number of hearts to show at full alpha and how many to show at 0.2 alpha. When creating the object, there is a table of options and the value passed in there options.max should be the count of hearts to show. Setting that to the number you want should make it work.  Then to gray out a heart call the damage function and let the math.min and math.max functions do their job.

Ideally you shouldn’t need to change this as the number of hearts can be defined when you create the object.

Rob

The maximum of lives is set to 3, and what I want is to add lives to max. I have managed to do it by removing the maximum value to the group: damage (amount) function:

before:

group.count = math.min( max, math.max( 0, group.count - ( amount or 1 ) ) )

after:

group.count = math.min( math.max( 0, group.count - ( amount or 1 ) ) )

… this works for me, and I can store all the lives I want in the table, the problem is that they are not displayed in the image module

You probably want to add a function/method to the object like group:damage() and group:heal(). Maybe call it group:addLives()

The variable max is local to the object, so you probably could take whatever value you pass in and just add it to max.

Please respond in English.

See our Forum rules: https://coronalabs.com/forum-rules/

Thanks!

Rob

Sorry for the lapse.

I created the function group: addLives () and call it with other.shield: addLives (). It increases my lives in the counting of tables, but I still don’t visualize them:

 function group:addLives() group.count = group.count + 1 end