[Solved]i want to make the enemy kill the player but it keeps giving me the error(read below)

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
NathanGaming2005
Prole
Posts: 3
Joined: Wed Jan 02, 2019 1:24 pm

[Solved]i want to make the enemy kill the player but it keeps giving me the error(read below)

Post by NathanGaming2005 »

Code: Select all

function love.load()
player = {x = 200,y = 200,Speed = 400, width = 30, height = 50}
enemy = {x = 150,y = 150, speed = 1000}
points = {{x = 400,y = 555}}
pointscounter = 0


end

function love.draw()
	love.graphics.setColor(144,255,71)
[b]Here[/b]	 for i, v in pairs(player)	do
	love.graphics.rectangle("fill",v.x,v.y,v.width,v.height)
end
	love.graphics.setColor(245, 58, 252)
	for i , v in pairs(points) do
	love.graphics.rectangle("fill",v.x,v.y,10,10)
	end
	love.graphics.setColor(144,255,71)
	love.graphics.setColor(255,255,255)
	love.graphics.print(pointscounter,0,0,0,1.5,1.5)
	if pointscounter == 50 then
		love.graphics.setColor(255,255,255)
		love.graphics.print("You Win", 350, 300,0,1.5,1.5)
	end
	love.graphics.setColor(255,0,0)
	love.graphics.rectangle("fill",enemy.x,enemy.y,15,15)
end

function love.update( dt )
	if love.keyboard.isDown("d") and player.x + player.width < 800 then
		player.x = player.x + player.Speed * dt
	end
	if love.keyboard.isDown("a") and player.x > 0 then
		player.x = player.x - player.Speed * dt
	end
	if love.keyboard.isDown("w") and player.y > 0 then
		player.y = player.y - player.Speed * dt
	end
	if love.keyboard.isDown("s") and player.y + player.height < 600 then
		player.y = player.y + player.Speed * dt
	end
	
	dx = (player.x - enemy.x) * (enemy.speed * dt)
	dy = (player.y - enemy.y) * (enemy.speed * dt)
	enemy.x = enemy.x + (dx * dt)
	enemy.y = enemy.y + (dy * dt)
 
[b]here[/b]	for i,v in pairs(player) do 
		if CheckCollision(enemy.x,enemy.y,enemy.width,enemy.height,v.x,v.y,v.width,v.height) then
			print("Got Hit")
		end
	end

	for i,v in pairs(points) do
	if CheckCollision(player.x,player.y,player.width,player.height,v.x,v.y,10,10) then
	pointscounter = pointscounter + 1
	v.delete = true
	local newpoint = {x = math.random(690), y = math.random(490)}
	table.insert(points,newpoint)
	end
	end

	for x = #points,1,-1 do 
		if points[x].delete then
			table.remove(points, x)
		end
	end
end

function love.keypressed ( key )
	if key == "escape" then
	love.event.quit()
	end
end

-- not mine 
function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
  return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end
[code/]

[b]It Keeps givin me the error[/b] [i]attempt to index field "v" (a number value)[i/]
 [b]for some reason[/b]
Last edited by NathanGaming2005 on Fri Jan 11, 2019 10:08 am, edited 1 time in total.
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: i want to make the enemy kill the player but it keeps giving me the error(read below)

Post by grump »

Why are you iterating over player?
CaptainLK
Prole
Posts: 3
Joined: Wed Jan 02, 2019 1:07 pm
Location: Germany

Re: i want to make the enemy kill the player but it keeps giving me the error(read below)

Post by CaptainLK »

What this for loop does is iterating over every variable in player:
A little example of what you are doing:

Code: Select all

player = {x = 200,y = 200,Speed = 400, width = 30, height = 50}
for i, v in pairs(player) do
	print(v)
end
This is what it would output:

Code: Select all

200
200
400
30
50
So if you only have a single object there is no need for a for loop

Code: Select all

function love.load()
player = {x = 200,y = 200,Speed = 400, width = 30, height = 50}
enemy = {x = 150,y = 150, speed = 1000,width = 15,height = 15}
points = {{x = 400,y = 555,width = 10,height = 10}}
pointscounter = 0


end

function love.draw()
	love.graphics.setColor(144,255,71)
	love.graphics.rectangle("fill",player.x,player.y,player.width,player.height)
	love.graphics.setColor(245, 58, 252)
	for i , v in pairs(points) do
		love.graphics.rectangle("fill",v.x,v.y,10,10)
	end
	love.graphics.setColor(144,255,71)
	love.graphics.setColor(255,255,255)
	love.graphics.print(pointscounter,0,0,0,1.5,1.5)
	if pointscounter == 50 then
		love.graphics.setColor(255,255,255)
		love.graphics.print("You Win", 350, 300,0,1.5,1.5)
	end
	love.graphics.setColor(255,0,0)
	love.graphics.rectangle("fill",enemy.x,enemy.y,15,15)
end

function love.update( dt )
	if love.keyboard.isDown("d") and player.x + player.width < 800 then
		player.x = player.x + player.Speed * dt
	end
	if love.keyboard.isDown("a") and player.x > 0 then
		player.x = player.x - player.Speed * dt
	end
	if love.keyboard.isDown("w") and player.y > 0 then
		player.y = player.y - player.Speed * dt
	end
	if love.keyboard.isDown("s") and player.y + player.height < 600 then
		player.y = player.y + player.Speed * dt
	end
	
	dx = (player.x - enemy.x) * (enemy.speed * dt)
	dy = (player.y - enemy.y) * (enemy.speed * dt)
	enemy.x = enemy.x + (dx * dt)
	enemy.y = enemy.y + (dy * dt)

		if CheckCollision(enemy,player) then
			print("Got Hit")
		end
	for i,v in ipairs(points) do
	if CheckCollision(player,v) then
	pointscounter = pointscounter + 1
	v.delete = true
	local newpoint = {x = math.random(690), y = math.random(490)}
	table.insert(points,newpoint)
	end
	end

	for x = #points,1,-1 do 
		if points[x].delete then
			table.remove(points, x)
		end
	end
end

function love.keypressed ( key )
	if key == "escape" then
	love.event.quit()
	end
end

function CheckCollision(a,b)
  if a.x > b.x + b.width or b.x > a.x + a.width then
    return false
  end
  if a.y > b.y + b.height or b.y > a.y + a.height then
    return false
  end
  return true
end
[code/]

Here is the working code, I fixed some other errors and changed your collision detection
Only trying to help
NathanGaming2005
Prole
Posts: 3
Joined: Wed Jan 02, 2019 1:24 pm

Re: i want to make the enemy kill the player but it keeps giving me the error(read below)

Post by NathanGaming2005 »

i got it the new code detects if the enemy touches the player
like

Code: Select all

for I, v in pairs(enemies) do
 if aabb(player.x,player.y,player.width,player.height) then
 player_die()
end



end

function player_die()
game_load()
player.x = 50
player.y = 50
player.speed = 400
end
User avatar
NotARaptor
Citizen
Posts: 59
Joined: Thu Feb 22, 2018 3:15 pm

Re: [Solved]i want to make the enemy kill the player but it keeps giving me the error(read below)

Post by NotARaptor »

I'd probably add a "break" after the player_die() call - if two or more enemies touch the player in the same frame, game_load() will be called that many times, and you probably don't want that.
Post Reply

Who is online

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