Page 2 of 2

Re: Enemy Spawning

Posted: Mon Dec 09, 2013 1:15 am
by kingslovelua
Larsii30 wrote:May you should begin with the code from mickeyjm. It's much easier to understand.

The zombie table didn't get x, y values. They have to be in the self.table create like I wrote or you combine the code from
mickeyjm with mine. just remove function enemy:new and insert his code in the update.

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




I know this is a really old post and I'm not sure if it's not working because this way is out dated or i'm doing something wrong but I using your snippet of code in my project

basically I was trying to spawn new enemies from other character

bots , babies

Code: Select all


require"player"
require"mom"
function botLoad()
	
		--world = love.physics.newWorld(0, 0, true)  --Gravity is being set to 0 in the x direction and 0 in the y direction.

	
	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	
     
	

	bot2 = {}
	 bot2.body = love.physics.newBody(world, 500, 200, "dynamic")
     bot2.body:setMass(100) -- make it pretty light
	 bot2.shape = love.physics.newRectangleShape(0, 0, 15, 15)
	 bot2.fixture = love.physics.newFixture(bot2.body, bot2.shape, 2) 
	 bot2.fixture:setRestitution(0.5)  
     bot2.speed = 50
	 bot2.health = 25
	 bot2.damage = 10	
	 bot2.actX = 500
	 bot2.actY = 200
	
	bot3 = {}
     bot3.body = love.physics.newBody(world, 550, 200, "dynamic")
     bot3.body:setMass(100) -- make it pretty light
	 bot3.shape = love.physics.newRectangleShape(0, 0, 20, 20)
	 bot3.fixture = love.physics.newFixture(bot3.body, bot3.shape, 2) 
	 bot3.fixture:setRestitution(0.5)  
     bot3.speed = 55
	 bot3.health = 45
	 bot3.damage = 20	
	--[[ 
	block1 = {}
	block1.body = love.physics.newBody(world, 200,200,'dynamic')
	block1.shape = love.physics.newRectangleShape(0,0,50,100)
	block1.fixture = love.physics.newFixture(block1.body, block1.shape, 5)
	]]--
end

function botUpdate(dt)
	
	
	if player1.body:getY() > bot1.body:getY() then
		bot1.body:setY( bot1.body:getY() + 20 * dt) 
	end
	if player1.body:getY() < bot1.body:getY() then
		bot1.body:setY(bot1.body:getY() - 20 * dt)
	end
	if player1.body:getX() > bot1.body:getX() then
		bot1.body:setX(bot1.body:getX() + 20 * dt)
	end
	if player1.body:getX() < bot1.body:getX() then
		bot1.body:setX(bot1.body:getX() - 20 * dt) 
	end
	
	
	if player1.body:getY() > bot2.body:getY() then
		bot2.body:setY( bot2.body:getY() + 15 * dt) 
	end
	if player1.body:getY() < bot2.body:getY() then
		bot2.body:setY(bot2.body:getY() - 15 * dt)
	end
	if player1.body:getX() > bot2.body:getX() then
		bot2.body:setX(bot2.body:getX() + 15 * dt)
	end
	if player1.body:getX() < bot2.body:getX() then
		bot2.body:setX(bot2.body:getX() - 15 * dt) 
	end
	
	
if player1.body:getY() > bot3.body:getY() then
		bot3.body:setY( bot3.body:getY() + 10 * dt) 
	end
	if player1.body:getY() < bot3.body:getY() then
		bot3.body:setY(bot3.body:getY() - 10 * dt)
	end
	if player1.body:getX() > bot3.body:getX() then
		bot3.body:setX(bot3.body:getX() + 10 * dt)
	end
	if player1.body:getX() < bot3.body:getX() then
		bot3.body:setX(bot3.body:getX() - 10 * dt) 
	end
	
	
	
	
	 
	 
end

function botDraw()
	
	
	--Bot one
	love.graphics.setColor(51, 255, 204) -- set the drawing color to turquoise for bot1
    love.graphics.polygon("fill", bot1.body:getWorldPoints(bot1.shape:getPoints()))
	--Bot two
	love.graphics.setColor(51, 255, 102) -- set the drawing color to greenisg blue for the bot2
    love.graphics.polygon("fill", bot2.body:getWorldPoints(bot2.shape:getPoints()))
	--Bot three
	love.graphics.setColor(102, 255, 51) -- set the drawing color to green for the bot3
    love.graphics.polygon("fill", bot3.body:getWorldPoints(bot3.shape:getPoints()))
	
	
end


bot1 = {}

function spawnbot1()
    for i=1,100 do --Replace 10 with number of enemies you want
        bot1s[#bot1s+1]={
        x = mother.body:getY(),
        y = mother.body:getY(),
        }
    end
end

function bot1:init()

   self.timer = 0
   self.spawnTime = 3

end

function bot1:update( dt )

   self.timer = self.timer + dt
   if self.timer > self.spawnTime then
     spawnbot1()  -- his code
      self.timer = 0
	  print("spawn")
   end

        -- update anything else enemy related here
end



mom ,boss

Code: Select all


--require"bot"
require"map"
require"player"
function motherLoad()

 mother = {}
	
	 mother.body = love.physics.newBody(world, 590, 200, "dynamic")
     mother.body:setMass(100) -- make it pretty light
	 mother.shape = love.physics.newRectangleShape(0, 0, 35, 35)
	 mother.fixture = love.physics.newFixture(mother.body, mother.shape, 2) 
	 mother.fixture:setRestitution(0.5)  
     mother.speed = 32
	 mother.health = 100
	 mother.damage = 2	
    	 
	
	
	
end

function motherUpdate(dt)


	
if player1.body:getY() < mother.body:getY() then
		mother.body:setY( mother.body:getY() + 1 * dt) 
	end
	if player1.body:getY() > mother.body:getY() then
		mother.body:setY(mother.body:getY() - 1 * dt)
	end
	if player1.body:getX() < mother.body:getX() then
		mother.body:setX(mother.body:getX() + 1 * dt)
	end
	if player1.body:getX() > mother.body:getX() then
		mother.body:setX(mother.body:getX() - 1 * dt) 
	end
	
	


	motherPhysics(dt)
end

function motherDraw()

     love.graphics.setColor(205, 92, 92) -- set the drawing color to indianred for mother for testing 
    --love.graphics.setColor(40, 215, 198, 255) -- set the drawing color to blue for mother
    love.graphics.polygon("fill", mother.body:getWorldPoints(mother.shape:getPoints())) 
	
end

function motherPhysics(dt)


end









I wasn't sure if you code was suppose to be in the bot class or mom class