About the error:attempt to call method 'flip' (a nil value)

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
lovefirstly
Prole
Posts: 12
Joined: Sun Jun 21, 2015 3:27 pm

About the error:attempt to call method 'flip' (a nil value)

Post by lovefirstly »

Hi, I'm a new lover.I learn love by following a tutorial.Below is my code:

Code: Select all


local Quad = love.graphics.newQuad

function love.load()
	character = {}
	character.player = love.graphics.newImage("sprite.png")
	character.x = 50
	character.y = 50
	direction = "right"
	iteration = 1

	max = 8

	idle = true
	timer = 0.1
	quads = {}
	quads['right'] = {}
	quads['left'] = {}
	for j=1, 8 do
		quads['right'][j] = Quad((j-1)*32, 0, 32, 32, 256, 32);
		quads['left'][j] = Quad((j-1)*32, 0, 32, 32, 256, 32);
		quads.left[j]:flip(true, false)
	end
end

function love.update(dt)
	if idle == false then
		timer = timer + dt
		if timer > 0.2 then
			timer = 0.1
			iteration = iteration + 1
			if love.keyboard.isDown('right') then
				sprite.x = sprite.x + 5
			end
			if love.keyboard.isDown('left') then
				sprite.x = sprite.x - 5;
			end
			if iteration > max then
				iteration = 1
			end
		end
	end
end

function love.keypressed(key)
	if quads[key] then
		direction = key
		idle = false
	end
end

function love.keyreleased(key)
	if quads[key] and direction == key then
		idle =true
		iteration = 1
		direction = "right"
	end
end

function love.draw()
	love.graphics.drawq(sprite.player, quads[direction][iteration],
						sprite.x, sprite.y)
end

