tutorial question

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
Alnaumov
Prole
Posts: 6
Joined: Mon Feb 02, 2015 6:38 am

tutorial question

Post 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
User avatar
tjohnman
Prole
Posts: 16
Joined: Sat Aug 04, 2012 8:40 am

Re: tutorial question

Post 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')
Last edited by tjohnman on Sat Feb 07, 2015 7:21 pm, edited 1 time in total.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: tutorial question

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
tjohnman
Prole
Posts: 16
Joined: Sat Aug 04, 2012 8:40 am

Re: tutorial question

Post by tjohnman »

I totally ninja'd you ^^
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: tutorial question

Post by s-ol »

tjohnman wrote:I totally ninja'd you ^^
I was on mobile... :cry:

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

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