lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


[quote="kingslovelua"]Hey I stuck on a couple of things in my project.

first thing enemy spawning I was looking at this post https://love2d.org/forums/viewtopic.php?f=4&t=11701 and tried to use 


[code]

zombie = {}

function spawnZombies()
    for i=1,10 do --Replace 10 with number of enemies you want
        zombies[#zombies+1]={
        x = math.random(0,728),
        y = math.random(0,428),
        }
    end
end

function zombie:init()

   self.timer = 0
   self.spawnTime = 3

end

function zombie:update( dt )

   self.timer = self.timer + dt
   if self.timer > self.spawnTime then
     spawnZombies()  -- his code
      self.timer = 0
   end

        -- update anything else enemy related here
end

[/code]

and [code]
enemy = {}

function enemy:init()
self.table = {} -- stores the single enemys later
self.timer = 0 -- use with spawntimer
self.spawnTime = 3 -- time when the next enemy should spawn
end
[/code]
Then I create a function to generate a new enemy. This create a table into the self.table( self = enemy / the name of the global table ) for the enemy.
[code]
function enemy:new( x, y, xvel ) -- to use self you have to type a " : " to say that you want to use the self. 
local temp = {
x = x,
y = y,
w = 32,
h = 64,
xvel = xvel,
}
table.insert( self.table, temp )
end
[/code]
Now you have to update the spawn and the enemy itself( here is just the spawner ).
[code]
function enemy:update( dt )

self.timer = self.timer + dt
if self.timer > self.spawnTime then
enemy:new( math.random( -200, width + 200 ) , height - 64 , 150 ) -- math.random for a different position everytime you spawn an enemy
self.timer = 0
end

        -- update anything else enemy related here
end

[/code]
yeah, and now there is only one thing left to do. Draw all enemys.
the ipairs goes through the full self.table ( enemy.table = stores the single enemys ). v is the value of a single enemy, i is the index ( the position of the enemy in the self.table )
[code]
function enemy:draw()
for i, v in ipairs( self.table ) do
lg.setColor( 0, 0, 255 )
lg.rectangle( "line", v.x, v.y, v.w, v.h )
end

end
[/code]

But I don't know if I'm doing it wrong or the code is out dated 

then I'm having issues getting bullets to work and I have looked at [url]http://www.youtube.com/watch?v=S2oO2wbsXQs[/url], [url]https://love2d.org/wiki/Tutorial:Fire_Toward_Mouse[/url] , [url]http://love2d.org/forums/viewtopic.php?f=3&t=41104[/url] , [url]https://love2d.org/forums/viewtopic.php?f=4&t=45989[/url] and [url]http://www.headchant.com/2010/12/31/love2d-%E2%80%93-tutorial-part-2-pew-pew/[/url]
but for some reason I can't get it to work ??



and then for reason I can't get my audio to work out the way I want it to  I have one audio file for the menu and other for in game which i set up as 
[code]function audioLoad()

    if gameState == "menu" then 

  music = love.audio.newSource("sounds/menuMusic.mp3", "stream")
       love.audio.setVolume(4.0)
       love.audio.play(music)
end
    if gameState == "playing"  then
 
  musicInGame = love.audio.newSource("sounds/inGameMusic.mp3", "stream")
       love.audio.setVolume(4.0)
  love.audio.play(musicInGame)
        love.audio.stop(music)

end
end

[/code]



[b]Link to download the file[/b]
[url]https://www.dropbox.com/s/9jt6t69hecr7qr6/Projectzzzz.love[/url][/quote]