Hi, just wanted to inform you with what I think is a bug. For some reason, whenever I am creating a new class, in the draw function I put an ellipse in. In the setup, I setup the classes global values for X, y and size. For some reason, when I add these values in the ellipse, (self.x, self.y, self.size) I then add a draw function in the main class, and setup "name:draw(), I then run it, and it comes up with a syntax error stating “number expected got nil” even though I have already added in self.x, self.y and self.size,and they have been set up as variables with numbers in the setup, it still has the error. The only way to fix it is to delete the code with self.x, self.y, self.size, and add self.x, run it, add self.y, run it, then add self.size and run it. Is this a bug? Or am I doing something wrong? Sorry if I didn’t explain it clearly, I’m really tired.
It would help us understand better if you posted the code.
Yes, you need to post the code. Chances are very high that it is an error in your code.
@LL_Phoenix456 Since you didn’t post any code, here’s a simple example of what it sounds like you’re doing. The first ellipse e1 is defined in one statement. The second ellipse e2 is defined using 3 statements. How does this compare to what you’re trying to do that’s giving you the error.
function setup()
e1=myClass(100,200,100,50)
e2=myClass(200,400)
e2.sizex=150
e2.sizey=25
end
function draw()
background(0)
e1:draw()
e2:draw()
end
myClass=class()
function myClass:init(x,y,sx,sy)
self.x=x
self.y=y
self.sizex=sx
self.sizey=sy
end
function myClass:draw()
fill(255)
ellipse(self.x,self.y,self.sizex,self.sizey)
end
@dave1707 Nope. It’s different, I’ll grab the code. Sorry I forgot to get some.
Circle = class()
function Circle:init(x, y, size)
self.x = x
self.y = y
self.size = size or 100
self.origState = {x = self.x, y = self.y, size = self.size}
end
function Circle:reset()
self.x = self.origState.x
self.y = self.origState.y
self.size = self.origState.size
end
function Circle:draw()
pushStyle()
fill(0, 16, 255, 255)
stroke(255, 239, 0, 255)
strokeWidth(5)
ellipse(self.x, self.y, self.size) -- Right here. I think its working now though.
popStyle()
end
If you add this in a class, and in the main class add in “MyCircle:draw()” it won’t work. But now that ive “fixed” it in the way I did before, it might work. It only ever works if I am starting a new project. And I add the ellipse part, then the draw part and boom. Syntax error. That’s the thing, there is no syntax error.
Well, I haven’t put your code in a new project to test it out, but it looks like it would work to me. Does anyone else see anything that would cause an error?
@LL_Phoenix456 Unless you post all the code that doesn’t work, we can only guess at what you’re trying to do. Below is your code plus I added setup and draw that I think you’re doing. The below code works OK for me.
EDIT: If I take your class, this is how I would use it. How are you using it that’s causing errors.
function setup()
MyCircle=Circle(100,200,50)
end
function draw()
background(0)
MyCircle:draw()
end
Circle = class()
function Circle:init(x, y, size)
self.x = x
self.y = y
self.size = size or 100
self.origState = {x = self.x, y = self.y, size = self.size}
end
function Circle:reset()
self.x = self.origState.x
self.y = self.origState.y
self.size = self.origState.size
end
function Circle:draw()
pushStyle()
fill(0, 16, 255, 255)
stroke(255, 239, 0, 255)
strokeWidth(5)
ellipse(self.x, self.y, self.size) -- Right here. I think its working now though.
popStyle()
end
I’m pretty sure it’s Becuase when I added the ellipse, I was using self.size as a variable. When Codea read the ellipse statement, there was a syntax error Becuase it read self.size. Self.size didn’t have a number. So I added “size or 100” and it fixed. It’s weird Becuase on other projects it didn’t do that. Anyway I think it’s fixed
Okay, good. Thanks for asking!