Enemies chasing player

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
chingydongy
Prole
Posts: 5
Joined: Sat Jan 20, 2018 4:56 pm

Enemies chasing player

Post by chingydongy »

Now I am stuck on a part where enemies are supposed to chase the player but the when i run the thing, it gives me an error where it tries to do an arithmetic on field 'x' and says its a nil value, but the enemies should probably be spawned already by the time the last piece is loaded.

Code: Select all

require("player")
enemy = {}
enemySpeed = 10
math.randomseed(os.time())
score = 0
enemyNumber = 5
killTimeStart = 10 -- time limit/ level
killTime = killTimeStart
maxw, maxh = love.window.getDesktopDimensions(display)
timer = 0
spawnTime = 5 -- delay of spawn in seconds
round = 4 -- this is the amount of time that is multiplied by delta time (1s) to add to time allowed for the next round
function enemySpawn(x,y,w,h)
   for i = 1, enemyNumber do
      enemy[#enemy + 1] = {x = math.random(0 and player.x - 320, player.x + 300 and maxw - 64), y = math.random(0 and player.y - 320, player.y + 300 and maxh - 64), w = 64, h = 64}
   end
    if #enemy <= enemyNumber then
      enemyNumber = enemyNumber + math.random(1,3)
   end
end
function collcheck(dt)
   for i,v in ipairs(enemy) do      --collision check for bullet vs enemy
    for a,b in ipairs(bullet) do
      if b.x < v.x+64 and b.y < v.y+64 and
         b.x > v.x-8 and b.y > v.y-8 then
         table.remove(enemy, i)
         table.remove(bullet, a)
         score = score + 1
      end
   end
 end
end
function enemy:update(dt)
   killTime = killTime - dt
   		if killTime < 0 and #enemy > 0 then
      		killTime = killTimeStart
      	end
      	if killTime >=0 and #enemy == 0 then -- if there's remaining kill time and there are no enemies on the screen, increase the round time by 5s
         	killTime = killTime + round*dt -- done
   		end
   if #enemy == 0 then -- if all enemies are dead, start the timer for the next round (5s)
      timer = timer + dt
      if timer > spawnTime then
         enemySpawn()
         timer = 0
      end
   end
   collcheck()
end
function enemy:draw()
	for i,v in ipairs(enemy) do
		love.graphics.setColor(255,0,200)
		love.graphics.rectangle("fill", v.x, v.y, v.w, v.h)
	end
end
function enemymove(dt)
   for _,enememe in ipairs(enemy) do
      local enemyDirectionX = player.x - enemy.x
      local enemyDirectionY = player.y - enemy.y
      local distance = math.sqrt(enemyDirectionX * enemyDirectionX + enemyDirectionY * enemyDirectionY)
   end
   if distance ~= 0 then
      enemy.x = enemy.x + enemyDirectionX / distance * enemySpeed * dt
      enemy.y = enemy.y + enemyDirectionY / distance * enemySpeed * dt
   end
end
I've tried multiple times with multiple different ways of making the enemies chase the player, but none work simply because of the error. Any and all help would be appreciated because I only recently got into Lua
User avatar
galaxitic
Prole
Posts: 1
Joined: Sun Jan 21, 2018 9:58 pm

Re: Enemies chasing player

Post by galaxitic »

Hello!

I also just recently got into Lua/Love2D, but here's my two cents on the subject...

Is the error being reported on line 58 or 59, by any chance?
If that's so, I noticed your "for" variables on line 57 are "_" and "enememe" for the index and object respectively.
Yet, on lines 58 and 59 you're using "enemy" to get the "x" attribute.
Could it be you should use "enememe" instead of "enemy" in this case?

Best regards!
chingydongy
Prole
Posts: 5
Joined: Sat Jan 20, 2018 4:56 pm

Re: Enemies chasing player

Post by chingydongy »

galaxitic wrote: Sun Jan 21, 2018 10:22 pm Hello!

I also just recently got into Lua/Love2D, but here's my two cents on the subject...

Is the error being reported on line 58 or 59, by any chance?
If that's so, I noticed your "for" variables on line 57 are "_" and "enememe" for the index and object respectively.
Yet, on lines 58 and 59 you're using "enemy" to get the "x" attribute.
Could it be you should use "enememe" instead of "enemy" in this case?

Best regards!
Yes, most likely. I have changed this part and back to no avail, but probably wasn't working because I had the wrong pieces changed to enememe. I'll fix this when I can and post results.

Cheers!
chingydongy
Prole
Posts: 5
Joined: Sat Jan 20, 2018 4:56 pm

Re: Enemies chasing player

Post by chingydongy »

Well, I managed to get it to work by doing this:

Code: Select all

function enemymove(dt)
   for i,v in ipairs(enemy) do
       enemyDirectionX = player.x - v.x
       enemyDirectionY = player.y - v.y
       distance = math.sqrt(enemyDirectionX * enemyDirectionX + enemyDirectionY * enemyDirectionY)
   end
   if distance ~= 0 then
      for i,v in ipairs(enemy) do
         v.x = v.x + enemyDirectionX / distance * enemySpeed * dt
         v.y = v.y + enemyDirectionY / distance * enemySpeed * dt
      end
   end
end
(adding another for in the if)
Now the program is taking them all as one entity and is probably calculating the nearest's coordinates in relation to mine. I'll try things out but I'm not sure if I can get it to take every enemy's position separately.

edit: not nearest, but it takes a value from the array
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 79 guests