0

I try to do something like this:

require 'middleclass'

Button = class('Button',DisplayObject)

so in order to create some buttons that I want to be display objects...

myButton = Button()

But it does not work. It seems that DisplayObject isn't recognize as a class.

Baz
  • 36,440
  • 11
  • 68
  • 94

1 Answers1

0

DisplayObject is not a class...

It is not even a proper Lua object, DisplayObject is a user type, that might have many meanings, doing what you want to do is very bad idea...

If you want to create a Lua "class" out of a DisplayObject (like, a button), the really correct manner would be:

Mybutton = display.newImage(group, name)
function Mybutton:myCustomFunctionHere()
    --dostuff
end

or if you REALLY want a button constructor...

local function buttonConstructor(groupArg, nameArg, customArg)
    local returnValue = display.newImage(groupArg, nameArg);
    returnValue.someVar = customArg;
    returnValue.anotherVar = customArg;
    return returnValue;
end

middleclass was not made for the sort of stuff you want to do... DisplayObject is really not a class at all... (Even if it looks like one because it has properties and methods)

Don't code Lua with a heavy OOP mind, you can do OOP in Lua? Yes you can, but this is complexifing the simple, it is engineering a light-alloy steel wheel for a carriage, or putting a 3 stage rocket engine in a airplane, it is overengineering, and will not increase performance of anything (runtime, coding time, coding maintenance...) or might even make it worse.

Learn to use Lua as what it is! Very flexible and interesting, with a very wide range of architecture options!

speeder
  • 6,197
  • 5
  • 34
  • 51