Page 1 of 1

tutorial question

Posted: Sat Feb 07, 2015 7:05 pm
by Alnaumov
Hi,
on a different topic

getting through the tutorial, and the program returns an error (attempting to perform a numerical operation on player.x). Why is that?

Code: Select all

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

function love.load(arg)
  player = love.graphics.newImage('assets/ship.png')
end
function love.update(dt)
  if love.keyboard.isDown('escape') then
      love.event.push('quit')
    end
  if love.keyboard.isDown('left') then
    player.x = player.x - (player.speed*dt)
  end
  
end
function love.draw(dt)
love.graphics.draw (player, 100,100)
end

Re: tutorial question

Posted: Sat Feb 07, 2015 7:20 pm
by tjohnman
In love.load you are loading your image into the "player" table, which means it no longer has the attributes you defined on the first line.
Edit:
Should be

Code: Select all

player.img = love.graphics.newImage('assets/ship.png')

Re: tutorial question

Posted: Sat Feb 07, 2015 7:20 pm
by s-ol
Alnaumov wrote:Hi,
on a different topic

getting through the tutorial, and the program returns an error (attempting to perform a numerical operation on player.x). Why is that?

Code: Select all

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

function love.load(arg)
  player = love.graphics.newImage('assets/ship.png')
end
function love.update(dt)
  if love.keyboard.isDown('escape') then
      love.event.push('quit')
    end
  if love.keyboard.isDown('left') then
    player.x = player.x - (player.speed*dt)
  end
  
end
function love.draw(dt)
love.graphics.draw (player, 100,100)
end
You are overwriting the player table in love.load:

player = love.graphics.newImage('assets/ship.png')

Instead assign to player.sprite or something like that, then also change your drawing code.

BTW your love.draw always draws to 100,100 - you probably want player.x, player.y instead there.

Re: tutorial question

Posted: Sat Feb 07, 2015 7:22 pm
by tjohnman
I totally ninja'd you ^^

Re: tutorial question

Posted: Sat Feb 07, 2015 8:52 pm
by s-ol
tjohnman wrote:I totally ninja'd you ^^
I was on mobile... :cry: