Lua OOP网格类与事件 [英] Lua OOP grid class with event

查看:295
本文介绍了Lua OOP网格类与事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习lua,我正在尝试OOP方法。
要开始,我试图做一个网格类,但我想我仍然缺乏一些知识,使它做我想要做的。



这是我的代码到目前为止:

 本地屏幕= require('data.screen')
网格= {}
Grid_mt = {__index = Grid}



---构造一个新的Grid对象。
函数Grid:new(params)

local self = {}

本地函数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


函数网格:setEnable(value,x,y)
如果值〜= true和值〜= false然后
错误(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)
如果值〜= true和值〜= false然后
错误(Boolean expected)
end
self [x] [y] empty = value
end

function Grid:getEmpty(x,y)
return self [x] [y] .empty
end

函数Grid:SetmColor()
self [1] [4]:setFillColor(255,255)
end

我有两个问题:
我的事件有效,但我想做一些像:print(self:getEmpty())
,但每当我尝试使用一个方法我的事件,它不工作尝试调用方法'getEmpty'(一个零值)



以及setfillcolor不工作,我想成为能够改变一个案件的颜色。



感谢你的时间!
,如果我要走错路,让我知道,顺便说一下,我有一个没有上课的工作代码,这只是为了训练目的:)



Thnaks!

解决方案

问题是 self [i] [j ] 不是 Grid ,只有 self 是一个网格。所以当调用事件处理程序时,处理程序中的 self 是显示对象,而不是 Grid 对象。如果处理程序需要知道网格对象,则可以将网格分配给每个显示对象:

  self [i] [j ] .grid = self 

然后在 try 你可以做

  grid = self.grid 

但是,在这种情况下,如果您的点击处理程序不是每个显示对象都具体的,您可能会得到一个更干净的设计。在这种情况下,您可以使用最高价值,您可以使用 self

  function Grid:new(params)

local self = {}

本地函数try(event) - 省略第一个参数,self是一个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

由于收听者是一个函数比起一个表,它只得到一个参数,事件和self是一个更高的值,所以它将是在 try 函数之上创建的Grid实例。


I'm learning lua and i'm trying the OOP approach. 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)"

and also the setfillcolor wwon't work, I want to be able to change the color of a case with it.

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!

解决方案

The issue is that self[i][j] is not a Grid, only self is a Grid. So when the event handler is called, self in the handler is the display object, not the Grid object. 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

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:

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.

这篇关于Lua OOP网格类与事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