0 Members and 2 Guests are viewing this topic.
I am a proud cynic.
car = class()function car: init(power, type, lights, etc) self.power = power self.type = type self.lights = lights*2 self.options = etcendfunction car: drive(speed) return self.power * speed -- this doesn't make much sense, but its just an exampleend
volvo = car(10, "2x2", 4, {air-conditioning = True})jeep = car(60, "4x4", 12, {air-conditioning = False, trailer-hook = True})jeep:drive(10) -- returns 600volvo:drive(20) -- returns 200if jeep.options.trailer-hook then print("The jeep can have a trailer")else print("No trailer for you :(")end
--The table we keep all references to created buttons inButtonTable = {}--Create the classbutton = class()--Create and init the buttonfunction button:init(label, x, y, action) self.label = label self.x = x self.y = y self.action = action self.labelWidth = 0 self.width = 0 self.height = 20 table.insert(ButtonTable, self)end--Draw the buttonfunction button:paint(gc) self.labelWidth = gc:getStringWidth(self.label) self.width = self.labelWidth + 4 gc:setColorRGB(0,0,0) gc:setFont("serif", "b", 12) gc:drawRect(self.x, self.y, self.width, self.height) gc:drawString(self.label, self.x+1, self.y+2, "top")end
--The table we keep all references to created buttons inButtonTable = {}--Create the classbutton = class()--Create and init the buttonfunction button:init(label, x, y, action) self.label = label self.x = x self.y = y self.action = action self.labelWidth = 0 self.width = 0 self.height = 20 table.insert(ButtonTable, self)end--Draw the buttonfunction button:paint(gc) self.labelWidth = gc:getStringWidth(self.label) self.width = self.labelWidth + 4 gc:setColorRGB(0,0,0) gc:setFont("serif", "b", 12) gc:drawRect(self.x, self.y, self.width, self.height) gc:drawString(self.label, self.x+1, self.y+2, "top")end--What happens if you click the buttonfunction button:click() self.action()end--Check if clicked, and if clicked call its click methodfunction button:checkClick(x, y) if y >= self.y and y <= self.y + self.height and x >= self.x and x <= self.x + self.width then self:click() endend--Check ButtonTable, and draw all the button in itfunction drawButtons(gc) for _, selectedButton in pairs(ButtonTable) do selectedButton:paint(gc) endend------------------------The on.create even. Here we create a sample button, and make that it will call testFunction when clickedfunction on.create() button1 = button("Test!", 100, 50, testFunction)end--TestFunctionfunction testFunction() bclicked = true platform.window:invalidate()end--The paint eventfunction on.paint(gc) if bclicked then gc:drawString("The button was clicked", 10, 10, "top") end drawButtons(gc)end--Link the click event to the buttonsfunction on.mouseDown(x, y) for _, selectedButton in pairs(ButtonTable) do selectedButton:checkClick(x, y) endend
car = {car1 = {TopSpeed=50,Weight=10},car2 = {TopSpeed=60,Weight=11}...}function PrintTopSpeedgraydraw.print(car.car1.TopSpeed)end
"welcome to the world of computers, where everything seems to be based on random number generators"
Sorry to necro-post, but cant you do this instead of using classes?Code: [Select]car = {car1 = {TopSpeed=50,Weight=10},car2 = {TopSpeed=60,Weight=11}...}function PrintTopSpeedgraydraw.print(car.car1.TopSpeed)end