Hi i found this code here On this page, and iam New in Programming. I Test this code in codea of my ipad 3.
The Error is: error: [string "Button = class() … 24: attempt to Index Field ‘Position’ a Nil Value
Here is this code: File 1: MAIN
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
button = Button("Planet Cute:Chest closed","PlanetCute:Cest Open",WIDTH/2, HEIGHT/2 )
button.Action = function() buttonPressed() end
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
Button:draw()
end
function ButtonPressed()
print"Button pressed"
end
function touched(touch)
Button:touched(touch)
end
File 2: Button
Button = class()
function Button:init(btnNormal,btnSelect,x,y)
self.Position = vec2(x,y)
self.btnNormal = btnNormal
self.btnSelect = btnSelect
self.Selceted = false
self.Action = nil
self.Width,self.Height = spriteSize(self.btnNormal)
end
function Button:draw()
if self.Selceted == true then
sprite(self.btnSelect,self.Position.x,self.Position.y,self.Width,self.Height)
elseif self.Selcetet == false then
sprite(self.btnNormal,self.Position.x,self.Position.y,self.Width,self.Height)
end
end
function Button:touched(touch)
if touch.state == BEGAN or touch.state == MOVING then
if math.abs(self.Position.x - touch.x) <= (self.Width/2)
and math.abs(self.Position.y - touch.y) <= (self.Hight/2) then
self.Selected = true
else
self.Selceted = false
end
elseif touch.state == ENDED then
if self.Selected == true and self.Action then
self.Action()
end
self.Selected = false
end
end
He Can Not Show my Button , while Not the correct Position, but when i give x and y 200 and 200 it is also Nil.
I Hope One Guy Can me help what is my misstake.
Thank you.
@Striker_der_Echte For starters, you should put 3 ~ before and after your code so it formats correctly. Then you need to change Button:draw() to button:draw() and Button:touched(touch) to button:touched(touched).
Some of the words are not spelled right, but I just made them the same spelling. Here is a working version.
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
button = Button("Planet Cute:Chest Closed","Planet Cute:Chest Open",
WIDTH/2, HEIGHT/2 ) button.Action = function() ButtonPressed() end
end
-- This function gets called once every frame
function draw() -- This sets a dark background color
background(40, 40, 50)
button:draw()
end
function ButtonPressed()
print"Button pressed"
end
function touched(touch)
button:touched(touch)
end
Button = class()
function Button:init(btnNormal,btnSelect,x,y)
self.Position = vec2(x,y)
self.btnNormal = btnNormal
self.btnSelect = btnSelect
self.Selceted = false
self.Action = nil
self.Width,self.Height = spriteSize(self.btnNormal)
end
function Button:draw()
if self.Selceted == true then
sprite(self.btnSelect,self.Position.x,self.Position.y,self.Width,self.Height)
elseif self.Selceted == false then
sprite(self.btnNormal,self.Position.x,self.Position.y,self.Width,self.Height)
end
end
function Button:touched(touch)
if touch.state == BEGAN or touch.state == MOVING then
if math.abs(self.Position.x - touch.x) <= (self.Width/2) and
math.abs(self.Position.y - touch.y) <= (self.Height/2) then
self.Selceted = true
else
self.Selceted = false
end
elseif touch.state == ENDED then
if self.Selceted == true and self.Action then
self.Action() end self.Selected = false
end
end
Thank You Dave1707 i have found 5 error in my Code. And i Think i have Zero :-S
But 1. Question?
But i dont why must it the Class (Big B) Button:draw() and in the Main (small b) button:draw()
The Same is like Button: touched(touch) in the Class Funktion is it Name (Big B). And in the Main when it Recall my Class funktion it must Small b button:touched(touch) ???
This is Not logical for me.
:((
And Very Big Thank you 4 the RIGHT CODE.
The class is named Button, so all functions for that class must be defined using Button. In Main, you are creating an instance of the class named button, so each function for that instance must be called using button. If your instance of the class was created as myButton in Main, then you would call myButton:draw() to draw it and myButton:touched(touch) to handle it being touched.
@Striker_der_Echte Button is the class name. When you want to get an instance of that class, you assign it to a variable. That’s what’s done in setup() with the line button=Button(…). The variable “button” now refers to the Button code. So to call the Button draw() function you use button:draw(). Same goes for the Button touched() function. You use button:touched(). You could also use xyz=Button(…) . Then you would use xyz:draw() and xyz:touched(). The code used by “button” and “xyz” are different instances of the Button code and would work as seperate Buttons. You could use the Button code for as many buttons as you want to draw. You would just assign different locations for each button when you assign it to a different variable.