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