help with "expected userdata"

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
User avatar
BEANSFTW
Prole
Posts: 28
Joined: Sun Apr 15, 2012 2:58 pm

help with "expected userdata"

Post by BEANSFTW » Sun Apr 15, 2012 3:24 pm

Hi. I'm new to programing, love2d and lua. I have a problem with:
------------------------------------------------------------------------------
Error

main.lua:89: Incorrect parameter type: expected userdata.

Traceback

[C]: in function 'draw'
main.lua:89: in function 'draw'
[C]: in function 'xpcall'
-----------------------------------------------------------------------------------
Whats userdata? Help would be great :crazy:

here's my code:

main.lua:

Code: Select all

require "player"

function love.load()

	
  
	player = love.graphics.newImage("assets/player.png")
 
	playerx = 270
  
	playery = 420
  
  
  
	-- music
	music = love.audio.newSource( "assets/music.ogg" , "steam")
	music:setLooping(true)
	love.audio.play(music)
  
	Cloudx = 0
  
    yCloud = 128
  
	cloudimage = ("assets/cloud.gif")
	bullet = ("assets/bullet.gif")
  
	
end


function love.update(dt)
 


	Cloudx =  Cloudx + 32*dt 
	if Cloudx >= (900 + 278) then
		Cloudx = 0
	end

	
	
	if love.keyboard.isDown("d") then
		playerx = playerx + 275*dt
		player = love.graphics.newImage("assets/player.png")
	end	

	if love.keyboard.isDown("s") then
		playery = playery + 675*dt
	end
	
	
	
	
	if love.keyboard.isDown("a") then
		player = love.graphics.newImage("assets/playerback.png")
		playerx = playerx - 275*dt
	end	
	
	
	if playery > 420 then
		playery = 420
	end	
	
	
	
	
end

function love.draw()

	
	
	
  love.graphics.setColor(40,255,40)
  love.graphics.rectangle("fill",
    0,0,love.graphics.getWidth(),love.graphics.getHeight())
  love.graphics.setColor(255,255,255)
  
  
  
	love.graphics.setColor(111,183,255)
	love.graphics.rectangle("fill",0,0,love.graphics.getWidth(),450)
	love.graphics.setColor(255,255,255)
	
	
	
	love.graphics.draw(cloudimage, Cloudx - 278, 128,278)
	
	
	love.graphics.draw(player, playerx, playery)
end
player.lua:

Code: Select all

playerxy = {}
    x = 0
	y = 0

Giggzaw
Prole
Posts: 1
Joined: Fri Apr 13, 2012 8:29 pm

Re: help with "expected userdata"

Post by Giggzaw » Sun Apr 15, 2012 4:51 pm

Hi!

I only started yesterday with lua and love so i could be wrong.
I think the mistake is that you gave it a string instead of an image at line 89 (when you try drawing the cloud).

Probably you should change the cloudimage variable to:

Code: Select all

cloudimage = love.graphics.newImage("assets/cloud.gif")
As I said anyway, I could be wrong :D

User avatar
Nixola
Inner party member
Posts: 1942
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: help with "expected userdata"

Post by Nixola » Sun Apr 15, 2012 5:12 pm

Giggzaw, you're right. Beans, you should do the same with 'bullet'
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics

User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: help with "expected userdata"

Post by tentus » Sun Apr 15, 2012 7:58 pm

By the way, if you ever notice love.graphics.newImage inside of love.update, you've made a mistake (a very bad one). If I hold down D in your game, my computer has to open a file, read it, process it, and assign it to a variable, EVERY. SINGLE. FRAME.

The proper way to do it is to pre-load all your images in love.load, and then point the drawing variable at the proloaded images. A short example:

Code: Select all

function love.load()
   left = love.graphics.newImage("assets/left.png")
   right = love.graphics.newImage("assets/right.png")
   player = left
end
function love.update(dt)
   if love.keyboard.isDown("a") then
      player = left
   end
   if love.keyboard.isDown("d") then
      player = right
   end
end
function love.draw()
   love.graphics.draw(player, 5, 5)
end
Kurosuke needs beta testers

User avatar
BEANSFTW
Prole
Posts: 28
Joined: Sun Apr 15, 2012 2:58 pm

Re: help with "expected userdata"

Post by BEANSFTW » Sun Apr 15, 2012 8:48 pm

THANKS!

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 33 guests