Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

Inherit Methods and Properties from a Class
Started by vonncc123 Apr 01 2019 10:41 PM

- - - - -
1 reply to this topic
class inheritance
This topic has been archived. This means that you cannot reply to this topic.
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Contributor

  • 236 posts
  • Corona SDK

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!!!")

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.mainBody = display.newRect(0,0,25,25)

-- 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

[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Contributor

  • 980 posts
  • Corona SDK

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.