How to create an object oriented game with corona

Hey i have experience with oop on libgdx, android sdk etc.

but on corona i cant figure out how to use classes in a game to make it efficient.

like making a player class who has:

  • its x and y position

  • its image

  • getters and setters

  • and some other functions like move jump etc.

Can someone send me a tutorial that shows how to implement it in a game?

Here are some links about oop ‘like’ programming using lua.  

There are more then these out there on  the internet, a good search on google and on the forum site should give you some options.  I say options because there are several different ways apparently to structure your code in an oop-like manner :

 

http://forums.coronalabs.com/topic/49915-loogine-a-new-open-source-oop-engine-based-on-luoop-in-progress/

 

http://jessewarden.com/2011/07/corona-game-example-oop-scope-box2d-collisions-part-1.html

 

http://lazywombat.co.uk/object-oriented-programming-in-corona-sdk-part-1-basics/

 

 

Good luck.

Here is a more recent one from Develephant …  his work is excellent and contributes a lot to the Corona community. This may be closer to what you are looking for.

http://www.develephant.net/an-rpg-themed-oop-design-pattern-for-corona-sdk-part-1/

Bob

But i dont understand how can i add an image from another lua file to the main one.

what i mean is that i have a file called level 1 which is the main file of the game and i have a player class with image in it and i want to add the image through the player file to the scene group in the level 1 file, how do i manage to do it?

to your first question “how to oop”, google is your friend, but wrt corona there are several common patterns.

  1. you can just “decorate” display objects:

    local player = display.newImage(“player.png”, …whatever…) player.customProperty = 1234 function player:customMethod() print(“hello”) end

  2. you can create lua “wrapper” classes

    – player.lua local Player = {} local Player_mt = {__index=Player} – prep for metatable-based oop function Player:new() local instance = {} setmetatable(instance,Player_mt) – now instance isa Player instance.view = display.newImage(“player.png”, …whatever…) – and “owns” a display object return instance end return Player

and of course there are many variations on these two patterns, as well as other patterns, google it, cuz i’m too lazy to type it all up here :smiley:  some approaches are simpler to implement than others, but may not do “fancy” things like inheritance. suggest a lib like 30log (et al) if you’d like a full-blown oop system pre-built for you, or roll your own “one-liner” metatable-based class system.   you’ll have to experiment and pick an approach that works best for you – there is no one right way.

to your second question “how to reference”, let’s assume you have the Player.lua above, then in some other module:

-- level1.lua local Player = require("Player") -- get the class local player = Player:new() -- create an instance scene.view:insert(player.view) -- put player's display object into scene's group

Hey, first thank you for your answer!

second it doesn’t let me insert the player into the group view i wrote the player class like this:

