JS Game(Duck Hunt)将.png文件添加到特定级别

时间:2015-11-09 16:47:15

标签: javascript

我试图在经典游戏Duck Hunt中为不同级别添加不同的角色。我得到了原始的源代码形式git hub(https://github.com/MattSurabian/DuckHunt-JS),现在有3个级别,并通过将它们添加到css为级别创建了不同的.png文件,但是我不确定在JS脚本中调整代码的位置每个级别有一个不同的charcater?我猜我需要使用每个级别ID的if语句。

这是它在JS中拾取鸭子(duckA / B)的地方

{var e=t%2===0?"duckA":"duckB";this.duckMax++,this.liveDucks.push(new Duck(this.duckMax.toString(),e,this.level.speed,this.playfield).fly())}},
killDuck:function(t){this.levelStats.ducksKilled+=1,this.liveDucks=_(this.liveDucks).reject(function(e){return e.id===t.id}),
0===this.liveDucks.length&&this.playfield.trigger("wave:end",this.curWave)},
drawDucks:function(){var t="",e=this.level.ducks*this.curWave-this.levelStats.ducksKilled;e=e>25?25:e;

这些是级别

levels=[
{id:1,title:"Level 1",waves:1,ducks:10,pointsPerDuck:100,speed:2,bullets:15,time:25},
{id:2,title:"Level 2",waves:1,ducks:10,pointsPerDuck:150,speed:4,bullets:15,time:20},
{id:3,title:"Level 3",waves:1,ducks:10,pointsPerDuck:200,speed:6,bullets:15,time:18}],

我希望能够在第2级(id 2)上获得duckC / D,在第3级(第3级)获得duckE / F ....任何人都可以帮忙吗?

编辑:

所以我调整了duckhunt.min.js中的代码部分如下,但是只加载了默认的两个鸭子?知道为什么吗?

Duck.prototype.hatch=function(){
$('<div id="'+this.id+'" class="duck1 '+this.className+'"></div>').appendTo(this.game),
this.DOM=$("#"+this.id),
this.bindEvents()
$('<div id="'+this.id+'" class="duck2 '+this.className+'"></div>').appendTo(this.game),
this.DOM=$("#"+this.id),
this.bindEvents()},

releaseDucks:function(){
for(var t=0;t<this.level.ducks;t++)
{var e=t%2===0?"duckA":"duckB";
this.duckMax++,
this.liveDucks.push(new Duck(this.duckMax.toString(),e,this.level.speed,this.playfield).fly())
}
{var e=t%2===0?"duckC":"duckD";
this.duckMax++,
this.liveDucks.push(new Duck(this.duckMax.toString(),e,this.level.speed,this.playfield).fly())
}
},

1 个答案:

答案 0 :(得分:1)

您可以在duck.js中看到正在创建鸭子的DOM元素

Duck.prototype.hatch = function(){

    $('<div id="'+this.id+'" class="duck '+this.className+'"></div>').appendTo(this.game);
    this.DOM = $("#"+this.id);
    this.bindEvents();
};

Duck的构造函数通过了“duckA&#39;或者&#39; duckB&#39;通过this.className获取游戏内置鸭子的两种变体,因此请修改duckhunt.js以反映级别数据:

releaseDucks : function(){
    for(var i=0;i<this.level.ducks;i++){
        var duckClass = "duck"+this.level.id; //This is different!
        this.duckMax++;
        this.liveDucks.push(new Duck(this.duckMax.toString(),duckClass,this.level.speed,this.playfield).fly());
    }
},

然后,为您的课程duck1duck2duck3添加标题,并将它们设置为原始回购样式!