Questions about my zombie game

When the user touches the gun a bullet fires directly at the zombie . I want the zombie to disappear after a certain amount of hits . For example, if 5 bullets hits the zombie then it will disappear . 

Also how do I make multiple zombies appear without writing a sprite sheet for each of them ? 

hello,

create a table where you put each zombie in it:

table={}

for(int i=0;i<10;i++){

 table[i]=newSpriteSheet(your image)

 table[i].life=5

}

and when you shoot one of them:

talbe[theOneYouShoot].life–

if(talbe[theOneYouShoot].life<=0){table[theOneYouShoot]:removeSelf()}

Sorry I haven’t code for month in lua that why it not exactly in Lua but you have the idea :slight_smile:

I have this code :

local zombie={} function zombieTable( event ) for(int i=0;i\<10;i++){ table[i]=newSpriteSheet("leshy.png") table[i].life=5 } end local options = { --array of tables representing each frame (required) frames = { -- Frame 1 { --all parameters below are required for each frame x = 82, y = 1, width = 80, height = 110 }, -- Frame 2 { x = 1, y = 1, width = 80, height = 110 }, -- Frame 3 and so on... }, -- Optional parameters; used for scaled content support sheetContentWidth = 161, sheetContentHeight = 110 } local imageSheet = graphics.newImageSheet("leshy.png", options ) -- consecutive frames local sequenceData = { name="walking", start=1, count=2, time=750, loopCount = 0, -- Optional ; default is 0 (loop indefinitely) loopDirection = "forward" -- Optional ; values include "forward" or "bounce" } zombie = display.newSprite( imageSheet, sequenceData ) physics.addBody( zombie, "static", {density=3.0, friction=0.5, bounce=0.3, radius=12.5} ) zombie.xScale = -1 zombie:scale( 0.7, 0.7 ) zombie.x = 400 + 100 zombie.y = 280 zombie:play() transition.to( zombie, { x = zombie.x - 400, time = 8000, onComplete = function( self ) self:pause() end } ) --[[local zombie = display.newSprite( imageSheet, sequenceData ) zombie.xScale = -1 zombie.x = display.contentCenterX + 200 zombie.y = display.contentCenterY zombie:play() transition.to( zombie, { x = zombie.x - 400, time = 8000, onComplete = function( self ) self:pause() end } )]] Coins = 0 local centerX = 270 local centerY = 50 CoinsTxt = display.newText( "Coins: "..Coins, centerX, centerY, native.systemFontBold, 20 ) local options = { --array of tables representing each frame (required) frames = { -- Frame 1 { --all parameters below are required for each frame x = 82, y = 1, width = 80, height = 110 }, -- Frame 2 { x = 1, y = 1, width = 80, height = 110 }, -- Frame 3 and so on... }, -- Optional parameters; used for scaled content support sheetContentWidth = 161, sheetContentHeight = 110 } local imageSheet = graphics.newImageSheet("leshy.png", options ) -- consecutive frames local sequenceData = { name="walking", start=1, count=2, time=750, loopCount = 0, -- Optional ; default is 0 (loop indefinitely) loopDirection = "forward" -- Optional ; values include "forward" or "bounce" } zombie2 = display.newSprite( imageSheet, sequenceData ) physics.addBody( zombie2, "static", {density=3.0, friction=0.5, bounce=0.3, radius=12.5} ) zombie2.xScale = -1 zombie2:scale( 0.7, 0.7 ) zombie2.x = 400 + 100 zombie2.y = 280 zombie2:play() transition.to( zombie2, { x = zombie2.x - 400, time = 6000, onComplete = function( self ) self:pause() end } ) local options = { --array of tables representing each frame (required) frames = { -- Frame 1 { --all parameters below are required for each frame x = 82, y = 1, width = 80, height = 110 }, -- Frame 2 { x = 1, y = 1, width = 80, height = 110 }, -- Frame 3 and so on... }, -- Optional parameters; used for scaled content support sheetContentWidth = 161, sheetContentHeight = 110 } local imageSheet = graphics.newImageSheet("leshy.png", options ) -- consecutive frames local sequenceData = { name="walking", start=1, count=2, time=750, loopCount = 0, -- Optional ; default is 0 (loop indefinitely) loopDirection = "forward" -- Optional ; values include "forward" or "bounce" } zombie3 = display.newSprite( imageSheet, sequenceData ) physics.addBody( zombie3, "static", {density=3.0, friction=0.5, bounce=0.3, radius=12.5} ) zombie3.xScale = -1 zombie3:scale( 0.7, 0.7 ) zombie3.x = 400 + 100 zombie3.y = 280 zombie3:play() transition.to( zombie3, { x = zombie3.x - 400, time = 10000, onComplete = function( self ) self:pause() end } )

And I am getting this error :

game.lua:98: '\<name\>' expected near '('

