2017-08-17 99 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 
      //... 

     }); 

当你有许多重复的情况下,一个事件控制器这是非常有用的。