Page 1 of 1

new and confused : player.img:getWidth()

Posted: Wed Mar 02, 2016 11:43 am
by jpcr
HI everyone,

i'm new to love 2d and tryting to follow the tutorial called : Your First Love2d Game in 200 Lines.

this is my code so far :

Code: Select all

-- http://osmstudios.com/tutorials/your-first-love2d-game-in-200-lines-part-1-of-3

debug = true

player = { x = 200, y = 710, speed = 150, img = nil }

function love.load(arg)
    player.Img = love.graphics.newImage('assets/plane.png')
    --we now have an asset ready to be used inside Love
end

-- Updating
function love.update(dt)
	-- I always start with an easy way to exit the game
	if love.keyboard.isDown('escape') then
		love.event.push('quit')
	end

  if love.keyboard.isDown('left','a') then
    if player.x > 0 then -- binds us to the map
      player.x = player.x - (player.speed*dt)
    end
  elseif love.keyboard.isDown('right','d') then
   
    if player.x < (love.graphics.getWidth() - player.img:getWidth() ) then
      player.x = player.x + (player.speed*dt)
    end
  end

end

function love.draw(dt)
    love.graphics.draw(player.Img, player.x , player.y)
end
the love 2d stops when i press the right key and seems to dislike player.img:getWidth()

i get :
Error: main.lua:25: attempt to index field 'img' (a nil value)
stack traceback:
main.lua:25: in function 'update'
[string "boot.lua"]:461: in function <[string "boot.lua"]:433>
[C]: in function 'xpcall'

any help would be appreciated .
I was wondering it it had something to do with version 0.10 since the tutorial is in a previous build?

thanks
jp

Re: new and confused : player.img:getWidth()

Posted: Wed Mar 02, 2016 11:57 am
by T-Bone
It looks like a typo. In the beginning of love.load, you store the image in "player.Img" (with a capital "I"), not "player.img". Lua is case-sensitive.

Lua's error messages are formulated in a kind of technical way, that might be a bit difficult for beginners to understand. What it means is that it tries to find a field (something stored in a table) called "img" but it doesn't find it (it is nil).

Re: new and confused : player.img:getWidth()

Posted: Wed Mar 02, 2016 3:52 pm
by jpcr
wow, thank you, i had no idea it was case sensitive for variables.

it works now!

jp