Page 1 of 1

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

Posted: Thu Aug 11, 2016 8:33 am
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 ...

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

Posted: Thu Aug 11, 2016 9:07 am
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?

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

Posted: Thu Aug 11, 2016 11:14 am
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.

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

Posted: Thu Aug 11, 2016 2:06 pm
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...

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

Posted: Thu Aug 11, 2016 4:04 pm
by 4aiman
You've got a little tiny typo on line #20 of map.lua.
It's "nextPosX", not "nexpPosX". :)

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

Posted: Thu Aug 11, 2016 5:04 pm
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=)

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

Posted: Thu Aug 11, 2016 6:29 pm
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 :)

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

Posted: Thu Aug 11, 2016 10:18 pm
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 :)