Attempt to call field 'player_collide' (a nil value)

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
Wrathguy78
Prole
Posts: 10
Joined: Sun Jul 27, 2014 4:54 pm

Attempt to call field 'player_collide' (a nil value)

Post by Wrathguy78 »

So, it won't call the function even though its their in enemy!
The code in enemy is:

Code: Select all

enemy = {}
enemy.timer = 0
enemy.timerLim = math.random(2,3)
enemy.amount = math.random(1,5)
enemy.side = math.random(1,3)
function enemy.generate(dt)
	enemy.timer = enemy.timer + dt
	if enemy.timer > enemy.timerLim then

	for i=1,enemy.amount do
		if enemy.side == 1 then --left
			enemy.spawn(100,150)
end
		if enemy.side == 2 then --right
			enemy.spawn(3500,1500)
		end
		if enemy.side == 3 then --middle
			enemy.spawn(2000,1500)
	end
		enemy.side = math.random(1,4)
	end
	enemy.amount = math.random(1,5)
	enemy.timerLim = math.random(3,5)
	enemy.timer = 0

end
end



enemyImage = love.graphics.newImage("Enemy.png")
enemy.speed = 1000
enemy.width = 1
enemy.height = 1
enemy.friction = 7.5

function enemy.spawn(x,y)
	table.insert(enemy, {x = x, y = y, xvel = 0, yvel = 0, health = 5,width = enemy.width,height = enemy.height})
end

function enemy.draw()
	for i,v in ipairs(enemy) do
		love.graphics.draw(enemyImage, v.x, v.y, enemy.width,enemy.height)
end
end
function enemy.physics(dt)
	for i,v in ipairs(enemy) do
	v.x = v.x + v.xvel * dt
	v.y = v.y + v.yvel * dt
	v.xvel = v.xvel * (1 - math.min(dt*enemy.friction, 1))
    v.yvel = v.yvel * (1 - math.min(dt*enemy.friction, 1))
	end
end

function enemy.AI(dt)
	for i,v in ipairs(enemy) do
		if player.x + player.width / 2 < v.x + v.width / 2 then
		if v.xvel > -enemy.speed then
				v.xvel = v.xvel - enemy.speed * dt
		end
	end
	if player.x + player.width / 2 > v.x + v.width / 2 then
		if	v.xvel < enemy.speed then
				v.xvel = v.xvel + enemy.speed * dt
				end
			end
end
end
function enemy.bullet_collide()
	for i,v in ipairs(enemy) do
		for ia,va in ipairs(bullet) do
			if va.x + va.width > v.x and
			va.x < v.x + v.width and
			va.y + va.height > v.y and
			va.y < v.y + v.height then
				table.remove(enemy, i)
				table.remove(bullet, ia)
end
end
end
function enemy.player_collide()
	for i,v in ipairs(enemy) do
		for ia, va in ipairs(player) do
			if va.x + va.width > v.x and
			va.x < v.x + v.width and
			va.y + va.height > v.y and
			va.y < v.y + v.height then
				va.health = va.health -25
end
end
end
end
function DRAW_ENEMY()
end
function UPDATE_ENEMY(dt)
end
end
and in player:

Code: Select all

local sdping = love.sound.newSoundData("Bullet.mp3")
function playSound(sd)
  love.audio.newSource(sd, "static"):play()
  end

player = {}
player.health = 100
player.x = 0
player.y = 1500
pic = love.graphics.newImage("flappy.png")
player.xvel = 0
player.y_velocity = 1500
player.width = 1
player.height = 1
player.speed = 300
player.friction = 3.5
jump_height = -300
gravity = -400
health = player.health

background = {}
background2 = love.graphics.newImage("background.png")

floor = {}
floor.x = 0
floor.y = 1580
floorPic = love.graphics.newImage("floor.png")


function player_draw()

	love.graphics.draw(background2, 0, 0)
	love.graphics.draw(pic, player.x, player.y)
	love.graphics.draw(floorPic, floor.x, floor.y)
end

function player_control(dt)

	player.x = player.x + player.xvel

	player.xvel = player.xvel * (1 - math.min(dt*player.friction, 1))

	if love.keyboard.isDown("d") then
		player.x = player.x + (player.speed * dt)
	end
    if love.keyboard.isDown('a') then
		player.x = player.x - (player.speed * dt)
		end
	if player.y_velocity ~= 0 then
        player.y = player.y + player.y_velocity * dt
        player.y_velocity = player.y_velocity - gravity * dt
        if player.y > 1500 then
            player.y_velocity = 1500
            player.y = 1500
        end
    end
end

function love.keypressed(key)
    if key == " " then
        if player.y_velocity == 1500 then
            player.y_velocity = jump_height
        end
    end
end

function player.shoot(key)
	if key == 'up' then
		bullet.spawn(player.x + player.width /2 - bullet.width / 2,player.y - bullet.height,'up')
		end
	if key == 'down' then
		bullet.spawn(player.x + player.width /2 - bullet.width / 2,player.y + bullet.height,'down')
		end
	if key == 'left' then
		bullet.spawn(player.x - bullet.width,player.y + player.height / 2 - bullet.height /2, 'left')
		end
	if key == 'right' then
		bullet.spawn(player.x + player.width,player.y + player.height / 2 - bullet.height /2, 'right')
		end
end
function player.death()
	if player.health < 1 then
		player.x = 0
end
end
If you could help I would appreciate!
Wrathguy78
Prole
Posts: 10
Joined: Sun Jul 27, 2014 4:54 pm

Re: Attempt to call field 'player_collide' (a nil value)

Post by Wrathguy78 »

Also the main:

Code: Select all

require "player"
require "bullet"
require "camera"
require "map"
require "enemy"
function love.load()
end
function love.keypressed(key)
player.shoot(key)
function love.update(dt)

	player.death()
	enemy.player_collide()
	enemy.generate(dt)
	player_control(dt)
	UPDATE_BULLET(dt)
	enemy.AI(dt)
	enemy.physics(dt)
	enemy.bullet_collide()

	if player.x > love.graphics.getWidth() / 2 then
		camera.x = player.x - love.graphics.getWidth() /2
	end
	if player.y > love.graphics.getWidth() / 2 then
		camera.y = player.y - love.graphics.getWidth() /2
	end
	map_collide()
	map_collide2()


function love.keypressed(key)
	player.shoot(key)

function love.draw()
	camera:set()

	player_draw()
	DRAW_BULLET()
	enemy.draw()
	camera:unset()

end
end
end
end
User avatar
Skeiks
Citizen
Posts: 51
Joined: Wed Jan 28, 2015 1:51 pm

Re: Attempt to call field 'player_collide' (a nil value)

Post by Skeiks »

Try calling the function using : instead of .

So Enemy:player_collide()
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Attempt to call field 'player_collide' (a nil value)

Post by Robin »

It would really help if you uploaded a .love. We don't even know on which line the error is.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest