Modular Classes with Storyboard API?

Wasn’t too sure whether to ask this in Game Development section or the General Corona section, so I’ll post it here >.<

So I was reading through Jonathan Beebe’s tutorial on modular classes; essentially, I want a game asset that is used to bounce “repel”/ bounce another object. Reason for using this method is to generally make the code more streamline; rather than manually writing a Bouncer object for every level as well as it’s functionality, I should be able to define what it is and what it can do in a seperate file, and just call that instead.

Bumper class:

local bumper = { } -- group  
local bumper\_mt = { \_\_index = bumper} --metadata, required to get classes to work  
-------------------------------------------------------  
-- PRIVATE FUNCTIONS --  
-------------------------------------------------------  
  
  
-------------------------------------------------------  
-- PUBLIC FUNCTIONS --  
-------------------------------------------------------  
  
-- Constructor  
function bumper.new(xPos, yPos)  
 print("gg no re")  
  
 local bumperObj = display.newRect(0,0, 300,300)  
 bumperObj:setReferencePoint(display.CenterReferencePoint)  
 bumperObj.x = xPos; bumperObj.y = yPos;  
 bumperObj:setFillColor(255,0,0)  
  
 return setmetatable(bumperObj, bumper\_mt )  
end  
return bumper  
  

(NOTE: the physics part isn’t done yet)
Now I can spawn this instance on to screen, it’s just that I want to be able to remove it as the scene changes. But as soon as I put this instance into a group, it doesn’t appear on screen even though the function for creating the instance is being called.

Storyboard bit:

local bumper1  
  
function scene:createScene( event )  
 local group = self.view  
  
 gamebase.startGame(); -- creates scene  
  
 bumper1 = bumperClass.new( (\_W/2), (\_H/2) ) -- x position, y position  
 group:insert(bumper1);  
  
end  

I have a feeling that this Modular Classes method doesn’t work very well with Storyboard, but I could be wrong >.<

Thank you [import]uid: 184921 topic_id: 36236 reply_id: 336236[/import]

where you have

group:insert(bumper1) … bumper1 is a table

I think you need to have

group:insert(bumper1.bumperObj) … bumper1.bumperObj is a display object, a member of the bumper1 table.

a displayGroup needs a display object, not a table. regardless wether using storyboard or not.
I do not have a strong understanding of the meta tables, so there might be something else wrong or something else missing, but regardless, I am fairly certain displayGroups … and I think ‘self.view’ of the storyboard api is a displayGroup, it wants the bumper1.bumperObj to display.

Hope this helps
[import]uid: 148857 topic_id: 36236 reply_id: 143968[/import]

Let me clarify some of what I mentioned above, a little.

try this code (not using storyboard) … but I think it will work the same in storyboard

– for your class ( I assume that is a separate module/file

-- bumper class  
  
local bumper = { } -- group  
local bumper\_mt = { \_\_index = bumper} --metadata, required to get classes to work  
   
   
-------------------------------------------------------  
-- PRIVATE FUNCTIONS --  
-------------------------------------------------------  
   
   
   
   
-------------------------------------------------------  
-- PUBLIC FUNCTIONS --  
-------------------------------------------------------  
   
-- Constructor  
function bumper.new(xPos, yPos)  
 print("gg no re")  
 local tbl = {}  
 tbl.bumperObj = display.newRect(0,0, 300,300)  
 tbl.bumperObj:setReferencePoint(display.CenterReferencePoint)  
 tbl.bumperObj.x = xPos; tbl.bumperObj.y = yPos;  
 tbl.bumperObj:setFillColor(255,0,0)  
  
 tbl.word = "HELLO"  
 return setmetatable(tbl, bumper\_mt )  
end  
   
   
return bumper  

Then your main

  
local \_W = display.contentWidth  
local \_H = display.contentHeight  
  
local bumperClass = require("bumper")  
  
local bumper1  
local group = display.newGroup()  
  
bumper1 = bumperClass.new( (\_W/2), (\_H/2) )  
group:insert(bumper1.bumperObj);  
  
print( bumper1.word)  
  
  

[import]uid: 148857 topic_id: 36236 reply_id: 143971[/import]

I did what you did, it isn’t displaying the square and it’s not showing any errors but it is displaying the word

edit: actually, it does work, I just forgot to add the y-coordinate in the table.
[import]uid: 184921 topic_id: 36236 reply_id: 144126[/import]

where you have

group:insert(bumper1) … bumper1 is a table

I think you need to have

group:insert(bumper1.bumperObj) … bumper1.bumperObj is a display object, a member of the bumper1 table.

a displayGroup needs a display object, not a table. regardless wether using storyboard or not.
I do not have a strong understanding of the meta tables, so there might be something else wrong or something else missing, but regardless, I am fairly certain displayGroups … and I think ‘self.view’ of the storyboard api is a displayGroup, it wants the bumper1.bumperObj to display.

Hope this helps
[import]uid: 148857 topic_id: 36236 reply_id: 143968[/import]

Let me clarify some of what I mentioned above, a little.

try this code (not using storyboard) … but I think it will work the same in storyboard

– for your class ( I assume that is a separate module/file

-- bumper class  
  
local bumper = { } -- group  
local bumper\_mt = { \_\_index = bumper} --metadata, required to get classes to work  
   
   
-------------------------------------------------------  
-- PRIVATE FUNCTIONS --  
-------------------------------------------------------  
   
   
   
   
-------------------------------------------------------  
-- PUBLIC FUNCTIONS --  
-------------------------------------------------------  
   
-- Constructor  
function bumper.new(xPos, yPos)  
 print("gg no re")  
 local tbl = {}  
 tbl.bumperObj = display.newRect(0,0, 300,300)  
 tbl.bumperObj:setReferencePoint(display.CenterReferencePoint)  
 tbl.bumperObj.x = xPos; tbl.bumperObj.y = yPos;  
 tbl.bumperObj:setFillColor(255,0,0)  
  
 tbl.word = "HELLO"  
 return setmetatable(tbl, bumper\_mt )  
end  
   
   
return bumper  

Then your main

  
local \_W = display.contentWidth  
local \_H = display.contentHeight  
  
local bumperClass = require("bumper")  
  
local bumper1  
local group = display.newGroup()  
  
bumper1 = bumperClass.new( (\_W/2), (\_H/2) )  
group:insert(bumper1.bumperObj);  
  
print( bumper1.word)  
  
  

[import]uid: 148857 topic_id: 36236 reply_id: 143971[/import]

I did what you did, it isn’t displaying the square and it’s not showing any errors but it is displaying the word

edit: actually, it does work, I just forgot to add the y-coordinate in the table.
[import]uid: 184921 topic_id: 36236 reply_id: 144126[/import]

where you have

group:insert(bumper1) … bumper1 is a table

I think you need to have

group:insert(bumper1.bumperObj) … bumper1.bumperObj is a display object, a member of the bumper1 table.

a displayGroup needs a display object, not a table. regardless wether using storyboard or not.
I do not have a strong understanding of the meta tables, so there might be something else wrong or something else missing, but regardless, I am fairly certain displayGroups … and I think ‘self.view’ of the storyboard api is a displayGroup, it wants the bumper1.bumperObj to display.

Hope this helps
[import]uid: 148857 topic_id: 36236 reply_id: 143968[/import]

Let me clarify some of what I mentioned above, a little.

try this code (not using storyboard) … but I think it will work the same in storyboard

– for your class ( I assume that is a separate module/file

-- bumper class  
  
local bumper = { } -- group  
local bumper\_mt = { \_\_index = bumper} --metadata, required to get classes to work  
   
   
-------------------------------------------------------  
-- PRIVATE FUNCTIONS --  
-------------------------------------------------------  
   
   
   
   
-------------------------------------------------------  
-- PUBLIC FUNCTIONS --  
-------------------------------------------------------  
   
-- Constructor  
function bumper.new(xPos, yPos)  
 print("gg no re")  
 local tbl = {}  
 tbl.bumperObj = display.newRect(0,0, 300,300)  
 tbl.bumperObj:setReferencePoint(display.CenterReferencePoint)  
 tbl.bumperObj.x = xPos; tbl.bumperObj.y = yPos;  
 tbl.bumperObj:setFillColor(255,0,0)  
  
 tbl.word = "HELLO"  
 return setmetatable(tbl, bumper\_mt )  
end  
   
   
return bumper  

Then your main

  
local \_W = display.contentWidth  
local \_H = display.contentHeight  
  
local bumperClass = require("bumper")  
  
local bumper1  
local group = display.newGroup()  
  
bumper1 = bumperClass.new( (\_W/2), (\_H/2) )  
group:insert(bumper1.bumperObj);  
  
print( bumper1.word)  
  
  

[import]uid: 148857 topic_id: 36236 reply_id: 143971[/import]

I did what you did, it isn’t displaying the square and it’s not showing any errors but it is displaying the word

edit: actually, it does work, I just forgot to add the y-coordinate in the table.
[import]uid: 184921 topic_id: 36236 reply_id: 144126[/import]

where you have

group:insert(bumper1) … bumper1 is a table

I think you need to have

group:insert(bumper1.bumperObj) … bumper1.bumperObj is a display object, a member of the bumper1 table.

a displayGroup needs a display object, not a table. regardless wether using storyboard or not.
I do not have a strong understanding of the meta tables, so there might be something else wrong or something else missing, but regardless, I am fairly certain displayGroups … and I think ‘self.view’ of the storyboard api is a displayGroup, it wants the bumper1.bumperObj to display.

Hope this helps
[import]uid: 148857 topic_id: 36236 reply_id: 143968[/import]

Let me clarify some of what I mentioned above, a little.

try this code (not using storyboard) … but I think it will work the same in storyboard

– for your class ( I assume that is a separate module/file

-- bumper class  
  
local bumper = { } -- group  
local bumper\_mt = { \_\_index = bumper} --metadata, required to get classes to work  
   
   
-------------------------------------------------------  
-- PRIVATE FUNCTIONS --  
-------------------------------------------------------  
   
   
   
   
-------------------------------------------------------  
-- PUBLIC FUNCTIONS --  
-------------------------------------------------------  
   
-- Constructor  
function bumper.new(xPos, yPos)  
 print("gg no re")  
 local tbl = {}  
 tbl.bumperObj = display.newRect(0,0, 300,300)  
 tbl.bumperObj:setReferencePoint(display.CenterReferencePoint)  
 tbl.bumperObj.x = xPos; tbl.bumperObj.y = yPos;  
 tbl.bumperObj:setFillColor(255,0,0)  
  
 tbl.word = "HELLO"  
 return setmetatable(tbl, bumper\_mt )  
end  
   
   
return bumper  

Then your main

  
local \_W = display.contentWidth  
local \_H = display.contentHeight  
  
local bumperClass = require("bumper")  
  
local bumper1  
local group = display.newGroup()  
  
bumper1 = bumperClass.new( (\_W/2), (\_H/2) )  
group:insert(bumper1.bumperObj);  
  
print( bumper1.word)  
  
  

[import]uid: 148857 topic_id: 36236 reply_id: 143971[/import]

I did what you did, it isn’t displaying the square and it’s not showing any errors but it is displaying the word

edit: actually, it does work, I just forgot to add the y-coordinate in the table.
[import]uid: 184921 topic_id: 36236 reply_id: 144126[/import]