0

Straight forward: How do I change the position of Kinetic.Group children in the array? It is really important for me that I can alter the array and the position of each children.

I tried to add a child with Array.splice on a specific position but the library crashed. So I sticked to the native .add() function, took the child from the end of the array and spliced it on first place afterwards:

mapTilesContainer.add(image);
var tempChild = mapTilesContainer.children[mapTilesContainer.children.length - 1];
// delete it from the last place
mapTilesContainer.children.splice(mapTilesContainer.children.length - 1, 1);
// add it to the first place
mapTilesContainer.children.splice(0, 0, tempChild);

which somehow works, but if I then want to destroy a child regularly with .destroy() it crashes again:

mapTilesContainer.children[8].destroy();

Telling me: Uncaught TypeError: Cannot call method 'getLayer' of undefined in kinetic.js:2

Adding and destroying without messing with splice works though. Any ideas?

4

1 に答える 1

0

--- 非常にまれな要件 ---

キネティックの内部配列をいじる代わりに、コードに回避策はありませんか?

ライブラリ スクリプトの内部をいじるのは、ほとんどの場合、悪い考えです。

そうは言っても、kinetic の Container.js コードをざっと見てみました。

https://github.com/ericdrowell/KineticJS/blob/master/src/Container.js

これは内部の add child メソッドです:

add: function(child) {
    var children = this.children;
    this._validateAdd(child);
    child.index = children.length;
    child.parent = this;
    children.push(child);
    this._fire('add', {
        child: child
    });
    // chainable
    return this;
},

child.indexプロパティは、子の配列内の子の位置に設定されることに 注意してください。

各 child.index を適切にリセットすることが、Kinetic の children 配列を操作するための鍵となるでしょう。

Container.js の 237 行目には、次の内部メソッドがあります。

_setChildrenIndices: function() {
    var children = this.children, len = children.length;
    for(var n = 0; n < len; n++) {
        children[n].index = n;
    }
},

このメソッドは、キネティックで子配列のインデックスをリセットするために使用される場合があります。

children 配列をシャッフルしてから、独自のバージョンの を実行すると、_setChildrenIndex独自のアプリが爆発しない可能性があります。

また:

ライブラリ スクリプトの内部をいじるのは、ほとんどの場合、悪い考えです。

于 2013-09-14T03:06:17.607 に答える