Page 1 of 1

Help with Error.

Posted: Sat Jul 07, 2012 11:32 pm
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!

Re: Help with Error.

Posted: Sat Jul 07, 2012 11:35 pm
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)

Re: Help with Error.

Posted: Sat Jul 07, 2012 11:43 pm
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'

Re: Help with Error.

Posted: Sat Jul 07, 2012 11:55 pm
by Nixola
You deleted

Code: Select all

hero = {}

Re: Help with Error.

Posted: Sun Jul 08, 2012 1:23 pm
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.

Re: Help with Error.

Posted: Sun Jul 08, 2012 1:55 pm
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.

Re: Help with Error.

Posted: Sun Jul 08, 2012 2:55 pm
by Pyromaniac
I'm trying to make it so my 'Hero' can move around on the ground, the green.