Answer the question
In order to leave comments, you need to log in
Visibility of variables and OOP in lua?
I use Corona sdk to develop games for mobile platforms. Upon completion of the first project, I realized the need to use OOP in the following games, but I encounter difficulties.
To create classes, I use this library yonaba.github.io/30log
The essence of the question is as follows:how can an instance of a class call a function from the main scene? For example - in level.lua I control the calculation of game points, create, for example, a soap bubble, when you click on the bubble, the points counter should increase by 10. If I create it with a function in level.lua itself, there is no problem, I just go to the points variable I add 10. But if I create it as an instance of a class (let's say ball.lua), then how do I know in level.lua that the bubble was pressed? Of course, the bubble itself knows that it was clicked, but how to access the counter variable in order to increase it?
Answer the question
In order to leave comments, you need to log in
either so , or pass in the constructor of each game object some object of the world type, in which all objects are stored.
Ball = class("Ball", { x = 0, y = 0, world = nil })
function Ball:init(x, y, world)
self.x, self.y self.world = x, y, world
end
function Ball:touch()
local counter = self.world.get('counter')
counter.increase(10)
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question