Modular Engine Class

I am new to Corona and i am trying to build a game engine class. I’d like to be able to spawn enemies, pause and resume the game, and other game functions from this class. I have seen classes like the dog class (linked to below), but i don’t want to call a new function to setup variables. Does anyone have a sample or know of a place i can find a sample of code for something like this?

Dog Class: http://www.coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/

[import]uid: 26491 topic_id: 30357 reply_id: 330357[/import]

Hi.
I am not sure to catch the problem, but I assume you need to create a class and instantiate objects without calling specifically the new constructor.

So fair, enough, you can work this way:

aClass = {}  
aClass.\_\_index = aClass  
setmetatable(aClass, {\_\_call = function(self,...) return self:new(...) end})  
  
function aClass:new(x,y)  
 local anObject = {x = x,y = y}  
 return setmetatable(anObject,aClass)  
end  

Now, creating an object can be achieved this way:

obj = aClass:new(1,2)

or this way:

obj = aClass(1,2)

Same effect.

Edit: You can have a look at 30log or Lua-Class-System (more advanced, though). [import]uid: 142361 topic_id: 30357 reply_id: 121647[/import]

Awesome!! Thank you so much. That is exactly what i was looking for. :slight_smile: [import]uid: 26491 topic_id: 30357 reply_id: 121689[/import]

Do you happen to know a way to use sprite sheets and display objects in this type of class? I setup a variable called enemySprite that holds the sprite, but now on collision events e.other refers to the enemySprite and not the enemy class itself. I’m not sure if there is a way to reference the main object from e.other or if i am way off in doing this anyway. Here is some of my code:

This is a different class than the one in the question above btw (obviously this isn’t then entire class, but hopefully it’s enough for you to go on).

[code]


– enemy.lua

local enemy = {}
local enemy_mt = { __index = enemy } – metatable


– PRIVATE FUNCTIONS


– PUBLIC FUNCTIONS

function enemy.new( displayGroup, name ) – constructor
– Load sprite sheet data and prepare sprite
local enemyData = require(“resources.sprite_sheets.guy”)
local enemySpriteSheet = sprite.newSpriteSheetFromData( “resources/sprite_sheets/guy.png”, enemyData.getSpriteSheetData() )
local enemySpriteSet = sprite.newSpriteSet(enemySpriteSheet, 1, 24)
sprite.add(enemySpriteSet,“run”, 1, 10, 1000, 0)
sprite.add(enemySpriteSet,“walk”, 11, 14, 1000, 0)
local enemySprite = sprite.newSprite(enemySpriteSet)

– Insert cat into display group
–displayGroup:insert(enemySprite)

– Prepare animation and play
enemySprite:prepare(“walk”)
enemySprite:play()

– Add physics body
physics.addBody(enemySprite, “kinematic”, { isSensor = true })

enemySprite:setLinearVelocity(1000)

– Position cat
enemySprite.x = -(enemySprite.width)
enemySprite.y = 312 - (enemySprite.height * 0.5)
enemySprite.collisionCount = 0

local newEnemy = {
enemyTableId,
objType = “enemy”,
name = name or “Unnamed”,
active = false,
collisionCount = 0,
enemyData = enemyData,
enemySpriteSheet = enemySpriteSheet,
enemySpriteSet = enemySpriteSet,
enemySprite = enemySprite,
destroyTimer
}

return setmetatable( newEnemy, enemy_mt )
end

function enemy:move(velocity)
– Add collision listener
self.enemySprite:addEventListener(“collision”, self)

– Set velocity
self.enemySprite:setLinearVelocity(velocity)

self.setDestroyTimer = timer.performWithDelay(10, function() – Wait 1 frame
if self.enemySprite.x > 0 then
timer.cancel(self.setDestroyTimer)
self:destroyTimer()
end
end, 0)
end

return enemy
[/code] [import]uid: 26491 topic_id: 30357 reply_id: 121715[/import]

Hi.
I am not sure to catch the problem, but I assume you need to create a class and instantiate objects without calling specifically the new constructor.

So fair, enough, you can work this way:

aClass = {}  
aClass.\_\_index = aClass  
setmetatable(aClass, {\_\_call = function(self,...) return self:new(...) end})  
  
function aClass:new(x,y)  
 local anObject = {x = x,y = y}  
 return setmetatable(anObject,aClass)  
end  

Now, creating an object can be achieved this way:

obj = aClass:new(1,2)

or this way:

obj = aClass(1,2)

Same effect.

Edit: You can have a look at 30log or Lua-Class-System (more advanced, though). [import]uid: 142361 topic_id: 30357 reply_id: 121647[/import]

Awesome!! Thank you so much. That is exactly what i was looking for. :slight_smile: [import]uid: 26491 topic_id: 30357 reply_id: 121689[/import]

Do you happen to know a way to use sprite sheets and display objects in this type of class? I setup a variable called enemySprite that holds the sprite, but now on collision events e.other refers to the enemySprite and not the enemy class itself. I’m not sure if there is a way to reference the main object from e.other or if i am way off in doing this anyway. Here is some of my code:

This is a different class than the one in the question above btw (obviously this isn’t then entire class, but hopefully it’s enough for you to go on).

[code]


– enemy.lua

local enemy = {}
local enemy_mt = { __index = enemy } – metatable


– PRIVATE FUNCTIONS


– PUBLIC FUNCTIONS

function enemy.new( displayGroup, name ) – constructor
– Load sprite sheet data and prepare sprite
local enemyData = require(“resources.sprite_sheets.guy”)
local enemySpriteSheet = sprite.newSpriteSheetFromData( “resources/sprite_sheets/guy.png”, enemyData.getSpriteSheetData() )
local enemySpriteSet = sprite.newSpriteSet(enemySpriteSheet, 1, 24)
sprite.add(enemySpriteSet,“run”, 1, 10, 1000, 0)
sprite.add(enemySpriteSet,“walk”, 11, 14, 1000, 0)
local enemySprite = sprite.newSprite(enemySpriteSet)

– Insert cat into display group
–displayGroup:insert(enemySprite)

– Prepare animation and play
enemySprite:prepare(“walk”)
enemySprite:play()

– Add physics body
physics.addBody(enemySprite, “kinematic”, { isSensor = true })

enemySprite:setLinearVelocity(1000)

– Position cat
enemySprite.x = -(enemySprite.width)
enemySprite.y = 312 - (enemySprite.height * 0.5)
enemySprite.collisionCount = 0

local newEnemy = {
enemyTableId,
objType = “enemy”,
name = name or “Unnamed”,
active = false,
collisionCount = 0,
enemyData = enemyData,
enemySpriteSheet = enemySpriteSheet,
enemySpriteSet = enemySpriteSet,
enemySprite = enemySprite,
destroyTimer
}

return setmetatable( newEnemy, enemy_mt )
end

function enemy:move(velocity)
– Add collision listener
self.enemySprite:addEventListener(“collision”, self)

– Set velocity
self.enemySprite:setLinearVelocity(velocity)

self.setDestroyTimer = timer.performWithDelay(10, function() – Wait 1 frame
if self.enemySprite.x > 0 then
timer.cancel(self.setDestroyTimer)
self:destroyTimer()
end
end, 0)
end

return enemy
[/code] [import]uid: 26491 topic_id: 30357 reply_id: 121715[/import]