Help with Error.

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
Pyromaniac
Prole
Posts: 15
Joined: Sat Jul 07, 2012 2:02 pm

Help with Error.

Post by Pyromaniac »

I was trying to code my testgame when I got an error when trying to make my "Hero" a picture I made.
Here is my code

Code: Select all

function love.load()
hero = {} -- new table for the hero
hero.x = 300
hero.y = 450
hero.speed = 200
end

function love.update(dt)
if love.keyboard.isDown("left") then
  hero.x = hero.x - hero.speed*dt
  elseif love.keyboard.isDown("right") then
  hero.x = hero.x + hero.speed*dt
  end
end

function love.draw()
  -- draws the ground
  love.graphics.setColor(0,255,0,255)
  love.graphics.rectangle("fill",0,465,800,150)
  love.graphics.rectangle("fill", 0,465,800,150)
  
  --draws the hero
  love.graphics.draw("Hero.png", hero.x,hero.y, 30, 15)
end
I get this error:
main.lua23:Incorrect parameter type: expected userdata
Traceback

[C]: in function 'draw'
main.lua23: in function 'draw'
[C]: in function 'xpcall'

Code to fix the problem would be greatly appreciated.
Thanks in Advance!
(Insert funny or inspiring signature here)
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Help with Error.

Post by Nixola »

You can't draw images like this. You first have to load images

Code: Select all

hero.image = love.graphics.newImage("Hero.png") --path to image. It's case sensitive!
then you can draw them

Code: Select all

love.graphics.draw(hero.image, hero.x, hero.y, 30, 15)
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Pyromaniac
Prole
Posts: 15
Joined: Sat Jul 07, 2012 2:02 pm

Re: Help with Error.

Post by Pyromaniac »

I'm new to this so I might be getting stuff wrong. But I (hopefully) put my code in the right spot.

Code: Select all

function love.load()
hero.image = love.graphics.newImage("Hero.png") --path to image. It's case sensitive!
hero.x = 300
hero.y = 450
hero.speed = 200
end
function love.update(dt)
if love.keyboard.isDown("left") then
  hero.x = hero.x - hero.speed*dt
  elseif love.keyboard.isDown("right") then
  hero.x = hero.x + hero.speed*dt
  end
end

function love.draw()
  -- draws the ground
  love.graphics.setColor(0,255,0,255)
  love.graphics.rectangle("fill",0,465,800,150)
  love.graphics.rectangle("fill", 0,465,800,150)
  
  --draws the hero
  love.graphics.draw(hero.image, hero.x, hero.y, 30, 15)
end
But I get this error.
main.lua:2 attempt to index global 'hero' (a nil value)


traceback

main.lua:2: in function ' load'
[C]: in function 'xpcall'
(Insert funny or inspiring signature here)
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Help with Error.

Post by Nixola »

You deleted

Code: Select all

hero = {}
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
Pyromaniac
Prole
Posts: 15
Joined: Sat Jul 07, 2012 2:02 pm

Re: Help with Error.

Post by Pyromaniac »

That fixed it, but I think my image is either too big or i messed something up.
Take a look.

https://www.dropbox.com/s/v603jhtdj6lm5aa/TestGame.love
The giant green thing is the stickman, he's way too large.
(Insert funny or inspiring signature here)
coffee
Party member
Posts: 1206
Joined: Wed Nov 02, 2011 9:07 pm

Re: Help with Error.

Post by coffee »

Pyromaniac wrote:That fixed it, but I think my image is either too big or i messed something up.
Take a look.

https://www.dropbox.com/s/v603jhtdj6lm5aa/TestGame.love
The giant green thing is the stickman, he's way too large.
Yes but only because you asking LOVE to do it that way.

Code: Select all

love.graphics.draw(hero.image, hero.x, hero.y, 30, 15) 

I don't understand what you exactly you want to reach with 30,15. Scale?
Well 30 in that place is rotation/orientation of hero and when you tell 15 in that place you are zooming/enlarging almost 15x times your image in width.

Code: Select all

love.graphics.draw( drawable, x, y, r, sx, sy, ox, oy, kx, ky )
love.graphics.draw( drawable, x, y, orientation, Scale factor (x-axis), Scale factor (y-axis), ... )
If you want to change instead only scale use

Code: Select all

love.graphics.draw(hero.image, hero.x, hero.y,0, 30, 15) 

But don't use 5,10,15 or 30 or anything of that size. 1 is default size. Less than that reduces. More than 1 enlarges. 2 for example duplicate size.
Pyromaniac
Prole
Posts: 15
Joined: Sat Jul 07, 2012 2:02 pm

Re: Help with Error.

Post by Pyromaniac »

I'm trying to make it so my 'Hero' can move around on the ground, the green.
(Insert funny or inspiring signature here)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 2 guests