[英]Lua OOP grid class with event
I'm learning lua and i'm trying the OOP approach. 我正在学习lua,并且正在尝试OOP方法。 To start, I'm trying to make a grid class, but I think i'm still lacking some knowledge to make it do what I want it to do. 首先,我正在尝试创建一个网格类,但是我认为我仍然缺少一些知识来使它完成我想做的事情。
This is my code so far: 到目前为止,这是我的代码:
local screen = require( 'data.screen')
Grid = {}
Grid_mt = { __index = Grid }
--- Constructs a new Grid object.
function Grid:new( params )
local self = {}
local function try( self, event )
print(self.empty)
end
for i = 1, screen.tilesAcross do
if not self[i] then
self[i] = {};
end
for j = 1, screen.tilesDown do
self[i][j] = display.newImageRect( params.group, "images/playable.png", screen.tileWidth, screen.tileHeight )
self[i][j].x = (j - 1) * (screen.tileWidth + screen.tileSpacing) + screen.leftSpacing
self[i][j].y = (i - 1) * (screen.tileHeight + screen.tileSpacing) + screen.topSpacing
self[i][j]._x = i
self[i][j]._y = j
self[i][j].enable = false
self[i][j].empty = true
self[i][j].tap = try
self[i][j]:addEventListener( "tap", self[i][j] )
end
end
setmetatable( self, Grid_mt )
return self
end
function Grid:setEnable(value, x, y)
if value ~= true and value ~= false then
error("Boolean expected")
end
self[x][y].enable = value
end
function Grid:getEnable(x, y)
return self[x][y].enable
end
function Grid:setEmpty(value, x, y)
if value ~= true and value ~= false then
error("Boolean expected")
end
self[x][y].empty = value
end
function Grid:getEmpty(x, y)
return self[x][y].empty
end
function Grid:SetmColor()
self[1][4]:setFillColor( 255,255 )
end
I have 2 questions: My event works but I would like to do something like: print(self:getEmpty() ) but whenever I try to use a method in my event, it doesn't work "attempt to call method 'getEmpty' (a nil value)" 我有2个问题:我的事件有效,但是我想做类似的事情:print(self:getEmpty()),但是每当我尝试在事件中使用方法时,它都不会起作用“试图调用方法'getEmpty' (零值)”
and also the setfillcolor wwon't work, I want to be able to change the color of a case with it. 并且setfillcolor无法正常工作,我希望能够使用它来更改案例的颜色。
Thanks for your time! 谢谢你的时间! and if i'm going to the wrong road, let me know, and by the way, I have a working code without make a class, this is just for training purpose :) 如果我走错了路,请告诉我,顺便说一句,我有一个没有上课的工作代码,这只是出于培训目的:)
Thnaks! Thnaks!
For the first question, more information would be needed. 对于第一个问题,将需要更多信息。 See my comment above . 见上面我的评论 。
As for the setFillColor
not working, the following is taken from the Corona docs : 至于setFillColor
不起作用,以下内容摘自Corona文档 :
object:setFillColor( gray )
object:setFillColor( gray, alpha )
object:setFillColor( red, green, blue )
object:setFillColor( red, green, blue, alpha )
object:setFillColor( gradient )
gray, red, green, blue, alpha
(optional)gray, red, green, blue, alpha
(可选)Numbers between 0 and 1 that represent the corresponding value for that channel. 0到1之间的数字表示该通道的相应值。 alpha represents the opacity of the object. alpha表示对象的不透明度。
Now, when you are trying to execute: 现在,当您尝试执行时:
self[1][4]:setFillColor( 255,255 )
you are passing the values for gray
and alpha
channels. 您正在传递gray
和alpha
通道的值。 The values NEED TO BE less than, or equal to 1
. 的值需要小于或等于1
。 Probably you want to pass 1
(as 255 is generally the max. value) 可能您想传递1
(因为255通常是最大值)
The issue is that self[i][j]
is not a Grid
, only self
is a Grid
. 问题是self[i][j]
不是Grid
,只有self
是Grid
。 So when the event handler is called, self
in the handler is the display object, not the Grid
object. 因此,在调用事件处理程序时,处理程序中的self
是显示对象,而不是Grid
对象。 If the handler needs to know the grid object, you could assign the grid to each display object: 如果处理程序需要了解网格对象,则可以将网格分配给每个显示对象:
self[i][j].grid = self
then in try
you could do 然后try
grid = self.grid
But in this case you may get a cleaner design if you have the tap handler not be specific to each display object. 但是在这种情况下,如果您没有特定于每个显示对象的点击处理程序,则可以得到更简洁的设计。 In this case you would use an upvalue, you can just use self
: 在这种情况下,您将使用升值,您可以仅使用self
:
function Grid:new( params )
local self = {}
local function try( event ) -- omit the first param, self is an upvalue
print(self.empty)
end
for i = 1, screen.tilesAcross do
...
for j = 1, screen.tilesDown do
self[i][j] = display.newImageRect( ... )
...
self[i][j]:addEventListener( "tap", try )
end
end
Since listener given is a function rather than a table, it gets only one parameter, the event, and self is an upvalue so it will be the Grid instance created just above the try
function. 由于给定的侦听器是一个函数而不是一个表,因此它仅获取一个参数,事件,而self是一个升值,因此它将是在try
函数上方创建的Grid实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.