Stuck on a couple of thing

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
kingslovelua
Citizen
Posts: 71
Joined: Mon Sep 26, 2011 7:16 pm

Stuck on a couple of thing

Post by kingslovelua »

Hey I stuck on a couple of things in my project.

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

Code: Select all


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

and

Code: Select all

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
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: Select all

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
Now you have to update the spawn and the enemy itself( here is just the spawner ).

Code: Select all

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

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: Select all

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
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 http://www.youtube.com/watch?v=S2oO2wbsXQs, https://love2d.org/wiki/Tutorial:Fire_Toward_Mouse , http://love2d.org/forums/viewtopic.php?f=3&t=41104 , viewtopic.php?f=4&t=45989 and http://www.headchant.com/2010/12/31/lov ... 2-pew-pew/
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: Select all

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



Link to download the file
https://www.dropbox.com/s/9jt6t69hecr7q ... tzzzz.love
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Stuck on a couple of thing

Post by micha »

Next time, please reduce the download file size: First you have a really large sound file inside and second you have a .love file nested inside the .love file. By removing this you could at least reduce the file size to a half. Also the .love was packed incorrectly. You need to zip the content of the folder into a .zip, not the folder.

And for the actual questions you ask: Could you please state more precisely what you want to achieve and what happens instead? Something like "When I run my game and press x, there should be enemies appearing, but they don't".
kingslovelua
Citizen
Posts: 71
Joined: Mon Sep 26, 2011 7:16 pm

Re: Stuck on a couple of thing

Post by kingslovelua »

micha wrote:Next time, please reduce the download file size: First you have a really large sound file inside and second you have a .love file nested inside the .love file. By removing this you could at least reduce the file size to a half. Also the .love was packed incorrectly. You need to zip the content of the folder into a .zip, not the folder.

And for the actual questions you ask: Could you please state more precisely what you want to achieve and what happens instead? Something like "When I run my game and press x, there should be enemies appearing, but they don't".
Spawning new enemies

Ok basically none of the code is working for me I would like the mother character to spawn new bots about 100 over a course of 3 - 4 minutes and when player gets near her (50px - 100px) she would spawn more bots/ enemies. But for some reason I can't get what I got from the tutorials to work for me nothing happens. I first put the spawn code in the mom class and then require"bot" and did the same for bot class but I'm not sure where it should go the mom, bot or should there be another class called spawn.

Firing bullets

just like the first question I can't get the code to work for me. What I was looking to do for now was a simple shoot when spacebar is down and if I hit a bot/enemy the bullet and the enemy would be removed, but nothing happens.

I feel my problem might have something to do with the i,v pairs I don't full understand it. if there is a good tutorial on i,v pairs please link it to me.

now my third question I wanted there to be a menu background track and then when you start playing the game a new track would start playing
like

if gameState == "menu" then
play menuTrack
end
if gameState == "playing" then
stop menuTrack
play inGameTrack
end

but for some reason only menu track plays and continue to play in game but If I make gameState =="playing" the inGameTrack plays fine I not sure what I'm doing wrong.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Stuck on a couple of thing

Post by micha »

Spawning enemies:
The code that you found will work, but you have to modify it, such that it works together with love.physics.
In your code you have this part, where you manually create an enemy:

Code: Select all

	bot1 = {}
	 bot1.body = love.physics.newBody(world, 450, 200, "dynamic")
     bot1.body:setMass(100) -- make it pretty light
	 bot1.shape = love.physics.newRectangleShape(0, 0, 10, 10)
	 bot1.fixture = love.physics.newFixture(bot1.body, bot1.shape, 2) 
	 bot1.fixture:setRestitution(0.5)  
     bot1.speed = 45
	 bot1.health = 10
	 bot1.damage = 2	
     
Put this code into a function and call it createBot or something. Inside rename the bot1 to newBot and make it lokal. Everytime you call the function then, a new bot should be created. Also make sure to pass all parameters to this function that may vary. So for example you get a function like this:

Code: Select all

function createBot(x,y,width,height,mass,speed,health,damage)
And also: I didn't manage to get to the actual game from your menu. Next time please provide a .love file, where we can directly get to the problem you have. In this case I had to change the code before I could even start your game. (In the menu-update you add all button each frame. Remove this part from the code, otherwise you end up with many thousand buttons).
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest