Classes in Corona SDK and manipulating them in pong

So lately I’ve been trying to make a remake of pong however I want to stay professional and create each game object as their own class (in the oop sense). The main thing I am struggling on right now is the paddle. I have a class called “paddle”. Here is the code for it

local paddle = {} local paddle\_mt = { \_\_index = paddle} ------------------------------------------------- -PRIVATE FUNCTIONS ------------------------------------------------- ------------------------------------------------- -PUBLIC FUNCTIONS ------------------------------------------------- function paddle.new() -- constructor local image = display.newImage("paddle.png") image.x = 150; image.y = 150 image.anchorX - 0.5; image.anchorY = 0.5 return image end function paddle.move(event) -- move the paddle end return paddle

what I am stuck on is how to make the paddle move from an event listener in the main file. Here is the code

local paddle = require ("paddleClass") background = display.newRect(150,300,1080,1920) local newPaddle = paddle.new() background:addEventListener ("touch", paddle.move)

I don’t know how to make the paddle move from the main lua file without getting message boxes saying error. I believe the problem is that when I make the class, I want to add the listener to the event inside the class, when the object was created and “brought to existence” in the main file so the real question is: How do I move the paddle when it hasn’t been created yet. Because this function of moving the paddle is going to be shared amongst other game modes of my pong game, so i’m putting this in a class because it would take memory

I don’t mean to be a downer here and I don’t want to discourage learning.  However, I think adding OOP complexity on top of learning Lua , Corona, and game design is going to be a real challenge and may not help you much in the future.  

(Others may disagree.)

I say this for two reasons:

  1. LUA is not strictly an OOP language.  It support most of the features of an OOP language, but personally I find it to be a poor starting place to learn OOP.  (I’m an old biased guy who learned OOP on old languages like Ada, C++, COBOL, Lisp, etc.
  2. In my experience, especially w/ 2D games, you can more easily and quickly make them without resorting to full-on OOP design.  Modules are good, ‘classes’ not so much.

However, let me ask you to sort this list for me by order of importance.  

  • Learn Corona
  • Learn LUA
  • Make a game(s)
  • Learn about game development
  • Learn OOP
  • Other

What order would you sort the above list in (i.e. first being most important to you, second next most important, and so forth).

If you don’t ignore me, which is fine if you do, I’d suggest focusing on learning to write modules instead.  This is a step in the right direction for organization and has some correlation to OOP design without working about things like:

  • Encapsulation (private and public members/methods)
  • Inheritance
  • Abstraction
  • Polymorphism

I know I should just shut up, but I want to say one more thing.

You can absolutely write an OOP example using LUA.  You can set up ‘classes’ and class hierarchies.  However, as soon as you try to merge that with the Corona modules and libraries you’ll run into issues.  You just can’t easily or nicely turn Corona objects into OOP objects that exhibit the traits mentioned above.  Corona is not meant to be used that way.  It follows a module based design and that is it.  They kept is simple instead of layering on top their own rules and abstractions.  This is why it is so easy to use and build up from.

OK. Done.  Best of luck to you and I hope to see great things.

Cheers,

Ed

Oh yeah, I know lua isn’t strictly the oop language. I know enough of c# to replicate pong on there, however I use corona for the box2d physics and the simulator, plus it’s just easier in general. The reason I was doing some oop on this game is because I’m almost done with the game already. But I look back and I realize that my code was all messy, and everything was unorganized and out of place - then again, it still works. It also took up more memory than I wanted it to be, so I decided to use classes to conserve some space and be more organized

I don’t mean to be a downer here and I don’t want to discourage learning.  However, I think adding OOP complexity on top of learning Lua , Corona, and game design is going to be a real challenge and may not help you much in the future.  

(Others may disagree.)

I say this for two reasons:

  1. LUA is not strictly an OOP language.  It support most of the features of an OOP language, but personally I find it to be a poor starting place to learn OOP.  (I’m an old biased guy who learned OOP on old languages like Ada, C++, COBOL, Lisp, etc.
  2. In my experience, especially w/ 2D games, you can more easily and quickly make them without resorting to full-on OOP design.  Modules are good, ‘classes’ not so much.

However, let me ask you to sort this list for me by order of importance.  

  • Learn Corona
  • Learn LUA
  • Make a game(s)
  • Learn about game development
  • Learn OOP
  • Other

What order would you sort the above list in (i.e. first being most important to you, second next most important, and so forth).

If you don’t ignore me, which is fine if you do, I’d suggest focusing on learning to write modules instead.  This is a step in the right direction for organization and has some correlation to OOP design without working about things like:

  • Encapsulation (private and public members/methods)
  • Inheritance
  • Abstraction
  • Polymorphism

I know I should just shut up, but I want to say one more thing.

You can absolutely write an OOP example using LUA.  You can set up ‘classes’ and class hierarchies.  However, as soon as you try to merge that with the Corona modules and libraries you’ll run into issues.  You just can’t easily or nicely turn Corona objects into OOP objects that exhibit the traits mentioned above.  Corona is not meant to be used that way.  It follows a module based design and that is it.  They kept is simple instead of layering on top their own rules and abstractions.  This is why it is so easy to use and build up from.

OK. Done.  Best of luck to you and I hope to see great things.

Cheers,

Ed

Oh yeah, I know lua isn’t strictly the oop language. I know enough of c# to replicate pong on there, however I use corona for the box2d physics and the simulator, plus it’s just easier in general. The reason I was doing some oop on this game is because I’m almost done with the game already. But I look back and I realize that my code was all messy, and everything was unorganized and out of place - then again, it still works. It also took up more memory than I wanted it to be, so I decided to use classes to conserve some space and be more organized