This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Tjakka5's profile picture

[Love2D] Attempt To Index Global 'block1' (A Nil Value)

Started by Tjakka5, 22 July 2013 - 08:08 AM
Tjakka5 #1
Posted 22 July 2013 - 10:08 AM
First off, let me say..
Yes, I know there is a Love2d forum, but it aint working for me, so I'd figured I post it here.
Im sorry if this gives you rage :3



So, me being a noob to Love2d I tried to create Pong, and I'm getting attempt to index global 'block1' (a nil value) when Im trying to make a function called block1:up().


function love.load()
love.graphics.setBackgroundColor( 200, 200, 255)
block = love.graphics.newImage("Block.png")
ball = love.graphics.newImage("Pong_Ball.png")


gameStage = "playing"


block1 = {
   pos = 200,
   vel = 0,
   speed = 10,
   }
end

function love.draw()
if gameStage == "playing" then
  love.graphics.draw(block, 1, block1.pos)
end

end
function block1:up(dt)   --This is line 21
self.pos = self.pos + (self.speed * dt)
end

function block1:down(dt)
self.pos = self.pos - (self.speed * dt)
end

function love.update(dt)
if dt > 0.05 then
  dt = 0.05
end

if love.keyboard.isDown("w") then
  block1:up()
end

if love.keyBoard.isDown("s") then
  block1:down()
end
end


If anyone could help me I would greatly appreciate it.
If anyone wants to have the full program (That is the pictures, config, etc.) just say so and I will upload it.
Lyqyd #2
Posted 22 July 2013 - 01:21 PM
Moved to General.
Grim Reaper #3
Posted 22 July 2013 - 01:31 PM
You're declaring block 1 inside of the love.load function. The reference is only valid within the scope of that function because you haven't declared the identifier outside of that function.

You should set it outside of love.load and initialize it inside that function. Although, since its a table without any resources from outside the program (pictures, sounds, etc.), you probably don't have to initialize it inside of love.load :)/>
Tjakka5 #4
Posted 22 July 2013 - 02:33 PM
It worked!
Thanks :)/>