我是Lua的新手,并试图将事情分类.我试过这段代码:
function newCarousel(images)
local slideToImage = function()
print("ah!")
end
end
local testSlide = newCarousel(myImages)
testSlide.slideToImage()
Run Code Online (Sandbox Code Playgroud)
这给了我这个错误:
尝试索引本地"testSlide"(零值)...
为什么是这样?
此函数使用jQuery修改DOM元素的内容.我究竟做错了什么?
function updateScore() {
alert("Test score is: " + bucket.score);
$("#testScore").innerHTML = 'Current score is: + bucket.score';
}
Run Code Online (Sandbox Code Playgroud)
警报运行,但没有别的.我有一个<p>
id testScore
,但是当我运行该函数时它不会改变.为什么?
谢谢,Elliot Bonneville
运行此代码时,我从Python解释器中收到一个奇怪的错误:
def make_map():
map = [[Tile(0, 0) for col in range(MAP_WIDTH)] for row in range(MAP_HEIGHT)]
for x in range(MAP_WIDTH):
for y in range(MAP_HEIGHT):
map[x][y].tileType = round((libtcod.noise_perlin(noise2d,[y/MAP_WIDTH,x/MAP_HEIGHT])*100), 0)
Run Code Online (Sandbox Code Playgroud)
它在终端返回:
TypeError: 'builtin_function_or_method' object is unsubscriptable
Run Code Online (Sandbox Code Playgroud)
回溯也指向此功能:
def render_all():
global color_light_wall
global color_light_ground
#go through all tiles, and set their background color
for y in range(MAP_HEIGHT):
for x in range(MAP_WIDTH):
tileType = map[x][y].tileType
if tileType>30:
libtcod.console_set_back(con, x, y, color_dark_wall, libtcod.BKGND_SET )
else:
libtcod.console_set_back(con, x, y, color_dark_ground, libtcod.BKGND_SET )
#draw all objects in the …
Run Code Online (Sandbox Code Playgroud) 我有一个关于用Corona/Lua分层图像/按钮的问题.如果我在另一个按钮上创建一个按钮然后单击它,则会触发两个按钮的事件.我该如何防止这种情况?
谢谢,Elliot Bonneville
编辑:这是我创建按钮的方式:
button1 = display.newImage("button1.png")
button1:addEventListener("tap", Button1Call)
button2 = display.newImage("button2.png")
button2:addEventListener("tap", Button2Call)
Run Code Online (Sandbox Code Playgroud) 好的,所以我对以下Lua代码有一个奇怪的问题:
function quantizeNumber(i, step)
local d = i / step
d = round(d, 0)
return d*step
end
bar = {1, 2, 3, 4, 5}
local objects = {}
local foo = #bar * 3
for i=1, #foo do
objects[i] = bar[quantizeNumber(i, 3)]
end
print(#fontObjects)
Run Code Online (Sandbox Code Playgroud)
运行此代码后,对象的长度应为15,对吧?但不,这是4.这是如何工作的,我错过了什么?
谢谢,Elliot Bonneville.
我不确定如何用这句话,所以我无法搜索它.基本上,我有一个keydown()
绑定$(document)
.我想要show()
另一个div,并将所有keydown事件重新路由到此div并阻止在文档处理程序中触发.这甚至可能,或者我是否必须将所有主要的键绑定放在另一个div上并从那里开始工作?
为了澄清我的标题,我需要一种方法来确定一个对象不是String,Number,Boolean或任何其他预定义的JavaScript对象.想到的一种方法是:
if(!typeof myCustomObj == "string" && !typeof myCustomObj == "number" && !typeof myCustomObj == "boolean") {
Run Code Online (Sandbox Code Playgroud)
我可以查看是否myCustomObj
是一个对象,如下所示:
if(typeof myCustomObj == "object") {
Run Code Online (Sandbox Code Playgroud)
这只适用于原始值,因为这typeof new String("hello world") == "object")
是真的.
确定对象是否不是预定义JavaScript对象的可靠方法是什么?
将窗口中的空字符串指定为属性是否有任何缺点?
例如:
window[""] = function() {
console.log("foo");
}
(); // will log foo
Run Code Online (Sandbox Code Playgroud)
编辑:当然,这会打破内容以便澄清.你可能想要这个:
window[""] = function(prop) {
console.log(prop);
return prop;
}
Run Code Online (Sandbox Code Playgroud)
进一步编辑:上面的代码是各种IIFE.哎呀.这就是我不喜欢自动分号插入的原因:
window[""] = function() {
console.log("foo");
}();
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
// Initiate the font choosing lists.
var headerListWindow = Titanium.UI.createWindow();
var headerFontListData = [{title:"Row 1"},{title:"Row 2"},{title:"Row 3"}];
var headerFontList = Titanium.UI.createTableView({data:headerFontListData});
chooseHeader.addEventListener('click', function(e) {
headerListWindow.left=win.width;
var a = Titanium.UI.createAnimation();
a.left = 0;
a.duration = 1000;
headerListWindow.add(headerFontList);
headerListWindow.open(a);
});
headerFontList.addEventListener('click', function(e) {
var a = Titanium.UI.createAnimation();
a.left = win.width;
a.duration = 1000;
headerListWindow.close(a);
});
Run Code Online (Sandbox Code Playgroud)
这很好地滑动了一个窗口.当我headerListWindow
第一次打开时,一切都会出现,列表正常工作,窗口滑动得很好.close()
但是,当我在窗口中时,所有列表元素的名称都将恢复为"R".然后,在重新打开窗口时,列表元素完全消失.为什么是这样?
我可以在JavaScript类上创建私有成员,如下所示:
function Class(_foo, _bar) {
var foo = _foo, // private, yay
bar = _bar; // also private, also yay
}
Run Code Online (Sandbox Code Playgroud)
如何为此类实现通用的setter/getter方法?我想一种方法是使用eval
:
function Class(_foo, _bar) {
var foo = _foo,
bar = _bar;
this.get = function(property) {
return eval(property);
};
this.set = function(property, value) {
eval(property + " = " + value);
};
}
Run Code Online (Sandbox Code Playgroud)
但是,这个解决方案很难看.另一种方法是创建一个private
对象,然后访问该属性:
function Class(foo, bar) {
var private = {
foo: foo,
bar: bar
};
this.get = function(property) {
return private[property];
};
this.set …
Run Code Online (Sandbox Code Playgroud)