Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JavaScript oop代码转换为Lua_Javascript_Oop_Lua - Fatal编程技术网

将JavaScript oop代码转换为Lua

将JavaScript oop代码转换为Lua,javascript,oop,lua,Javascript,Oop,Lua,我正在尝试将一组代码从JavaScript翻译成Lua。不过,我在下一点上被卡住了。我不理解Lua面向对象的语法。有人能告诉我正确的语法是什么吗?谢谢 //======================================================= //======================================================= //=======================================================

我正在尝试将一组代码从JavaScript翻译成Lua。不过,我在下一点上被卡住了。我不理解Lua面向对象的语法。有人能告诉我正确的语法是什么吗?谢谢

//=======================================================
//=======================================================
//=======================================================
// The TMaze object
//------------------------------------------------------------------------------ Ctor
function TMaze(nHigh, nWide) {
    this.high = nHigh;
    this.wide = nWide;
    this.ary = TMazeArray(nHigh, nWide);

    this.totRooms = gnHigh * gnWide;  // less (short) bottom line
    this.curRoomCnt = 0;  // rooms that have been visited/door opened

    this.GetCell = function(x,y) {return this.ary[y][x];}
    this.SetCell = function(x,y,value) {this.ary[y][x] = value;}

    this.HasCellBit = function(x,y,value) {return ((this.ary[y][x] & value) == value);}
    this.SetCellBit = function(x,y,value) {this.ary[y][x] |= value;}
    this.ClearCellBit = function(x,y,value) {this.ary[y][x] &= ~value;}
    this.ToggleCellBit = function(x,y,value) {this.ary[y][x] ^= value;}

    this.IsEmptyCellAt = IsEmptyCellAt;  // some member fns, defined below
    this.IsValidXY = IsValidXY;
    this.GetNewXY = GetNewXY;
}

//----------- TRUE if cell in that direction is empty and valid
//
function IsEmptyCellAt(x, y, dir) {
    var o = this.GetNewXY(x, y, dir);

    if (!this.IsValidXY(o.newX, o.newY))   return (false); 
    if (this.GetCell(o.newX, o.newY) != 0)  return (false);

    return true;  // yes, it's possible to move into that cell
}

//------------------------------------------------------------------------------
// return -1 if that would be an invalid move (off the board)
// true if X,Y is on the board
//
function IsValidXY(x, y) {
    if (y < 0) return (false);
    if (y > this.high-1) return (false);

    if (x < 0) return (false);
    if (x > this.wide-1) return (false);

//    if (y & 1) {  // on off Y, max X an maxY are 1 less
//        if (x > this.wide-2) return (false);
//        if (y > this.high-2) return (false);
//    }
    return true;  // possible to move into that direction
}

//------------------------------------------
// If I move in a direction, what are the new X,Y values?
// Return an object with newX and newY properties
//
function GetNewXY(x, y, dir) {
    var oRet = {"newX":-1, "newY":-1, "isValid":false};
    var newX = x;
    var newY = y;

    newX += ganDeltaX[dir];  // add the deltas
    newY += ganDeltaY[dir];

    if (this.IsValidXY(newX, newY)) {
        oRet.newX = newX;
        oRet.newY = newY;
        oRet.isValid = true;
    }
    return oRet;
}
//=======================================================
//=======================================================
//=======================================================
//TMaze对象
//------------------------------------------------------------------------------执行器
功能TMaze(nHigh,nWide){
high=nHigh;
this.wide=nWide;
this.ari=TMazeArray(nHigh,nWide);
this.totRooms=gnHigh*gnWide;//更少(短)底线
this.curRoomCnt=0;//已访问的房间/门已打开
this.GetCell=函数(x,y){返回this.ary[y][x];}
this.SetCell=函数(x,y,value){this.ary[y][x]=value;}
this.HasCellBit=函数(x,y,value){return((this.ary[y][x]&value)==value);}
this.SetCellBit=函数(x,y,value){this.ary[y][x]|=value;}
this.ClearCellBit=函数(x,y,value){this.ary[y][x]&=~value;}
this.ToggleCellBit=函数(x,y,value){this.ary[y][x]^=value;}
this.IsEmptyCellAt=IsEmptyCellAt;//某些成员FN,定义如下
this.IsValidXY=IsValidXY;
this.GetNewXY=GetNewXY;
}
//-----------如果该方向的单元格为空且有效,则为TRUE
//
函数Isemptycella(x,y,dir){
var o=this.GetNewXY(x,y,dir);
如果(!this.IsValidXY(o.newX,o.newY))返回(false);
if(this.GetCell(o.newX,o.newY)!=0)返回(false);
return true;//是的,可以移动到该单元格中
}
//------------------------------------------------------------------------------
//返回-1,如果这是无效的移动(离开板)
//如果板上有X,Y,则为true
//
函数IsValidXY(x,y){
如果(y<0)返回(false);
如果(y>此.high-1)返回(false);
如果(x<0)返回(false);
如果(x>this.wide-1)返回(false);
//如果(y&1){//on off y,则max X和max y小于1
//如果(x>this.wide-2)返回(false);
//如果(y>此.high-2)返回(false);
//    }
return true;//可能向该方向移动
}
//------------------------------------------
//如果我朝一个方向移动,新的X,Y值是什么?
//返回具有newX和newY属性的对象
//
函数GetNewXY(x,y,dir){
var-oRet={“newX”:-1,“newY”:-1,“isValid”:false};
var newX=x;
var-newY=y;
newX+=ganDeltaX[dir];//添加增量
newY+=ganDeltaY[dir];
if(this.IsValidXY(newX,newY)){
oRet.newX=newX;
oRet.newY=newY;
oRet.isValid=true;
}
返回oRet;
}
您可以忽略按位操作,因为我已经知道如何转换它。我最关心的是面向对象的东西

