简体   繁体   English

无法在舞台上使用GSAP内部函数定位MovieClip(Animate CC)

[英]Can't use GSAP inside function to target MovieClip on stage (Animate CC)

I'm an HTML5 newbie and I was wondering if someone could tell me what I'm doing wrong here. 我是HTML5新手,我想知道是否有人可以告诉我我在做什么错。 I would like to be able to use GSAP to animate a vector file I'd add to the stage and would need to be able to make it animate when I call a function, however when I try to do this I keep getting cannot tween a null object , but if it's not wrapped in a function the animation plays fine. 我希望能够使用GSAP设置要添加到舞台上的矢量文件的动画效果,并且在调用函数时需要使其具有动画效果,但是当我尝试执行此操作时,我一直无法进行补间null对象 ,但如果未将其包装在函数中,则动画可以正常播放。

I created a new HTML5 canvas to see if the issue persisted and it did, so this is what I did: 我创建了一个新的HTML5画布,以查看问题是否仍然存在,并且确实如此,所以我要做的是:

  1. Added a symbol to a blank HTML5 canvas, made it a Movie Clip and drew a circle. 在空白的HTML5画布中添加了一个符号,使其成为影片剪辑并画了一个圆圈。 I called the instance mcThing 我将实例称为mcThing
  2. In the Timeline, I selected the first frame and went into Actions 在时间轴中,我选择了第一帧并进入动作
  3. I wrote: 我写:

     function playAnimation() { TweenMax.to(this.mcThing, 3, {y:500}); } playAnimation(); 
  4. When testing in Chrome, I get cannot tween a null object . 在Chrome中进行测试时,我无法补间null对象 If I reference it as mcThing (omitting the this. I instead get mcThing is not defined . 如果我将其引用为mcThing (省略了this.相反,我未定义mcThing
  5. If I then remove the function and just have this: 如果我然后删除该功能,只是拥有这个:

     TweenMax.to(this.mcThing, 3, {y:500}); 

    It plays fine, but now I can't call it when I need to. 它可以正常播放,但是现在我无法在需要时调用它。

Some context: 一些背景:

Essentially what I currently have is a WebSocket listening for messages. 我目前基本上拥有的是一个侦听消息的WebSocket。 When it receives a message, it's added to the queue. 收到消息后,它将添加到队列中。 I am trying to get it to play an animation and insert the text from that message. 我试图让它播放动画并从该消息中插入文本。 The text itself should be okay: I used CreateJS to instantiate a text in the code and TweenMax works there, the problem is animating shapes/drawings. 文本本身应该可以:我使用CreateJS实例化代码中的文本,TweenMax在那里工作,问题在于动画形状/图形。 I suppose I could instantiate all the shapes in the code itself and TweenMax would work then but I don't think this is realistic as the animation/shapes are fairly complex, so I'm trying to target the stage. 我想我可以实例化代码本身中的所有形状,然后TweenMax可以工作,但是我不认为这是现实的,因为动画/形状相当复杂,所以我尝试以舞台为目标。 The animation would play out, stop, then the message would be removed from the queue and the next one would play (same animation, different text). 动画将播放,停止,然后从队列中删除该消息,然后播放下一条消息(相同的动画,不同的文本)。

I think this is a scope issue, but I'm not sure what I need to change. 我认为这是一个范围问题,但是我不确定需要更改什么。 Any help would be much appreciated! 任何帮助将非常感激!

This issue is because of the scope. 此问题是由于范围。 Your playAnimation is not scoped to this , so it is called in the global scope. 您的playAnimation不在this范围内,因此在全局范围内被调用。

Try this: 尝试这个:

this.playAnimation = function() {
    TweenMax.to(this.mcThing, 3, {y:500});
}
this.playAnimation();

Putting your mcThing into the function scope would also work: 将您的mcThing放入功能范围也可以:

var thing = this.mcThing;
function playAnimation() {
    TweenMax.to(thing, 3, {y:500});
}
playAnimation();

Or you could scope the function call itself! 或者,您可以限定函数本身的作用域!

function playAnimation() {
    TweenMax.to(this.mcThing, 3, {y:500});
}
playAnimation.call(this);

There are lots of ways to get around it once you understand how the scoping works. 一旦了解了作用域的工作原理,就有很多方法可以解决它。 I recommend the first approach. 我建议第一种方法。

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM