Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

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

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

vonncc123

[GLOBAL: userInfoPane.html]
vonncc123
  • 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!!!")
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


[TOPIC: post.html]
#2

thomas6

[GLOBAL: userInfoPane.html]
thomas6
  • 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.




[topic_controls]
[/topic_controls]