bad argument #2 to 'draw' in function love.draw()

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
McFree
Prole
Posts: 12
Joined: Thu Aug 11, 2016 7:31 am

bad argument #2 to 'draw' in function love.draw()

Post by McFree »

Greetings to all, I had the problem, understanding of what such can not. before the looming image to fit the window, and I could easily move it in the window as soon as I added a check for transparency of the image the image immediately start to produce an error when drawing
Error: map.lua:9: bad argument #2 to 'draw' (Quad expected, got nil)
main.lua

Code: Select all

function love.load()
	lg = love.graphics;
	lm = love.mouse;
	lk = love.keyboard;

	-- requaire
	require "load"
  	require "menu"
  	require "mouseactions"
  	require "keyboardactions"
  	require "player"
  	require "game"
  	require "settings"
  	require "map"
	-- loading
	loadFonts();
	loadImages();
	loadSettings();
	-- initKeys --
  	keyPressed = {};
	-- inicialization
	initMenu();
	initMouse();
	initGame();
  	initPlayer();
  	initMap();

	showMenu();

	updateTime = 0;
end

function love.draw()
	if mapShow then
		drawMap(); 
	end
	if playerSpawned then
		drawPlayer();
		moveKeyPressed();
	end
	mouseSwitched = false;
  	mouseScrollUp = false;
  	mouseScrollDown = false;
end
game.lua
(Which is called when selecting a new game in main menu)

Code: Select all

function startGame()
	playerSpawned = true;
	mapShow = true;
end
map.lua

Code: Select all

function initMap()
  mapShow = false
  mapPosX = 140
  mapPosY = 120
  colorChanged = false;
end

function drawMap()
	lg.draw(gameMap, mapPosX, mapPosY); -- This line produces an error
	lg.draw(gameMapView, mapPosX, mapPosY);  -- And there
end

function moveMap(X,Y)
	nextPosX = mapPosX+X; -- There can receive global variable
 	nextPosY = mapPosY+Y; -- And there
	dx = 0;
	dy = 0;
	r, g, b, a = gameMapData:getPixel(windowWidth/2-nextPosX+dx,windowHeight/2-nextPosY+dy);
	if a >= 50 then
		mapPosX = nexpPosX;
		mapPosY = nextPosY;
	else 
		r, g, b, a = gameMapData:getPixel(windowWidth/2-nextPosX+dx,windowHeight/2-mapPosY+dy)
	   	if a >= 50 then
	    	mapPosX = nextPosX;
	    end
	    r, g, b, a = gameMapData:getPixel(windowWidth/2-mapPosX+dx,windowHeight/2-nextPosY+dy)
	    if a >= 50 then
	    	mapPosY = nextPosY;
	    end
	    if a < 50 then
	    	mapPosX = -100
  			mapPosY = -100
  		end
  	end
end
help me please ...
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: bad argument #2 to 'draw' in function love.draw()

Post by Plu »

What is the value of gameMap? It seems to be a Texture, which means that your mapPosX should be a Quad, but that does not seem to be what you think you're getting.

The definition of gameMap is not visible anywhere, can you post that?
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: bad argument #2 to 'draw' in function love.draw()

Post by 4aiman »

Like Plu just said, your gameMap is nil.
You *do* invoke the initMap() but it doesn't set gameMap to a value of any drawable type.

It is also unclear where you start your game.
There's a function "startGame" that seems be the only one to be able to set mapShow variable to true.
But I can't see a place it is being called.
So, maybe that piece of your code is hiding the answer.
McFree
Prole
Posts: 12
Joined: Thu Aug 11, 2016 7:31 am

Re: bad argument #2 to 'draw' in function love.draw()

Post by McFree »

I attach a file to the topic .love Link

Drawing on the image problem is solved, now the problem in arithmetic operations to global variables
Error: map.lua:14: attempt to perform arithmetic on global 'mapPosX' (a nil value)
PS I'm sorry that I have made a second subject. I had problems with the internet and it does not show...
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: bad argument #2 to 'draw' in function love.draw()

Post by 4aiman »

You've got a little tiny typo on line #20 of map.lua.
It's "nextPosX", not "nexpPosX". :)
McFree
Prole
Posts: 12
Joined: Thu Aug 11, 2016 7:31 am

Re: bad argument #2 to 'draw' in function love.draw()

Post by McFree »

4aiman wrote:You've got a little tiny typo on line #20 of map.lua.
It's "nextPosX", not "nexpPosX". :)
Thanks again for your reply to my topic =)

I am very grateful to the Russian communities so fast when there is little support=)
User avatar
4aiman
Party member
Posts: 262
Joined: Sat Jan 16, 2016 10:30 am

Re: bad argument #2 to 'draw' in function love.draw()

Post by 4aiman »

Feel free to ask me via PM if you need to.
I'm far from being the best love2d user, but I'll try to help.
Especially since you'll be able to ask in Russian :)
McFree
Prole
Posts: 12
Joined: Thu Aug 11, 2016 7:31 am

Re: bad argument #2 to 'draw' in function love.draw()

Post by McFree »

4aiman wrote:Feel free to ask me via PM if you need to.
I'm far from being the best love2d user, but I'll try to help.
Especially since you'll be able to ask in Russian :)
Thank you, I'll remember you, if I need help turn to you :)
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 1 guest