strange nil value error

Hey guys, I’m working on my first game, and after 700 lines of code I decided to rewrite the whole thing, because mechanics are so chaotic written, it get’s just worse and worse to work with.

So I started from the basics, just to get fundamentals of my game to work, and then I’ll do everything else.

But after 50 lines it just won’t work as before and I’m crushed.

I searched the forums and web which gave me some sense of insight on this, but I can’t fix it anyway.

local cW, cH, polW, polH = display.contentWidth, display.contentHeight, display.contentWidth\*0.5, display.contentHeight\*0.5 local ball2, ball3, ball4, ball5 local ball = display.newImage("images/bigball.png") ball.x = polW ball.y = polH function ball:touch (event) print('big ball touched') display.remove(ball) ball2 = display.newImage("images/bigball.png") ball2.x = ball.x - 200 ball2.y = ball.y ball2.xScale = 0.5 ball2.yScale = 0.5 ball3 = display.newImage("images/bigball.png") ball3.x = ball.x + 200 ball3.y = ball.y ball3.xScale = 0.5 ball3.yScale = 0.5 end ball:addEventListener( "touch", ball ); function ball2:touch (event) print('left ball touched') display.remove(ball2) ball4 = display.newImage("images/bigball.png") ball4.x = ball2.x - 100 ball4.y = ball2.y ball4.xScale = 0.3 ball4.yScale = 0.3 ball5 = display.newImage("images/bigball.png") ball5.x = ball2.x + 100 ball5.y = ball2.y ball5.xScale = 0.3 ball5.yScale = 0.3 end ball2:addEventListener( "touch", ball2 );

First function works properly, but if I add second function, the code gets broken.

The first thing is, the second function won’t highlight in sublime text, and I don’t know why, because I wrote it exactly as the first one, but I don’t know if that’s the problem.

The second thing, if I wanna run it, it gives me Runtime Error in line 31: attempt to index local ball2 (a nil value).

But I declared this on the top of the code. I don’t get it. I tried to juggle with local/global on every piece but it won’t fix.

Please, please give me some feedback! I’m aware that’s propably supersimple to fix, but my head ache, I overthought this problem.

Cheers

local cW, cH, polW, polH = display.contentWidth, display.contentHeight, display.contentWidth\*0.5, display.contentHeight\*0.5 local ball2, ball3, ball4, ball5 local ball = display.newImage("images/bigball.png") ball.x = polW ball.y = polH function ball:touch (event)    print('big ball touched') display.remove(ball) ball2 = display.newImage("images/bigball.png") ball2.x = ball.x - 100 ball2.y = ball.y ball2.xScale = 0.5 ball2.yScale = 0.5 ball3 = display.newImage("images/bigball.png") ball3.x = ball.x + 200 ball3.y = ball.y ball3.xScale = 0.5 ball3.yScale = 0.5 ball2:addEventListener( "touch", ball2function ); end ball:addEventListener( "touch", ball ); function ball2function(event)          print('left ball touched') display.remove(ball2) ball4 = display.newImage("images/bigball.png") ball4.x = ball2.x - 100 ball4.y = ball2.y ball4.xScale = 0.3 ball4.yScale = 0.3 ball5 = display.newImage("images/bigball.png") ball5.x = ball2.x + 100 ball5.y = ball2.y ball5.xScale = 0.3 ball5.yScale = 0.3 end

I tested this and this worked.

Jay

http://jayscratchpad.wordpress.com/

muchos gracias, per favore, I get it now… so simple argh

local cW, cH, polW, polH = display.contentWidth, display.contentHeight, display.contentWidth\*0.5, display.contentHeight\*0.5 local ball2, ball3, ball4, ball5 local ball = display.newImage("images/bigball.png") ball.x = polW ball.y = polH function ball:touch (event)    print('big ball touched') display.remove(ball) ball2 = display.newImage("images/bigball.png") ball2.x = ball.x - 100 ball2.y = ball.y ball2.xScale = 0.5 ball2.yScale = 0.5 ball3 = display.newImage("images/bigball.png") ball3.x = ball.x + 200 ball3.y = ball.y ball3.xScale = 0.5 ball3.yScale = 0.5 ball2:addEventListener( "touch", ball2function ); end ball:addEventListener( "touch", ball ); function ball2function(event)          print('left ball touched') display.remove(ball2) ball4 = display.newImage("images/bigball.png") ball4.x = ball2.x - 100 ball4.y = ball2.y ball4.xScale = 0.3 ball4.yScale = 0.3 ball5 = display.newImage("images/bigball.png") ball5.x = ball2.x + 100 ball5.y = ball2.y ball5.xScale = 0.3 ball5.yScale = 0.3 end

I tested this and this worked.

Jay

http://jayscratchpad.wordpress.com/

muchos gracias, per favore, I get it now… so simple argh