-- player.lua local Player = {} local Player\_mt = {\_\_index=Player} -- prep for metatable-based oop function Player:new() { local instance = {} setmetatable(instance,Player\_mt) -- now instance isa Player instance.view = display.newImage("crate.png", 10,10) -- and "owns" a display object return instance end return Player

and the level 1 class like this:

local composer = require( "composer" ) local scene = composer.newScene() local Player = require("Player") -- get the class local player = Player:new() -- create an instance -- include Corona's "physics" library local physics = require "physics" physics.start(); physics.pause() -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 function scene:create( event ) -- Called when the scene's view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. local sceneGroup = self.view -- create a grey rectangle as the backdrop local background = display.newRect( 0, 0, screenW, screenH ) background.anchorX = 0 background.anchorY = 0 background:setFillColor( .5 ) -- make a crate (off-screen), position it, and rotate slightly -- create a grass object and add physics (with custom shape) local grass = display.newImageRect( "grass.png", screenW, 82 ) grass.anchorX = 0 grass.anchorY = 1 grass.x, grass.y = 0, display.contentHeight -- define a shape that's slightly shorter than image bounds (set draw mode to "hybrid" or "debug" to see) local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 } physics.addBody( grass, "static", { friction=0.3, shape=grassShape } ) -- all display objects must be inserted into group sceneGroup:insert( background ) sceneGroup:insert( grass) sceneGroup.view:insert( player.view ) end

but it gives me an error

level1.lua:51: attempt to index field ‘view’ (a nil value)

You could get that error if the image was not found.  Do you have a 'crate.png" in your main directory where your main.lua file is?  Is the name of the image file spelled exactly the same as it is in your code?  In other words, “crate.png” in your code when the file name has a different ‘case’ like 'Crate.png" will fail.

That is what I would check first.

I don’t use meta-tables myself, so I cannot answer to if there is anything wrong there (I just don’t understand meta tables).  

Dave seems very knowledgable, so I imagine the code is correct, therefore I think I would look at that filename of the image being a possible cause.

Good luck

Bob

[quote name=“cyberparkstudios” post=“314097” timestamp=“1450054703”]You could get that error if the image was not found.  Do you have a 'crate.png" in your main directory where your main.lua file is?  Is the name of the image file spelled exactly the same as it is in your code?  In other words, “crate.png” in your code when the file name has a different ‘case’ like 'Crate.png" will fail.   That is what I would check first.   I don’t use meta-tables myself, so I cannot answer to if there is anything wrong there (I just don’t understand meta tables).     Dave seems very knowledgable, so I imagine the code is correct, therefore I think I would look at that filename of the image being a possible cause.   Good luck Bob[/quote] What are you using instead of meta tables?

you assign the scene’s view to sceneGroup, so just use that “sceneGroup:insert(player.view)”, same as when you insert the grass, sceneGroup itself has no .view property

I tried it still isn’t working i wrote

sceneGroup:insert( player.view )

but it gives me an error

level1.lua:51: ERROR table expected. if this is a fuction call you might have used '.' instead of ':'

I use a method like this :

http://theindiestone.com/forums/index.php/topic/8023-simple-lua-oop-without-metatables/

I should have included this link originally, but I had lost it from my reference folder.  I had to search google again to find it for you.

I do not know which method is better or best.  This one works for me.  There are apparently several ways to create an ‘oop’ style in lua, but I don’t have time to play around and find which is best. I settled on this method; and like I say it works for me.

If you are going to do some serious coding with corona, you benefit yourself to research several of the articles out there about oop and lua/corona and find the one you understand the best and works for you.  I don’t know enough about all of them to steer you in any direction, that is why i provided several links and the suggestion to search for further articles on oop.

Good Luck. 

Bob

no offense intended, but you should probably start w something simpler –  learn from that first, then make it more complicated as you become more comfortable with some basic things.   for example, ignore your Player.lua entirely for now, and just try this simple test near the bottom of scene:create()

player={} -- just an empty table as an "object", not a "class" player.img=display.newImage("crate.png",10,10) -- give it a display object as a property self.view:insert(player.img) -- put that display object into scene's display group

Once you can get something like that to work, THEN maybe you can consider pulling it out into a module/etc.

Right now your problem really isn’t the oop/class/module stuff – you just have some “simple” problems with Corona fundamentals.

Here are some links about oop ‘like’ programming using lua.  

There are more then these out there on  the internet, a good search on google and on the forum site should give you some options.  I say options because there are several different ways apparently to structure your code in an oop-like manner :

 

http://forums.coronalabs.com/topic/49915-loogine-a-new-open-source-oop-engine-based-on-luoop-in-progress/

 

http://jessewarden.com/2011/07/corona-game-example-oop-scope-box2d-collisions-part-1.html

 

http://lazywombat.co.uk/object-oriented-programming-in-corona-sdk-part-1-basics/

 

 

Good luck.

Here is a more recent one from Develephant …  his work is excellent and contributes a lot to the Corona community. This may be closer to what you are looking for.

http://www.develephant.net/an-rpg-themed-oop-design-pattern-for-corona-sdk-part-1/

Bob

But i dont understand how can i add an image from another lua file to the main one.

what i mean is that i have a file called level 1 which is the main file of the game and i have a player class with image in it and i want to add the image through the player file to the scene group in the level 1 file, how do i manage to do it?

to your first question “how to oop”, google is your friend, but wrt corona there are several common patterns.

  1. you can just “decorate” display objects:

    local player = display.newImage(“player.png”, …whatever…) player.customProperty = 1234 function player:customMethod() print(“hello”) end

  2. you can create lua “wrapper” classes

    – player.lua local Player = {} local Player_mt = {__index=Player} – prep for metatable-based oop function Player:new() local instance = {} setmetatable(instance,Player_mt) – now instance isa Player instance.view = display.newImage(“player.png”, …whatever…) – and “owns” a display object return instance end return Player

and of course there are many variations on these two patterns, as well as other patterns, google it, cuz i’m too lazy to type it all up here :smiley:  some approaches are simpler to implement than others, but may not do “fancy” things like inheritance. suggest a lib like 30log (et al) if you’d like a full-blown oop system pre-built for you, or roll your own “one-liner” metatable-based class system.   you’ll have to experiment and pick an approach that works best for you – there is no one right way.

to your second question “how to reference”, let’s assume you have the Player.lua above, then in some other module:

-- level1.lua local Player = require("Player") -- get the class local player = Player:new() -- create an instance scene.view:insert(player.view) -- put player's display object into scene's group

Hey, first thank you for your answer!

second it doesn’t let me insert the player into the group view i wrote the player class like this:

-- player.lua local Player = {} local Player\_mt = {\_\_index=Player} -- prep for metatable-based oop function Player:new() { local instance = {} setmetatable(instance,Player\_mt) -- now instance isa Player instance.view = display.newImage("crate.png", 10,10) -- and "owns" a display object return instance end return Player

and the level 1 class like this:

local composer = require( "composer" ) local scene = composer.newScene() local Player = require("Player") -- get the class local player = Player:new() -- create an instance -- include Corona's "physics" library local physics = require "physics" physics.start(); physics.pause() -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 function scene:create( event ) -- Called when the scene's view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. local sceneGroup = self.view -- create a grey rectangle as the backdrop local background = display.newRect( 0, 0, screenW, screenH ) background.anchorX = 0 background.anchorY = 0 background:setFillColor( .5 ) -- make a crate (off-screen), position it, and rotate slightly -- create a grass object and add physics (with custom shape) local grass = display.newImageRect( "grass.png", screenW, 82 ) grass.anchorX = 0 grass.anchorY = 1 grass.x, grass.y = 0, display.contentHeight -- define a shape that's slightly shorter than image bounds (set draw mode to "hybrid" or "debug" to see) local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 } physics.addBody( grass, "static", { friction=0.3, shape=grassShape } ) -- all display objects must be inserted into group sceneGroup:insert( background ) sceneGroup:insert( grass) sceneGroup.view:insert( player.view ) end

but it gives me an error

level1.lua:51: attempt to index field ‘view’ (a nil value)

You could get that error if the image was not found.  Do you have a 'crate.png" in your main directory where your main.lua file is?  Is the name of the image file spelled exactly the same as it is in your code?  In other words, “crate.png” in your code when the file name has a different ‘case’ like 'Crate.png" will fail.

That is what I would check first.

I don’t use meta-tables myself, so I cannot answer to if there is anything wrong there (I just don’t understand meta tables).  

Dave seems very knowledgable, so I imagine the code is correct, therefore I think I would look at that filename of the image being a possible cause.

Good luck

Bob

[quote name=“cyberparkstudios” post=“314097” timestamp=“1450054703”]You could get that error if the image was not found.  Do you have a 'crate.png" in your main directory where your main.lua file is?  Is the name of the image file spelled exactly the same as it is in your code?  In other words, “crate.png” in your code when the file name has a different ‘case’ like 'Crate.png" will fail.   That is what I would check first.   I don’t use meta-tables myself, so I cannot answer to if there is anything wrong there (I just don’t understand meta tables).     Dave seems very knowledgable, so I imagine the code is correct, therefore I think I would look at that filename of the image being a possible cause.   Good luck Bob[/quote] What are you using instead of meta tables?

you assign the scene’s view to sceneGroup, so just use that “sceneGroup:insert(player.view)”, same as when you insert the grass, sceneGroup itself has no .view property