我有一个app-object构造函数,如下所示:
var app = function(loadedsettings) {
return {
init: function() {
this.loop();
},
loop: function() {
this.update();
window.requestAnimationFrame(this.loop);
},
update: function() {
//loop through settings and call update on every object.
},
settings: [
//array of settings objects, all with update methods.
]
};
}
Run Code Online (Sandbox Code Playgroud)
然后,当我这样做:
var theApp = app(settings);
theApp.init();
Run Code Online (Sandbox Code Playgroud)
我明白了:
Uncaught TypeError: Object [object global] has no method 'update'
Run Code Online (Sandbox Code Playgroud)
因为当调用requestAnimationFrame时,循环函数内的this-value设置为window.
有没有人知道如何调用requestAnimatinFrame并将'theApp'对象设置为this-value?
我是MongoDB(+ Mongoose)的新手.我有一个高分的集合,文档看起来像这样:
{id: 123, user: 'User14', score: 101}
{id: 231, user: 'User10', score: 400}
{id: 412, user: 'User90', score: 244}
{id: 111, user: 'User12', score: 310}
{id: 221, user: 'User88', score: 900}
{id: 521, user: 'User13', score: 103}
+ thousands more...
Run Code Online (Sandbox Code Playgroud)
现在我得到了前5名球员:
highscores
.find()
.sort({'score': -1})
.limit(5)
.exec(function(err, users) { ...code... });
Run Code Online (Sandbox Code Playgroud)
这很棒,但我还想做一个像" user12高分榜上有什么位置?"的查询.
这可能以某种方式通过查询实现吗?
我需要一个排行榜,用户存储得分和级别完整性的百分比.我只需要能够按分数排序并按分数排名.
使用Redis的排序集可以轻松存储用户的分数,如下所示:
ZADD leaderboard:gamemode1 100 user1
ZADD leaderboard:gamemode1 300 user2
Run Code Online (Sandbox Code Playgroud)
然而,我struggelig弄清楚,如何最好地保存属于的分数的百分比值100和300.
我应该做这样的事情:
ZADD leaderboard:gamemode1 100 user1:29.45
Run Code Online (Sandbox Code Playgroud)
其中:29.45是user1的分数的百分比100的gamemode1?我认为这会让以后更新user1的gamemode1得分变得复杂.
或者我应该创建一个哈希表作为簿记机制,存储所有用户的分数和所有游戏模式的百分比?
我对哈希表方法的关注是,如果我想用他们的分数和百分比值来显示约50个用户的排行榜,那么在拉出top50分数之后,我将不得不查询该哈希表50次以获得百分比ZRANGE.
有没有人对如何以一种好的方式构建它有任何意见?
我正在创建一个包含Cells的程序,为此我有一个Cell类和一个CellManager类.单元格以二维数组组织,Cell类管理器有两个int成员变量xgrid和ygrid,它们反映了数组的大小.
出于某种原因,我无法弄清楚,这些成员变量在程序执行过程中会发生变化.任何人都可以看到为什么会这样,或者可能指向我的方向去看.
使用的类和函数如下所示:
class Cell
{
public:
Cell(int x, int y);
}
---------------------------------
class CellManager
{
public:
CellManager(int xg, int yg)
void registercell(Cell* cell, int x, int y);
int getxgrid() {return xgrid;}
int getygrid() {return ygrid;}
private:
int xgrid;
int ygrid;
Cell *cells[40][25];
}
-----------------------
and CellManagers functions:
CellManager::CellManager(int xg, int yg)
{
CellManager::xgrid = xg;
CellManager::ygrid = yg;
}
void CellManager::registercell(Cell *cell, int x, int y)
{
cells[x][y] = cell;
}
Run Code Online (Sandbox Code Playgroud)
这是主要功能:
int main ()
{
const int XGRID …Run Code Online (Sandbox Code Playgroud) 假设您有一系列子挑战,如下所示:
subchallenges = [
{name: 'score',
result: 'gold'},
{name: 'time',
result: 'bronze'},
...and so on.... ]
Run Code Online (Sandbox Code Playgroud)
您需要找到最终得分,这与任何子挑战中的最低结果相等.排名层次结构
['gold', 'silver', 'bronze', 'none']
Run Code Online (Sandbox Code Playgroud)
所以在上面的示例数组中,如果它只包含那些子挑战对象,那么最终得分将等于青铜.您认为最好的方法是什么?