2017-08-17 97 views
0

我正在研究createjs遊戲中的圖像被容納在容器內。我想將圖像補間到屏幕上的某個位置,並將圖像切換到另一張圖像。經過幾秒鐘後,我想從畫布/屏幕中刪除新圖像。CreateJS中的補間函數

目前,我將一個(evt)傳入函數,但其​​他遊戲/示例都不會打擾這部分?

它在第一個.call函數中工作,但在.wait和第二個.call之後我註釋掉的部分不起作用。突然,TheThingBeingTweenedundefined

任何提示在正確的方向應該是有幫助的。

createjs.Tween 
     .get(inkContainer, {onChange: onInkContainerTweenChange}) 
     .to({ 
       y: playerContainer.y + (Math.random() * 200 - 100), 
       x: playerContainer.x + (Math.random() * 200) 
      }, 8000) 
     .call(function (evt) { 
    var theThingBeingTweened = evt.target; 

    //self.stage.removeChild(theThingBeingTweened); 

    var theContainer = theThingBeingTweened.parent; 
    theContainer.removeChild(theThingBeingTweened); 

    splatContainer = new createjs.Container(); 
    splat = new 
    createjs.Bitmap(queue.getResult("splat")); 
    splatContainer.addChild(splat); 
    splatContainer.x = theThingBeingTweened.x; 
    splatContainer.y = theThingBeingTweened.y; 
    theContainer.addChild(splatContainer); 
}); 

//.wait(3000) 
//.call(function (evt) { 
// var theThingBeingTweened = evt.target; 
// self.stage.removeChild(theThingBeingTweened); 
//}); 

回答

0

雖然確切原因theThingBeingTweenedundefined不清楚,你可以很容易地通過聲明你調用鏈的範圍theThingBeingTweened外面繞過這個限制,那麼就不會與undefined價值進行重新初始化在第二個電話。

var theThingBeingTweened; 

createjs.Tween 
     .get(inkContainer, {onChange: onInkContainerTweenChange}) 
     .to({ 
       y: playerContainer.y + (Math.random() * 200 - 100), 
       x: playerContainer.x + (Math.random() * 200) 
      }, 8000) 
     .call(function (evt) { 
    theThingBeingTweened = evt.target; 

    //self.stage.removeChild(theThingBeingTweened); 

    var theContainer = theThingBeingTweened.parent; 
    theContainer.removeChild(theThingBeingTweened); 

    splatContainer = new createjs.Container(); 
    splat = new 
    createjs.Bitmap(queue.getResult("splat")); 
    splatContainer.addChild(splat); 
    splatContainer.x = theThingBeingTweened.x; 
    splatContainer.y = theThingBeingTweened.y; 
    theContainer.addChild(splatContainer); 
}).wait(3000).call(function (evt) { 
    self.stage.removeChild(theThingBeingTweened); 
}); 
0

call方法做導致調度的事件。沒有event參數,所以沒有event.target

相反,您可以將參數傳遞給調用方法(第2個參數)。

createjs.Tween 
    .get(inkContainer, {onChange: onInkContainerTweenChange}) 
    .to({ 
      y: playerContainer.y + (Math.random() * 200 - 100), 
      x: playerContainer.x + (Math.random() * 200) 
     }, 8000) 
    .call(function (thing) { 
     // The thing being tweened is the first argument 
    }, [inkContainer, otherArg], optionalScope); 

需要注意的是,如果它事件,則目標將是補間實例,而不是東西補間的目標。當您使用addEventListeneron來支持Tweens的支持事件時(這只是「更改」)。 http://www.createjs.com/docs/tweenjs/classes/Tween.html#event_change

1

您還可以通過覆蓋或添加一個屬性

例如擴大你的對象:

// It coud be instance or singleton it can knows everything to handle your issue 
var imageHandler = new ImageHandlerClass(); 

Object.defineProperty(inkContainer, "imageHandler", { value:imageHandler , configurable: true, writable: true, enumerable: true }); 

createjs.Tween 
     .get(inkContainer) 
     .to({ 
       y: playerContainer.y + (Math.random() * 200 - 100), 
       x: playerContainer.x + (Math.random() * 200) 
      }, 8000) 
     .call(function (evt) { 
      var linkToImageHandler = evt.target.imageHandler; 
      // Do something with image using imageHandler defined erlier 
      //... 

     }); 

當你有許多重複的情況下,一個事件控制器這是非常有用的。