Inherit Methods and Properties from a Class

Good day,

how can I inherit my PlayerClass with properties and functions in it, to my CannonClass.

here is my code for PlayerClass:

-- Player Class local P = {} local private\_int = 1 P.public\_int = 100 P.MyMethod = function() print("DO SOMETHING!!!") end return P

and here is my code to CannonClass:

-- Cannon Class local player = display.newGroup() player.barrel = display.newRect(0,0,10,30) player.barrel.anchorY = 1 player.barrel:setFillColor(1,0,0) player.mainBody = display.newRect(0,0,25,25) player:insert(player.barrel) player:insert(player.mainBody) -- Inheritance happens here, although I don't have an idea how to inherit in LUA -- so that when I create a new instance of cannon, I can use the .MyMethod from PlayerClass -- even though .MyMethod function cannot be found in CannonClass, but on PlayerClass instead

It doesn’t look like inheritance is taking place.

If your cannon class is inherited from the player class, the first thing you do should be saying “the cannon class is in its basic form a player class, with some extended functionality”.

In code that means you start with saying (somewhere)

local cannon = playerClass.new()

And then you add functionality. I see that you’re not using constructor methods. Look at the “.new” function in my examples of yesterday for more info.

It doesn’t look like inheritance is taking place.

If your cannon class is inherited from the player class, the first thing you do should be saying “the cannon class is in its basic form a player class, with some extended functionality”.

In code that means you start with saying (somewhere)

local cannon = playerClass.new()

And then you add functionality. I see that you’re not using constructor methods. Look at the “.new” function in my examples of yesterday for more info.