This is NOT lua code (it is JS or C#)

for(int i=0;i\<10;i++){
local zombie={} function zombieTable( event ) local options = { --array of tables representing each frame (required) frames = { -- Frame 1 { --all parameters below are required for each frame x = 82, y = 1, width = 80, height = 110 }, -- Frame 2 { x = 1, y = 1, width = 80, height = 110 }, -- Frame 3 and so on... }, -- Optional parameters; used for scaled content support sheetContentWidth = 161, sheetContentHeight = 110 } local imageSheet = graphics.newImageSheet("leshy.png", options ) -- consecutive frames local sequenceData = { name="walking", start=1, count=2, time=750, loopCount = 0, -- Optional ; default is 0 (loop indefinitely) loopDirection = "forward" -- Optional ; values include "forward" or "bounce" } for i=0;10; do table[i]=display.newSprite( imageSheet, sequenceData ) physics.addBody(table[i], "static", {density=3.0, friction=0.5, bounce=0.3, radius=12.5} ) table[i].life=5 table[i].xScale = -1 table[i]:scale( 0.7, 0.7 ) table[i].x = 400 + 100 table[i].y = 280 table[i]:play() transition.to( table[i], { x = table[i].x - 400, time = 8000, onComplete = function( self ) self:pause() end } ) end end Coins = 0 local centerX = 270 local centerY = 50 CoinsTxt = display.newText( "Coins: "..Coins, centerX, centerY, native.systemFontBold, 20 )

Try with that. It will work better. Don’t hesitate to use table or function it’s help you to don’t have some identical code

good luck

When I run that code nothing shows up . The zombies don’t com on screen and I don’t get errors .

You run the code like this? Oo How the function zombieTable is launch? If you don’t call the function it won’t launch it by itself.

Delete “function zombieTable( event )” and a “end” before Coins = 0

As I’ve said before HRH, you seem to be trying to run before you can walk. You plonk code in from various places and hope for the best. The fact you pasted in a C-style for loop that remi accidently gave you is evidence that you didn’t read the code before you put it in.

You need to ensure you understand what EVERY line of code in your project is doing and why. Like a good pianist can read sheet music and hear the tune in their head, you need to be able to read a line or block of code and visualise what the computer will do with it.

If something doesn’t work as you expected (and even the best coders make mistakes all the time), the only way to find the solution is to work out what the code is doing and why this differs from your intention.

The only way is practise, and building upon solid foundations. Complete a simple project where you didn’t have to ask for help or copy and paste a single line of code. Research, understand, apply. Every time you ask for help and paste the fixed code in without understanding why the poster made the changes they did, you are missing a huge learning opportunity. Yes, your game might work. But next time you get a similar issue, you’re stuck asking for a copy and paste solution again instead of knowing instinctively what is wrong.

@nickSherman is rude but he is right. Learn lua or another language before develop a new project. You will perhaps spend a full day to learn how to code(array, function, listener, table, for/while and if) but then you will win a lot of times because you won’t be stuck somewhere

Personally I don’t think he was being rude at all.

hello,

create a table where you put each zombie in it:

table={}

for(int i=0;i<10;i++){

 table[i]=newSpriteSheet(your image)

 table[i].life=5

}

and when you shoot one of them:

talbe[theOneYouShoot].life–

if(talbe[theOneYouShoot].life<=0){table[theOneYouShoot]:removeSelf()}

Sorry I haven’t code for month in lua that why it not exactly in Lua but you have the idea :slight_smile:

I have this code :

local zombie={} function zombieTable( event ) for(int i=0;i\<10;i++){ table[i]=newSpriteSheet("leshy.png") table[i].life=5 } end local options = { --array of tables representing each frame (required) frames = { -- Frame 1 { --all parameters below are required for each frame x = 82, y = 1, width = 80, height = 110 }, -- Frame 2 { x = 1, y = 1, width = 80, height = 110 }, -- Frame 3 and so on... }, -- Optional parameters; used for scaled content support sheetContentWidth = 161, sheetContentHeight = 110 } local imageSheet = graphics.newImageSheet("leshy.png", options ) -- consecutive frames local sequenceData = { name="walking", start=1, count=2, time=750, loopCount = 0, -- Optional ; default is 0 (loop indefinitely) loopDirection = "forward" -- Optional ; values include "forward" or "bounce" } zombie = display.newSprite( imageSheet, sequenceData ) physics.addBody( zombie, "static", {density=3.0, friction=0.5, bounce=0.3, radius=12.5} ) zombie.xScale = -1 zombie:scale( 0.7, 0.7 ) zombie.x = 400 + 100 zombie.y = 280 zombie:play() transition.to( zombie, { x = zombie.x - 400, time = 8000, onComplete = function( self ) self:pause() end } ) --[[local zombie = display.newSprite( imageSheet, sequenceData ) zombie.xScale = -1 zombie.x = display.contentCenterX + 200 zombie.y = display.contentCenterY zombie:play() transition.to( zombie, { x = zombie.x - 400, time = 8000, onComplete = function( self ) self:pause() end } )]] Coins = 0 local centerX = 270 local centerY = 50 CoinsTxt = display.newText( "Coins: "..Coins, centerX, centerY, native.systemFontBold, 20 ) local options = { --array of tables representing each frame (required) frames = { -- Frame 1 { --all parameters below are required for each frame x = 82, y = 1, width = 80, height = 110 }, -- Frame 2 { x = 1, y = 1, width = 80, height = 110 }, -- Frame 3 and so on... }, -- Optional parameters; used for scaled content support sheetContentWidth = 161, sheetContentHeight = 110 } local imageSheet = graphics.newImageSheet("leshy.png", options ) -- consecutive frames local sequenceData = { name="walking", start=1, count=2, time=750, loopCount = 0, -- Optional ; default is 0 (loop indefinitely) loopDirection = "forward" -- Optional ; values include "forward" or "bounce" } zombie2 = display.newSprite( imageSheet, sequenceData ) physics.addBody( zombie2, "static", {density=3.0, friction=0.5, bounce=0.3, radius=12.5} ) zombie2.xScale = -1 zombie2:scale( 0.7, 0.7 ) zombie2.x = 400 + 100 zombie2.y = 280 zombie2:play() transition.to( zombie2, { x = zombie2.x - 400, time = 6000, onComplete = function( self ) self:pause() end } ) local options = { --array of tables representing each frame (required) frames = { -- Frame 1 { --all parameters below are required for each frame x = 82, y = 1, width = 80, height = 110 }, -- Frame 2 { x = 1, y = 1, width = 80, height = 110 }, -- Frame 3 and so on... }, -- Optional parameters; used for scaled content support sheetContentWidth = 161, sheetContentHeight = 110 } local imageSheet = graphics.newImageSheet("leshy.png", options ) -- consecutive frames local sequenceData = { name="walking", start=1, count=2, time=750, loopCount = 0, -- Optional ; default is 0 (loop indefinitely) loopDirection = "forward" -- Optional ; values include "forward" or "bounce" } zombie3 = display.newSprite( imageSheet, sequenceData ) physics.addBody( zombie3, "static", {density=3.0, friction=0.5, bounce=0.3, radius=12.5} ) zombie3.xScale = -1 zombie3:scale( 0.7, 0.7 ) zombie3.x = 400 + 100 zombie3.y = 280 zombie3:play() transition.to( zombie3, { x = zombie3.x - 400, time = 10000, onComplete = function( self ) self:pause() end } )

And I am getting this error :

game.lua:98: '\<name\>' expected near '('

This is NOT lua code (it is JS or C#)

for(int i=0;i\<10;i++){
local zombie={} function zombieTable( event ) local options = { --array of tables representing each frame (required) frames = { -- Frame 1 { --all parameters below are required for each frame x = 82, y = 1, width = 80, height = 110 }, -- Frame 2 { x = 1, y = 1, width = 80, height = 110 }, -- Frame 3 and so on... }, -- Optional parameters; used for scaled content support sheetContentWidth = 161, sheetContentHeight = 110 } local imageSheet = graphics.newImageSheet("leshy.png", options ) -- consecutive frames local sequenceData = { name="walking", start=1, count=2, time=750, loopCount = 0, -- Optional ; default is 0 (loop indefinitely) loopDirection = "forward" -- Optional ; values include "forward" or "bounce" } for i=0;10; do table[i]=display.newSprite( imageSheet, sequenceData ) physics.addBody(table[i], "static", {density=3.0, friction=0.5, bounce=0.3, radius=12.5} ) table[i].life=5 table[i].xScale = -1 table[i]:scale( 0.7, 0.7 ) table[i].x = 400 + 100 table[i].y = 280 table[i]:play() transition.to( table[i], { x = table[i].x - 400, time = 8000, onComplete = function( self ) self:pause() end } ) end end Coins = 0 local centerX = 270 local centerY = 50 CoinsTxt = display.newText( "Coins: "..Coins, centerX, centerY, native.systemFontBold, 20 )

Try with that. It will work better. Don’t hesitate to use table or function it’s help you to don’t have some identical code

good luck

When I run that code nothing shows up . The zombies don’t com on screen and I don’t get errors .

You run the code like this? Oo How the function zombieTable is launch? If you don’t call the function it won’t launch it by itself.

Delete “function zombieTable( event )” and a “end” before Coins = 0

As I’ve said before HRH, you seem to be trying to run before you can walk. You plonk code in from various places and hope for the best. The fact you pasted in a C-style for loop that remi accidently gave you is evidence that you didn’t read the code before you put it in.

You need to ensure you understand what EVERY line of code in your project is doing and why. Like a good pianist can read sheet music and hear the tune in their head, you need to be able to read a line or block of code and visualise what the computer will do with it.

If something doesn’t work as you expected (and even the best coders make mistakes all the time), the only way to find the solution is to work out what the code is doing and why this differs from your intention.

The only way is practise, and building upon solid foundations. Complete a simple project where you didn’t have to ask for help or copy and paste a single line of code. Research, understand, apply. Every time you ask for help and paste the fixed code in without understanding why the poster made the changes they did, you are missing a huge learning opportunity. Yes, your game might work. But next time you get a similar issue, you’re stuck asking for a copy and paste solution again instead of knowing instinctively what is wrong.

@nickSherman is rude but he is right. Learn lua or another language before develop a new project. You will perhaps spend a full day to learn how to code(array, function, listener, table, for/while and if) but then you will win a lot of times because you won’t be stuck somewhere

Personally I don’t think he was being rude at all.