I run the program, but take a error.I don't know the error why.Anyone can help me? :rofl: :rofl: :rofl:
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: About the error:attempt to call method 'flip' (a nil val

Post by davisdude »

Are you running 0.9.2 or 0.8.0? Because drawq is an 0.8.0 function, and IIRC, quad:flip was also removed in 0.9.0
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
lovefirstly
Prole
Posts: 12
Joined: Sun Jun 21, 2015 3:27 pm

Re: About the error:attempt to call method 'flip' (a nil val

Post by lovefirstly »

davisdude wrote:Are you running 0.9.2 or 0.8.0? Because drawq is an 0.8.0 function, and IIRC, quad:flip was also removed in 0.9.0
Thanks! Minutes ago,I did google and saw the love version log. As you say, it's removed since 0.9.0.The love.graphics.draw can replace the flip(I searched the forum and found some people meet the same problem) :awesome: :awesome: Maybe we followed the same tutorial. :x

Now I have solved the problem.I hope a new tutorial since the love 0.9.0, do you know where to find?
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: About the error:attempt to call method 'flip' (a nil val

Post by Positive07 »

First: This is should have been posted in the Support and Development section, next time you have an issue with your code post there instead (The General sections is more about off topic stuff or issues with LÖVE itself... etc)

Code: Select all


local Quad = love.graphics.newQuad

function love.load()
	character = {}
	character.player = love.graphics.newImage("sprite.png")
	character.x = 50
	character.y = 50
	direction = "right"
	iteration = 1

	max = 8

	idle = true
	timer = 0.1
	quads = {}
	quads['right'] = {}
	quads['left'] = {}
	for j=1, 8 do
		quads['right'][j] = Quad((j-1)*32, 0, 32, 32, 256, 32);
		quads['left'][j] = Quad((j-1)*32, 0, 32, 32, 256, 32);
		--Quad flip is now done in love.graphics.draw (with the scale value)
	end
end

function love.update(dt)
	if idle == false then
		timer = timer + dt
		if timer > 0.2 then
			timer = 0.1
			iteration = iteration + 1
			if love.keyboard.isDown('right') then
				sprite.x = sprite.x + 5
			end
			if love.keyboard.isDown('left') then
				sprite.x = sprite.x - 5;
			end
			if iteration > max then
				iteration = 1
			end
		end
	end
end

function love.keypressed(key)
	if quads[key] then
		direction = key
		idle = false
	end
end

function love.keyreleased(key)
	if quads[key] and direction == key then
		idle =true
		iteration = 1
		direction = "right"
	end
end

function love.draw()
	--love.graphics.drawq is now part of love.graphics.draw
	--since quads cant be flipped I use a negative scale in the X axis (sx) to flip the quad
	--(direction == "right" and 1 or -1) means: if direction == "right" then 1 else -1 end, but returns that value
	love.graphics.draw(sprite.player, quads[direction][iteration],
						sprite.x, sprite.y, 0,(direction == "right" and 1 or -1), 1)
end

Here you have your issues solved to work with 0.9.2, check it out and try to learn what I did from them

PS: With this you could even use a single quads table because the direction only changes the scale value, so instead of using quads[direction][iteration] you could use quads[iteration], just remember to change the loop where you create the quads to this:

Code: Select all

quads = {}
-- quads['right'] = {} --Not needed anymore
-- quads['left'] = {} --Not needed anymore
for j=1, 8 do
	quads[j] = Quad((j-1)*32, 0, 32, 32, 256, 32);
	-- quads.right[j] = Quad((j-1)*32, 0, 32, 32, 256, 32); --Not needed anymore
	-- quads.left[j] = Quad((j-1)*32, 0, 32, 32, 256, 32); --Not needed anymore
	--Quad flip is now done in love.graphics.draw (with the scale value)
end
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
arohacs
Prole
Posts: 1
Joined: Sun May 01, 2016 12:03 am

Re: About the error:attempt to call method 'flip' (a nil value)

Post by arohacs »

Thanks for your solution, Positive07; there were still some issues with the code. The "sprite" and "character" variables are all mixed up in the book and in the solution. It gave me a starting point to find a solution, so I think you for it :awesome:

The author of the packtpub book, in the errata, points to this forum post blindly instead of testing the solution to make sure it works, adjusting the code, and putting it in the errata: https://www.packtpub.com/books/content/support/14413
This makes me angry, but the world is what it is these days. I received the book for reviewing a book on flask, which also was rife with errors. At least I didn't pay for either one.

I used https://github.com/jaz303/presto/blob/m ... _right.png for the sprite and renamed it to "sprite.png" for ease.

The one-liner for the quads doesn't work, so I left it out.

To anyone who bought this book - I suggest you email packtpub and the writer and encourage them both to put this solution in the errata of the book's support page.

Code: Select all

local Quad = love.graphics.newQuad
--sprite = love.graphics.newImage("sprite.png")
function love.load()
  character = {}
  character.player = love.graphics.newImage("sprite.png")
  character.y = 50
  character.x = 50
  direction = "right"
  iteration = 1
  max = 8
  idle = true
  timer = 0.1
  quads = {}
  quads['right'] = {}
  quads['left'] = {}
  for j = 1,8 do
    quads['right'][j] = Quad((j-1)*32, 0, 32, 32, 256, 32);
    quads['left'][j] = Quad((j-1)*32, 0, 32, 32, 256, 32);
  end
end

function love.update(dt)
  if idle == false then
    timer = timer + dt
    if timer > 0.2 then
      timer = 0.1
      iteration = iteration + 1
      if love.keyboard.isDown('right') then
        character.x = character.x + 5
      end
      if love.keyboard.isDown('left') then
        character.x = character.x - 5
      end
      if iteration > max then
        iteration = 1
      end
    end
  end
end

function love.keypressed(key)
  if quads[key] then
    direction = key
    idle = false
  end
end
function love.keyreleased(key)
if quads[key] and direction == key then
    idle = true
    iteration = 1
    direction = "right"
  end
end

function love.draw()
  love.graphics.draw(character.player, quads[direction][iteration], character.x, character.y, 0,(direction == "right" and 1 or -1), 1)
end
Post Reply

Who is online

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