2015-12-10 48 views
0

我一直在学习很多关于原型和经典继承的知识,而且我正在尝试重写一些我写过的用于使用这些方法之一的意大利面条代码。设置涉及逻辑的函数构造函数属性的最佳方法

我遇到的问题是我不确定初始化构造函数的属性的最佳方法,因为它们中的一些需要一些逻辑来确定值。

我下面的例子是在它下面的spawnEnemy函数中使用的一个敌人构造函数。当我重写这个时,我想我可以使spawnEnemy成为原型的一个函数,并让它由我创建的对象继承。

我需要识别的第一个值是spawnDir,因为它用于设置敌人的x和y坐标。速度也是在一组值之间随机化的,颜色将在稍后重写时成为精灵,这将成为传入的对象(这不在我的问题的范围内)。

我在下面提供了我现有的代码,也许没有像我希望的那样的“干净”解决方案。我想学习最佳实践,我不禁觉得有一种解决方案,在某处使用继承并在实例化某种子类时运行一些附加方法。我只是无法概念化这个模型。

理想的解决方案

我不知道如果我解释上面的东西最好的方式,所以我会尽量简洁这里与我所期待的。我希望使用经典模型,最好是提供一个解决方案,使用构造函数和继承来设置从最终类实例化的对象的属性。

面条代码(是的,我知道,这是非常面条)

var Enemy = function(color, spawnDir, speed, w, h) { 

    // spawnDir is randomized and passed in as argument 
    // objects starting position is set based on what spawn direction was set 
    if (spawnDir == 0) { 
    this.x = -w; 
    } else if (spawnDir) { 
    this.x = GAME_WIDTH + w; 
    } 

    this.y = (GAME_HEIGHT/2) - (h/2); // objects y coordinate always set to very middle of screen 
    this.color = color;     // color or sprite image of enemy 
    this.spawnDir = spawnDir;    // spawn direction passed as argument 
    this.speed = speed;     // speed passed as argument, randomized 
    this.width = w;      // width passed as argument 
    this.height = h;      // height passed as argument 
    this.collision = 0;     // sets whether object has collided, 0 = default, incremenets with each collision detected 
    //this.id = generateRandomId(8);  // randomized id assigned to object as identifier (NOT USED CURRENTLY) 

    // called in the draw loop, renders object to canvas 
    this.draw = function(ctx) { 
    ctx.shadowColor = "black"; 
    ctx.shadowBlur = 5; 
    ctx.shadowOffsetX = 2; 
    ctx.shadowOffsetY = 2; 

    ctx.fillStyle = this.color; 
    ctx.fillRect(this.x, this.y, this.width, this.height); 
    }; 
}; 

// enemy spawn function 
var spawnEnemy = function() { 
    // if enemies array length is less than enemy reserve limit and spawn counter is set to 0, then success 
    if (enemies.length < enemyReserve && spawnCounter == 0) { 
    var t; 
    var x = Math.floor(Math.random() * 99 + 1); // randomizes spawn direction by generating random number between 0-100 
    if (x <= 50) {        // if the number is less than 50 then spawn is from the left side of the screen 
     t = 0;         // if the number is greater than 50 then spawn is from the right side of the screen 
    } else if (x > 50) { 
     t = 1; 
    } 
    var s = Math.floor(Math.random() * 3 + 1); // randomizes speed of the enemy 
    var enemy = new Enemy("purple", t, s, 40, 20); // instantiates new enemy object with some statis + dynamic arguments 

    spawnCounter = spawnRate;     // spawn counter reset back to default value set in global variables 
    enemies.push(enemy);      // adds the new object to enemies global array 
    } else if (spawnCounter != 0) { 
    spawnCounter--;        // if counter is not set to 0 then lowers counter value by 1 
    } 
}; 

回答

1

我喜欢在那里我定义构造函数中的属性,构造后的任何静态方法使用一个模式,然后静态方法之后的原型上的成员方法。它结束了看起来像这样:

​​

至于试图创建子类(种不同的敌人类型例如)看答案Subclassing a class with required parameters in JavaScript

+0

我是新来的理解时,功能添加到原型与构造函数本身,所以希望这不是一个愚蠢的问题。为什么不将Enemy.spawn添加到原型中,将其直接保存在Enemy构造函数中的好处是什么? (如果我正确理解这一点) – crispyfrybits

+0

添加到原型的函数处理对象的特定实例。添加到构造函数中的函数可能是用于创建新实例的工厂函数,也可能是函数并处理多个单独的实例。你的产卵功能似乎是一个工厂功能。 – kicken

+0

谢谢你,我正在看你链接的其他答案。 – crispyfrybits

相关问题