[编辑]

这是我到目前为止得到的,但它崩溃了

--=======================================================
--=======================================================
-- The TMaze object
-------------------------------------------------------------------------------- Ctor
TMaze =
{
    high = gnHigh,
    wide = gnWide,
    ary = TMazeArray(gnHigh, gnWide),
    totRooms = gnHigh * gnWide,         -- less (short) bottom line
    curRoomCnt = 0,                     -- rooms that have been visited/door opened
}
TMaze.GetCell = function(self,x,y) return self.ary[y][x] end
TMaze.SetCell = function(self,x,y,value) self.ary[y][x] = value end
TMaze.HasCellBit = function(self,x,y,value) return bit.band(self.ary[y][x], value) == value end
TMaze.SetCellBit = function(self,x,y,value) self.ary[y][x] = bit.bor(self.ary[y][x], value) end
TMaze.ClearCellBit = function(self,x,y,value) self.ary[y][x] = bit.band(self.ary[y][x], bit.bnot(value)) end
TMaze.ToggleCellBit = function(self,x,y,value) self.ary[y][x] = bit.bxor(self.ary[y][x], value) end

--=======================================================
------------- TRUE if cell in that direction is empty and valid
--
TMaze.IsEmptyCellAt = function(self, x, y, dir)
    local o = self.GetNewXY(self, x, y, dir)
    if (not self.IsValidXY(self, o.newX, o.newY)) then return 0 end
    if (self.GetCell(self, o.newX, o.newY) ~= 0) then return 0 end
    return 1  -- yes, it's possible to move into that cell
end

--------------------------------------------------------------------------------
-- return -1 if that would be an invalid move (off the board)
-- true if X,Y is on the board
--
TMaze.IsValidXY = function(self, x, y)
    if (y < 1) then return 0 end
    if (y > self.high) then return 0 end

    if (x < 1) then return 0 end
    if (x > self.wide) then return 0 end

--  if (y & 1) then  -- on off Y, max X an maxY are 1 less
--      if (x > self.wide-2) then return 0 end
--      if (y > self.high-2) then return 0 end
--  end
    return 1  -- possible to move into that direction
end

--------------------------------------------
-- If I move in a direction, what are the new X,Y values?
-- Return an object with newX and newY properties
--
TMaze.GetNewXY =  function(self, x, y, dir)
    local oRet = {newX=-1, newY=-1, isValid=0}
    local newX = x
    local newY = y

    newX = newX + ganDeltaX[dir]  -- add the deltas
    newY = newY + ganDeltaY[dir]

    if (self.IsValidXY(self, newX, newY)) then
        oRet.newX = newX
        oRet.newY = newY
        oRet.isValid = 1
    end
    return oRet
end
--=======================================================
--=======================================================
--TMaze对象
--------------------------------------------------------------------------------执行器
TMaze=
{
高=gnHigh,
宽=gnWide,
ary=TMazeArray(gnHigh,gnWide),
总房间数=gnHigh*gnWide,--小于(短)底线
curRoomCnt=0,--已访问的房间/门已打开
}
TMaze.GetCell=函数(self,x,y)返回self.ary[y][x]结束
TMaze.SetCell=函数(self,x,y,value)self.ary[y][x]=值结束
TMaze.HasCellBit=函数(self,x,y,value)返回位.band(self.ary[y][x],value)==值结束
TMaze.SetCellBit=函数(self,x,y,value)self.ary[y][x]=bit.bor(self.ary[y][x],value)结束
TMaze.ClearCellBit=函数(self,x,y,value)self.ary[y][x]=位带(self.ary[y][x],bit.bnot(value))结束
TMaze.ToggleCellBit=函数(self,x,y,value)self.ary[y][x]=位bxor(self.ary[y][x],value)结束
--=======================================================
-------------如果该方向的单元格为空且有效,则为TRUE
--
TMaze.IsEmptyCellAt=函数(self、x、y、dir)
本地o=self.GetNewXY(self,x,y,dir)
如果(不是self.IsValidXY(self,o.newX,o.newY)),则返回0结束
如果(self.GetCell(self,o.newX,o.newY)~=0),则返回0结束
返回1——是的,可以移动到该单元格中
终止
--------------------------------------------------------------------------------
--返回-1,如果这是无效的移动(离开板)
--如果板上有X,Y,则为true
--
TMaze.IsValidXY=函数(self,x,y)
如果(y<1),则返回0结束
如果(y>自高),则返回0结束
如果(x<1),则返回0结束
如果(x>self.wide),则返回0结束
--如果是(y&1),则--on off y,max X和maxY小于1
--如果(x>self.wide-2),则返回0结束
--如果(y>self.high-2),则返回0结束
--结束
返回1——可能向该方向移动
终止
--------------------------------------------
--如果我朝一个方向移动,新的X,Y值是什么?
--返回具有newX和newY属性的对象
--
TMaze.GetNewXY=函数(self、x、y、dir)
本地oRet={newX=-1,newY=-1,isValid=0}
局部newX=x
本地newY=y
newX=newX+ganDeltaX[dir]——添加增量
newY=newY+ganDeltaY[dir]
如果(self.IsValidXY(self,newX,newY))那么
oRet.newX=newX
oRet.newY=newY
oRet.isValid=1
终止
返回奥利特
终止
正在更改

self.IsValidXY(self, newX, newY)


出于某种原因使它起作用。

这有帮助吗?这有点帮助。但正如你所看到的(我更新了我最初的帖子),我的问题更复杂。它在哪里崩溃,在什么错误信息下崩溃?它们实际上是一样的。使用“:”只是语法上的甜点,它会自动包含“self”参数
self:IsValidXY(newX, newY)