Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS类的多个实例_Javascript_Inheritance_Instance_Kineticjs - Fatal编程技术网

Javascript JS类的多个实例

Javascript JS类的多个实例,javascript,inheritance,instance,kineticjs,Javascript,Inheritance,Instance,Kineticjs,我在JavaScript中获得了以下“敌人”类: function Enemy(position, rotation) { this.name = null; this.enemyForm = new Kinetic.Rect({ x: 0, y: 0, width: 20, height: 20, fill: 'red', stroke: 'black', stro

我在JavaScript中获得了以下“敌人”类:

function Enemy(position, rotation) {
    this.name = null;

    this.enemyForm = new Kinetic.Rect({
        x: 0,
        y: 0,
        width: 20,
        height: 20,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 1
    });
    this.setX(position.posX);
    this.setY(position.posY);
    this.setRotation(rotation);
    this.setOffset(10, 10);
    this.add(this.enemyForm);
}

Enemy.prototype = new Kinetic.Group();
正如你所看到的,我扩展了一个动能组,因为敌人会有更多的动能元素,而不仅仅是一个矩形

现在,我创建该“类”的一些实例,并将它们添加到游戏层:

 var enemy1 = new Enemy({posX: 50, posY: 50}, 0);
 this.layer.add(enemy1);
 var enemy2 = new Enemy({posX: 100, posY: 100}, 0);
 this.layer.add(enemy2);
 var enemy3 = new Enemy({posX: 200, posY: 200}, 0);
 this.layer.add(enemy3);
问题:每个敌人都有“敌人3”的位置,而不是他们自己的位置。因此,每个敌人都将被吸引到位置“200200”。 现在,如果我在没有继承的情况下尝试此方法,效果很好:

function Enemy(position, rotation) {
    this.name = null;
    this.enemyForm = new Kinetic.Group();

    var rect = new Kinetic.Rect({
        x: 0,
        y: 0,
        width: 20,
        height: 20,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 1
    });
    this.enemyForm.setX(position.posX);
    this.enemyForm.setY(position.posY);
    this.enemyForm.setRotation(rotation);
    this.enemyForm.setOffset(10, 10);
    this.enemyForm.add(rect);
}

有谁能告诉我我遗漏了什么,以及为什么我不能用第一种方法得到单独的对象吗?

我从不看动力学组的代码。但如果代码使用闭包创建私有变量,则可能会出现此问题,如下所示:

Kinetic.Group = function(){
    var setX, getX;
    (function() {
       var X = 0;
       getX = function(){
          return X ;
       };
       setX = function(v){
          X = v;
       };
     })();

    this.setX = setX;
    this.getX = getX;
}
function Enemy(position, rotation) {

    // it create properties(and probably functions) on with Kinetic.Group depends
    // you could also pass x, y, rotation and offset in argument of this constructor
    Kinetic.Group.apply(this, [{}]);        

    this.name = null;
    this.enemyForm = new Kinetic.Rect({
        x: 0,
        y: 0,
        width: 20,
        height: 20,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 1
    });
    this.setX(position.posX);
    this.setY(position.posY);
    this.setRotation(rotation);
    this.setOffset(10, 10);
    this.add(this.enemyForm);
}

Enemy.prototype = new Kinetic.Group();
当您声明
敌方.prototype=new动能.Group()时仅创建了1个动力学组。当您调用
this.setX(position.posX)时
函数敌方(位置、旋转)
中,由于此函数在当前实例中不存在,它将在原型属性中查找该函数(与
动力学组相同,适用于所有
敌方
。构造函数创建的所有实例共享同一个变量
var X=0由闭包捕获

在第二种情况下,这种情况不会发生,因为您为每个
敌人创建了一个新的
Kinetic.Group

更新:(查看
Kinetic.Group的代码后

在代码中,我看到情况有所不同。每个
Kinetic.Group
维护一个
this.attrs={}
对于您的所有属性和
setX
getX
是在
Kinetic.Group.prototype
=>上定义的方法,当您使用
敌方.prototype=new Kinetic.Group();
时,您的所有敌方实例都共享一个
Kinetic.Group
实例的
attrs


恐怕您不能将继承与此代码一起使用。

我从未看过kinetic group的代码。但如果代码使用闭包创建私有变量,则可能会出现此问题,如下所示:

Kinetic.Group = function(){
    var setX, getX;
    (function() {
       var X = 0;
       getX = function(){
          return X ;
       };
       setX = function(v){
          X = v;
       };
     })();

    this.setX = setX;
    this.getX = getX;
}
function Enemy(position, rotation) {

    // it create properties(and probably functions) on with Kinetic.Group depends
    // you could also pass x, y, rotation and offset in argument of this constructor
    Kinetic.Group.apply(this, [{}]);        

    this.name = null;
    this.enemyForm = new Kinetic.Rect({
        x: 0,
        y: 0,
        width: 20,
        height: 20,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 1
    });
    this.setX(position.posX);
    this.setY(position.posY);
    this.setRotation(rotation);
    this.setOffset(10, 10);
    this.add(this.enemyForm);
}

Enemy.prototype = new Kinetic.Group();
当您声明
敌方.prototype=new dynamic.Group();
时,仅创建了一个动能.Group。当您调用
此.setX(position.posX);
函数敌方(位置、旋转)内部
,因为此函数在当前实例中不存在,它将在原型属性中查找该函数(对于所有
敌人
,使用相同的
Kinetic.Group
。由构造函数创建的所有实例共享闭包捕获的相同变量
var X=0;

在第二种情况下,这种情况不会发生,因为您为每个
敌人创建了一个新的
Kinetic.Group

更新:(查看
Kinetic.Group的代码后

在代码中,我看到情况有所不同。每个
Kinetic.Group
维护一个
this.attrs={}
对于您的所有属性和
setX
getX
是在
Kinetic.Group.prototype
=>上定义的方法,当您使用
敌方.prototype=new Kinetic.Group();
时,您的所有敌方实例都共享一个
Kinetic.Group
实例的
attrs


恐怕您不能将继承用于此代码。

我认为如果您想在javascript中拥有继承,您还应该在敌人的构造函数中调用组构造函数。请这样编写:

Kinetic.Group = function(){
    var setX, getX;
    (function() {
       var X = 0;
       getX = function(){
          return X ;
       };
       setX = function(v){
          X = v;
       };
     })();

    this.setX = setX;
    this.getX = getX;
}
function Enemy(position, rotation) {

    // it create properties(and probably functions) on with Kinetic.Group depends
    // you could also pass x, y, rotation and offset in argument of this constructor
    Kinetic.Group.apply(this, [{}]);        

    this.name = null;
    this.enemyForm = new Kinetic.Rect({
        x: 0,
        y: 0,
        width: 20,
        height: 20,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 1
    });
    this.setX(position.posX);
    this.setY(position.posY);
    this.setRotation(rotation);
    this.setOffset(10, 10);
    this.add(this.enemyForm);
}

Enemy.prototype = new Kinetic.Group();

我认为如果你想在javascript中继承,你也应该在敌人的构造函数中调用组构造函数

Kinetic.Group = function(){
    var setX, getX;
    (function() {
       var X = 0;
       getX = function(){
          return X ;
       };
       setX = function(v){
          X = v;
       };
     })();

    this.setX = setX;
    this.getX = getX;
}
function Enemy(position, rotation) {

    // it create properties(and probably functions) on with Kinetic.Group depends
    // you could also pass x, y, rotation and offset in argument of this constructor
    Kinetic.Group.apply(this, [{}]);        

    this.name = null;
    this.enemyForm = new Kinetic.Rect({
        x: 0,
        y: 0,
        width: 20,
        height: 20,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 1
    });
    this.setX(position.posX);
    this.setY(position.posY);
    this.setRotation(rotation);
    this.setOffset(10, 10);
    this.add(this.enemyForm);
}

Enemy.prototype = new Kinetic.Group();

将动力学子类化没有问题。组:

// a bunch of code from 
// http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.6.0.min.js

//optionally a config object can be passed to the constructor
//http://kineticjs.com/docs/Kinetic.Group.html
var Test = function(config){
  Kinetic.Group.call(this,config);
};
Test.prototype=Object.create(Kinetic.Group.prototype);

var test = new Test();

for(s in test){
  console.log(s);
}

将动力学子类化没有问题。组:

// a bunch of code from 
// http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.6.0.min.js

//optionally a config object can be passed to the constructor
//http://kineticjs.com/docs/Kinetic.Group.html
var Test = function(config){
  Kinetic.Group.call(this,config);
};
Test.prototype=Object.create(Kinetic.Group.prototype);

var test = new Test();

for(s in test){
  console.log(s);
}

你能展示一下你在
setX
setY
里面做了什么吗?这是一个动力学函数,不是我自己的。这个.setX>调用动力学组的“setX”。无论如何,在这种情况下应该不会有问题,因为第二个可以很好地使用相同的函数…最好不要使用
敌方.prototype=new dynamic.group()
,您可以继承,而无需在敌方主体中创建父对象的实例(请参见末尾的链接)can do'Kinetic.group.call(this)`将设置任何变量'this.something'作为敌方实例属性原型,在实例之间共享,但函数主体中的任何内容都是实例唯一的。How parent.call(this)works在这里被解释为:为什么要投否决票?请告诉我,我是否可以做些什么来改进我的问题。你能展示一下你在
setX
setY
中做了什么吗?这是一个动态函数,不是我自己的。这个.setX>调用“setX”在这种情况下,应该不会有问题,因为第二个可以使用相同的函数…最好不要使用
敌方.prototype=new-kinetic.group()
,您可以继承,而无需创建敌方身体中父对象的实例(见最后的链接)即可执行“kinetic.group.call”(this)`这会将任何变量“this.something”设置为敌方实例属性原型在实例之间共享,但函数体中的任何内容都是实例唯一的。如何调用Parent.call(this)这里解释了works:为什么要否决?请告诉我,我是否可以做些什么来改进我的问题。如果是这样的话,那么就没有办法将继承用于动能组了,对吗?代码在这里顺便说一下:在任何情况下,不应该为每个敌人创建一个新的动能组吗?毕竟,我只是扩展了来自Kinetic.Group的敌人类。@luschn:
在任何情况下都不应该为每个敌人创建一个新的Kinetic.Group
。不,当你使用
敌方.prototype=new Kinetic.Group();
时,只创建了一个。好吧,那我搞错了,该死。谢谢你的超级复杂的回答:)-