- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个与此类似的问题:LUA and Corona error: Attempt To Call Method ' ' (A Nil Value) - Driving Me Crazy
我有一个TCell类:
local TCell={};
local cell_mt = { __index=TCell };
function TCell.new(_contents_name,_x,_y)
...
local ncell=
{
...
};
function ncell:setup()
...
end
ncell:setup();
return setmetatable(ncell,cell_mt);
end
return TCell;
cells[ind1][ind2]=cells[ind3][ind4]
reset_metatable=function(target)
return setmetatable(target,cell_mt);
end;
cells[ind1][ind2]=cells[ind3][ind4];
cells[ind1][ind2]=cells[ind1][ind2]:reset_metatable();
-----------------------------------------------------------------------------------------
--
-- Main Cycle
--
-----------------------------------------------------------------------------------------
local storyboard = require( "storyboard" )
local scene = storyboard.newScene()
-- include Corona's "physics" library
local physics = require "physics"
--control_circle=display.newImageRect(C.INTERFACE_DIR..C.INTERFACE_CONTROL_CIRCLE or C.EMPTY_IMAGE,C.CARS_W,C.CARS_W,true);
local events_added=false;
stage_frames=0;
physics.start(); physics.pause()
physics.setGravity( 0,0);
local PCar=require("TCar")
local PBiped=require("TBiped");
local PCell=require("TCell");
local control_circle_len;
local cells={};
local cells_w,cells_h;
local wshift,hshift=-(C.SCREEN_THEORETICAL_W-C.SCREEN_W)/2,-(C.SCREEN_THEORETICAL_H-C.SCREEN_H)/2;
--------------------------------------------
-- forward declarations and other locals
-----------------------------------------------------------------------------------------
-- BEGINNING OF YOUR IMPLEMENTATION
--
-- NOTE: Code outside of listener functions (below) will only be executed once,
-- unless storyboard.removeScene() is called.
--
-----------------------------------------------------------------------------------------
local wsells,hcells=(C.SCREEN_W-C.SCREEN_W%C.LANDSCAPE_CELL_W)/C.LANDSCAPE_CELL_W+1,(C.SCREEN_H-C.SCREEN_H%C.LANDSCAPE_CELL_H)/C.LANDSCAPE_CELL_H+1;
local dfd=false;
local function manage_cells(cmx,cmy)
--print("#",cmx,cmy);
if((cmx==0 and cmy==0) or dfd)
then
return;
end
local cells_shift_w,cells_shift_h=0,0;
if(cmx>=0)
then
for i=cells_w,1,-1
do
if(cells[i][1]:out_of_borders_w()==true)
then
cells_shift_w=cells_shift_w+1;
else
break;
end
end
else
for i=1,cells_w,1
do
if(cells[i][1]:out_of_borders_w()==true)
then
cells_shift_w=cells_shift_w-1;
else
break;
end
end
end
if(cmy>=0)
then
for i=cells_h,1,-1
do
if(cells[1][i]:out_of_borders_h()==true)
then
--[[cells[1][i].contents.rotation=45;
dfd=true;]]
cells_shift_h=cells_shift_h+1;
else
break;
end
end
--return;
else
for i=1,cells_h,1
do
if(cells[1][i]:out_of_borders_h()==true)
then
cells_shift_h=cells_shift_h-1;
else
break;
end
end
end
--print("~",cells_shift_w,cells_shift_h);
local stx,finx,sty,finy=1,cells_shift_w,1,cells_shift_h;
if(cmx<0)
then
stx=cells_w+cells_shift_w;
finx=cells_w;
end
if(cmy<0)
then
sty=cells_h+cells_shift_h;
finy=cells_h;
end
for i=stx,finx,1
do
for j=1,cells_h,1
do
if(cells[i][j])
then
cells[i][j]:destroy();
cells[i][j]=nil;
end
end
end
for i=1,cells_w,1
do
for j=sty,finy,1
do
if(cells[i][j])
then
cells[i][j]:destroy();
cells[i][j]=nil;
end
end
end
stx,finx,sty,finy=cells_w-cells_shift_w-1,1,cells_h-cells_shift_h-1,1;
local itx,ity=-1,-1;
if(cmx>=0)
then
stx=cells_shift_w+1;
finx=cells_w;
itx=1;
print(stx,finx);
end
if(cmy>=0)
then
sty=cells_shift_h+1;
finy=cells_h;
ity=1;
end
for i=stx,finx,itx
do
for j=1,cells_h,1
do
if(cells_shift_w~=0)
then
if(j==1)
then
print(i,1,"to",i-cells_shift_w,1);
end
cells[i-cells_shift_w][j]=cells[i][j];
cells[i-cells_shift_w][j]=cells[i-cells_shift_w][j].reset_metatable(cells[i][j]);
print("++",cells[i-cells_shift_w][j].contents.width);
cells[i][j]:destroy();
cells[i][j]=nil;
if(j==1)
then
print(i-cells_shift_w,1,"width is",cells[i-cells_shift_w][1].contents.width);
if(i==7)
then
cells[6][1].debug=true;
print("debug is set");
end
end
end
end
end
for i=1,cells_w,1
do
for j=sty,finy,ity
do
if(cells_shift_h~=0)
then
print("?this?",i,j);
cells[i][j-cells_shift_h]=cells[i][j];
cells[i][j]:destroy();
cells[i][j]=nil;
end
end
end
for i=1,cells_w,1
do
for j=1,cells_h,1
do
if(cells[i][j]==nil)
then
print("*new",i,j);
cells[i][j]=PCell.new(C.LANDSCAPE_DICTIONARY(nil,1),(i-1)*C.LANDSCAPE_CELL_W+C.LANDSCAPE_CELL_W/2-wshift-camera:getX(),(j-1)*C.LANDSCAPE_CELL_H+C.LANDSCAPE_CELL_H/2-hshift-camera:getY());
end
end
end
end
local function stage_main_frame()
print("START NOW",6,1,"width is",cells[6][1].contents.width);
local old_camera_x,old_camera_y=camera._view.x,camera._view.y;
if(dfd==false)
then
--print("&");
camera._view.x=camera._view.x-stage_frames;
end
local cmx,cmy=-(camera:getX()-old_camera_x),-(camera:getY()-old_camera_y);
manage_cells(cmx,cmy);
stage_frames=stage_frames+1;
print("NOW",6,1,"width is",cells[6][1].contents.width);
return function(event)
end
end
-- Called when the scene's view does not exist:
function scene:createScene( event )
local group = self.view
end
-- Called immediately after scene has moved onscreen:
function control_player(event)
car1:get_touch(event);
end
function scene:enterScene( event )
camera:newLayer("land",1);
camera:newLayer("bipeds",1);
camera:newLayer("cars",1);
i=1;
while((i-1)*C.LANDSCAPE_CELL_W-C.LANDSCAPE_CELL_W/2-wshift<=C.SCREEN_W)
do
j=1;
cells[i]={};
while((j-1)*C.LANDSCAPE_CELL_H-C.LANDSCAPE_CELL_H/2-hshift<=C.SCREEN_H)
do
cells[i][j]=PCell.new(C.LANDSCAPE_DICTIONARY(nil,1),(i-1)*C.LANDSCAPE_CELL_W+C.LANDSCAPE_CELL_W/2-wshift-camera:getX(),(j-1)*C.LANDSCAPE_CELL_H+C.LANDSCAPE_CELL_H/2-hshift-camera:getY());
j=j+1;
end
i=i+1;
end
cells_w,cells_h=#cells,#cells[1];
local group = self.view
physics.start();
physics.setPositionIterations( 1 )
if(events_added==false)
then
events_added=true;
Runtime:addEventListener("touch",control_player);
Runtime:addEventListener("enterFrame",stage_main_frame);
end
end
-- Called when scene is about to move offscreen:
function scene:exitScene( event )
local group = self.view
physics.stop()
end
-- If scene's view is removed, scene:destroyScene() will be called just prior to:
function scene:destroyScene( event )
local group = self.view
package.loaded[physics] = nil
physics = nil
end
-----------------------------------------------------------------------------------------
-- END OF YOUR IMPLEMENTATION
-----------------------------------------------------------------------------------------
-- "createScene" event is dispatched if scene's view does not exist
scene:addEventListener( "createScene", scene )
-- "enterScene" event is dispatched whenever scene transition has finished
scene:addEventListener( "enterScene", scene )
-- "exitScene" event is dispatched whenever before next scene's transition begins
scene:addEventListener( "exitScene", scene )
-- "destroyScene" event is dispatched before view is unloaded, which can be
-- automatically unloaded in low memory situations, or explicitly via a call to
-- storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( "destroyScene", scene )
-----------------------------------------------------------------------------------------
return scene
local TCell={};
local cell_mt = { __index=TCell };
function TCell.new(_contents_name,_x,_y)
--print(camera.x);
if(_x==nil)
then
_x=0;
end
if(_y==nil)
then
_y=0;
end
camera:newLayer( "abacaba", 1 );
local ncell=
{
contents_name=_contents_name;
contents;
sequence_data;
sheet_data;
debug=false;
--[[main_frame;
main_frame_handler=function(self)
main_frame=function(event)
if(self.contents.x+self.contents.width/2<0 or self.contents.y+self.contents.height/2<0 or self.contents.x-self.contents.width/2>C.SCREEN_W or self.contents.y-self.contents.height/2>C.SCREEN_H)
then
--self:destroy();
end
end
return main_frame;
end;]]
clear=function(self)
if(self.debug==true)
then
print("CLEAR!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n\n\n");
end
--print(self.contents);
self.contents:removeSelf();
camera:removeObject("land",self.contents);
end;
show=function(self,_contents_name,__x,__y)
if(self.debug==true)
then
print("SHOW!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n\n\n\n");
end
self.contents_name=_contents_name;
if(self.contents_name~=C.EMPTY_IMAGE)
then
self:set_sequence_data();
end
if(self.contents_name~=C.EMPTY_IMAGE)
then
self.sheet_data=graphics.newImageSheet(self.contents_name,C.LANDSCAPE_SHEET_DATA);
self.contents=display.newSprite(self.sheet_data,self.sequence_data);
else
self.contents=display.newImageRect(C.EMPTY_IMAGE,C.LANDSCAPE_CELL_W,C.LANDSCAPE_CELL_H,true);
self.sequence_data=nil;
end
self.contents.x=__x;
self.contents.y=__y;
camera:addObject("land",self.contents);
end;
out_of_borders_w=function(self)
--print(self.contents.width);
return self.contents.x+self.contents.width/2-camera:getX()<C.LANDSCAPE_CELLS_LEFT_BORDER-C.LANDSCAPE_CELL_W or
self.contents.x-self.contents.width/2-camera:getX()>C.LANDSCAPE_CELLS_RIGHT_BORDER+C.LANDSCAPE_CELL_W;
end;
out_of_borders_h=function(self)
return self.contents.y+self.contents.height/2-camera:getY()<C.LANDSCAPE_CELLS_UPPER_BORDER-C.LANDSCAPE_CELL_H or
self.contents.y-self.contents.height/2-camera:getY()>C.LANDSCAPE_CELLS_LOWER_BORDER+C.LANDSCAPE_CELL_H;
end;
reset_metatable=function(target)
return setmetatable(target,cell_mt);
end;
set_sequence_data=function(self)
local px=math.floor(((_x)%C.LANDSCAPE_SHEET_DATA.sheetContentWidth)/C.LANDSCAPE_SHEET_DATA.width)+1;
local py=math.floor(((_y)%C.LANDSCAPE_SHEET_DATA.sheetContentHeight)/C.LANDSCAPE_SHEET_DATA.height)+1;
self.sequence_data=
{
{ name = "only", start=C.LANDSCAPE_SHEET_DATA.sheetContentWidth/C.LANDSCAPE_SHEET_DATA.width*(py-1)+px, count=1 }
};
end;
destroy=function(self)
--Runtime:removeEventListener("enterFrame",self.main_frame);
--Runtime:removeEventListener("enterFrame",self.main_frame_handler);
self:clear();
if(index~=nil)
then
--destroy_cell(index);
end
end
};
function ncell:setup()
self:show(self.contents_name,_x,_y);
end
ncell:setup();
return setmetatable(ncell,cell_mt);
end
return TCell;
最佳答案
我相信您可能会误解Lua中的变量分配。所有变量实际上都是对象的引用/指针。当你写的时候:
cells[ind1][ind2]=cells[ind3][ind4]
关于lua - 对象分配Lua,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15716914/
如何从 a.lua 传递值至 b.lua ? 让我们说在我的 a.lua我有这个变量代码。 local value = "Hello WOrld!" director:changeScene ("b"
我有一个使用命令行解释器运行的 lua 脚本,该脚本需要从文件加载表。 该表存储如下: create_object_action = { reflexive = true, which
我通过静态链接的方式在我的项目中嵌入了 Win32 上的 Lua(不,我不能切换到 DLL)。我想捆绑更多使用 native 代码的 Lua 扩展 - 而不仅仅是纯 .lua 文件。具体来说,我想捆绑
我需要一些帮助来解析 lua 文件的命令行。我正在执行一个 lua 文件,该 lua 文件有一个命令“dofile(2nd.lua-file)”,但是,我想通过第一个 lua 文件将一些参数传递给第二
这是我的代码示例: listOfPeople = {} listOfPeople["test"] = "hello" listOfPeople = nil “hello”字符串是否丢失并形成内存泄漏?
在一些源代码中,我看到了“Underscore.lua”模块的用法。 _ = require 'underscore' 描述如下: Underscore.lua is a Lua library th
在一些源代码中,我看到了“Underscore.lua”模块的用法。 _ = require 'underscore' 描述如下: Underscore.lua is a Lua library th
我一直在编程 io.write("How many languages do you speak?\n") answer = io.read() if (answer == 1) then io.wr
这个问题在这里已经有了答案: Getting multiple values from a function without creating a variables in LUA (2 个答案)
在阅读 Lua manual 时我遇到了这部分: 函数调用和赋值都可以以左括号开头。这种可能性导致了 Lua 语法中的歧义。考虑以下片段: a = b + c (print or io.write)(
假设我有以下循环: for name in poll() do if name == "quit" then return 0 end end "quit" 字符串是否
Pandoc 通过其 --lua-filter 参数原生支持 lua 过滤器。 但是,我想知道它使用的是什么版本的 lua,以及是否可以将 lua 模块(我相信它依赖于 C 代码)导入其中。 这是我调
这种语言是面向对象的语言吗? 它经常用作OO语言吗? 最佳答案 Lua 完全有能力 prototype-based类似于 JavaScript 的面向对象编程。 Prototype-based pro
我想从 C++ 传递一个 Lua 脚本(Lua 解释器可以处理的代码)并取回结果。 我在网上查看,但找不到任何可以帮助我的示例。我可以从 C++ 调用 Lua 函数,但这需要您使用 Lua 函数创建一
我正在阅读“在 Lua 中编程”,但我不明白这段代码中 Lua 中函数的行为: function newCounter () local i = 0 return function () --
我最近一直在查找 Lua 中的链表,并有一个简单的问题,到目前为止我还没有找到答案 local head = nil head = {next = head, value = "d"} head =
我知道有tonumber()函数,但是问题是我需要转换较大的数字,例如1000100110100011111010101001001001001100100101。我可以自己写,但是有没有办法将其集成
是否可以在 Lua 中对多个值执行算术运算。 我在 Windows 5.1.4 上使用 Lua。 目前我必须将多个值放入一个表中,然后解压缩它们,我希望能够跳过这一步。 是否可以。 这是我目前拥有的:
有什么区别吗 local splitPathFileExtension = function (res) end 和 function splitPathFileExtension(res) end
在下面的代码中,谁能解释一下 b,a = a,b 内部是如何工作的? -- Variable definition: local a, b -- Initialization a = 10 b = 3
我是一名优秀的程序员,十分优秀!