Shooting a gun

I tried to make the object a bullet and it didn’t work .

Remember that layering code I gave you?  Try placing different objects in different layers. 

  • Put your background image in layers.underlay.
  • Put the sprite in layers.content

Okay since I fount it complicated with doing what you said I put the background outside of the function game.create( group )

 and now the coins and the zombie is showing with the background there 

I’m disappointed to read this.   It  is a terrible idea and totally defeats the purpose of the content and instruction I’ve given you so far.  

If you can’t use the game.lua construct I gave you and understand the concepts of scope, visibility, group layering, the painters-algorithm,… you are going to find yourself in the same place you started this thread.

@Brandon,

I hope this gets you going again: https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2017/12/brandonCombining.zip

(_ may take a while to process to full resolution _)

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

I posted the wrong video but now the links right.  Please wait for it to finish processing.

It’s still not showing up . It says the video is unavailable 

Try it now.

Great I fixed it 

How do i give the zombie a body so when the bullet hits the zombie the users points go up by 5 points . I tried to use isBullet but that didn’t help . It made no change at all

You need to learn about collision detection next and detect when the bullet hits the zombie.  See:

https://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Rob

I tried to add a body and the zombie is just falling down .

zombie = display.newSprite( imageSheet, sequenceData ) physics.addBody( zombie, {density=3.0} )

local Coins = 0 local centerX = 350 local centerY = 60 local CoinsTxt = display.newText( "Coins: "..Coins, centerX, centerY, native.systemFontBold, 20 ) end local bullet = {} local bCounter = 1 local function shootBullet(event) if event.phase == "ended" then bullet[bCounter] = display.newImage( "bullet5.png", 40, 298, 1, 6) bullet[bCounter].value = bCounter physics.addBody( bullet[bCounter], "dynamic" ) bullet[bCounter].gravityScale = 0 bullet[bCounter].myName = "bullet" bullet[bCounter]:setLinearVelocity( 2100, -20 ) bCounter = bCounter + 1 end end local function onCollision( self, event ) if event.phase =="began" then Coins = Coins + 5 CoinsTxt.text="Coins: "..Coins end end function game.start( ) -- Game is already started, just exit if( isRunning ) then return end -- Mark game as running isRunning = true gun:addEventListener( "touch", shootBullet ) end

Whenever I add an event listener for the zombie I get a couple of errors 

@Brandon,

You should put you game on Pause, then go read the physics guide and poke around the physics examples that come with Corona till you understand:

  • bodies 
  • gravity
  • linearVelocity
  • forces

After that you’ll be able to understand why your zombie ‘falls’ when you add a body.  Till then, us just telling you won’t help a lot.  It will only move you forward incrementally.

Note: Your effort has inspired me (still thinking about it) to make a side-scroller template.  I’ll post back here if I make it OK?

Sure no problem .

OHHHH I had to add a static body 

When I shoot the gun it doesn’t touch the zombie . It’s like it touches the air around the zombie and bounces back .

Okay I fixed it all . Thanks to everyone who helped .

I want to make a bomb explosion . Do I do that witht he same code for the zombie ? I already have the sprite sheets 

I’d suggest starting without composer.* or any other scene framework.

i.e. Create a module that does all the work.

This module should have at least two functions:

  • mod.create( group ) - Creates game content and places it in the ‘passed in’ group.
  • mod.destroy() - Stops and destroys the game.

I would also suggest it have these functions:

  • mod.start() - Separates the starting logic from the creation logic which is cleaner.
  • mod.stop() - Separates stopping from destruction which may be useful to you later.

Then, if you can get your game (in a module) working with these tests, you’ll be ready to use that same module in composer.*

Test 1

local mod = require "mod" -- or whatever you named the module file. mod.create( display.currentStage ) timer.performWithDelay( 1000, mod.destroy ) -- If this doesn't crash you're ready for the next test.

Test 2

local mod = require "mod" -- or whatever you named the module file. mod.create( display.currentStage ) timer.performWithDelay( 1000, mod.start ) -- assuming you have a start() timer.performWithDelay( 2000, mod.stop ) -- assuming you have a stop() timer.performWithDelay( 3000, mod.destroy ) -- If this doesn't crash you're ready for the next test.

Test 3

Now, play your game for a while, and further tweak the module.  Later, when you think it is time to move to composer.* run test 1 and 2 again.

I see a lot of folks write their code directly in a composer.* scene and it often goes terribly wrong because they don’t have a good grasp of scope, display group rules, etc.  

So again, I suggest starting of in the simplest modular setting ( i.e. A single module) then move on to composer.*, game interfaces, etc.

Note I give this advice, because if you follow it, then writing the composer.* game scene is as easy as this:

local mod = require "mod" function scene:create(event) mod.create( self.view ) end function scene:show(event) if (event.phase == "did") then mod.start() end end function scene:hide(event) if (event.phase == "will") then mod.stop() end end function scene:destroy(event) mod.destroy() end