【问题标题】:How to pass a variables to a function called by ENTER_FRAME without override如何将变量传递给由 ENTER_FRAME 调用的函数而不覆盖
【发布时间】:2015-08-25 01:12:01
【问题描述】:

更新

我有一个大杂乱的代码,我想通过实例化一次CubeEaseOut Class 来加速。我通过 ff 做到这一点:

var myClass = new CubeEaseOut()

myClip.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
myClip2.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
myClip3.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);

function onMouseOver(e:Event){
    //made changes here
    myClass.initializer(e.currentTarget, ["scaleX",1.5,"height",200]); 
}

因此,每当我在任何剪辑上移动鼠标时,都会调用 CubeEaseOut 类中名为 initializer() 的函数。

CubeEaseOut 类中,我有一个代码可以在ENTER_FRAME 上进行一次午餐,然后定期调用函数animateThis()

package;

import goes here...

class CubeEaseOut extends Sprite {

    var here...

    public function new(){
        super();
        addEventListener(Event.ENTER_FRAME, animateThis);
    }

    public function initializer(mc:Dynamic, vars:Array<Dynamic>()){

        vars[1] //this is a Float with value 1.5
        mc[vars[0]] //is mc.scaleX

        //some other code
        //notice many variables received here are needed by other functions
        //but if I make this public, or placed outside functions, every call
        //on this function will overwrite previous values

        //other variables here

        var sp:Float = (vars[1] - mc[vars[0]])/50
    }
    private function animateThis(e:Event){
            //some other code
            //many variables from initialize() function is needed here

            iter++
            mc[vars[1]] += sp*iter

    }

}

问题是,要使animateThis() 工作,它需要来自initializer() 的变量。我如何将变量从initializer() 传递到animateThis() 而不会在下次调用initializer() 时覆盖以前的变量?

尝试:

  1. 我尝试使用全局变量,但这会使事情变得一团糟。
  2. 将 ENTER_FRAME 放在 initializer() 函数中,但事实证明 ENTER_FRAME 将被称为我的时间 - CPU 密集型。
  3. 创建了该类的多个实例 - 也是 CPU 密集型的。
  4. 尝试使用 this.aVar,aVar 会被下一次调用覆盖。

其他信息:

我正在做的完整代码和这个类似

Animation code not fired when mouse is out Clip1 but mouse is inside clip 2

但我现在正在实现类和 ENTER_FRAME。

【问题讨论】:

  • 是的,我会的,我正在使用您的建议完成我的代码移植。结果出来后我会回来的。
  • @GurtejSingh 用户提出问题会收到有关他们收到的答案的通知。我们不必向网站发送垃圾邮件“看看我的答案!”厘米。
  • @null 对不起!我只是确保用户确实看到了我发布的答案。会小心前行。道歉。

标签: performance actionscript-3 actionscript haxe openfl


【解决方案1】:

更新了下面的代码。据我了解,您想使用同一类CubeEaseOut 来管理多个动画。我尝试过(未测试)的是创建一个传递给此类的数据对象数组。在每个对象中,我存储传递的影片剪辑,以及为每个影片剪辑传递的数据。每次调用初始化程序时,它都会检查该影片剪辑的数据是否已存在于数组中。如果没有,它会将影片剪辑及其数据添加到数组中。如果它找到影片剪辑,它只是将对象分配为数组中的当前动画对象,并且不会覆盖它的数据。请注意,这可能会限制动画一次只能运行一个影片剪辑。

您可以选择在输入帧方法中运行循环并为数组中的所有对象执行动画。

import goes here...

class CubeEaseOut extends Sprite {

    private var objectsToAnimate:Array = new Array(); 
    private var currentMcData:Object;    

    public function new(){
        super();
        addEventListener(Event.ENTER_FRAME, animateThis);
    }

    public function initializer(mc:Dynamic, vars:Array<Dynamic>()){
        for (var i:uint = 0; i < objectsToAnimate.length; i++)
        {
              if (objectsToAnimate[i].movieClip != mc)
              {
                   var dataObj:Object = new Object(); 
                   dataObj.movieClip = mc; 
                   dataObj.dataVars = vars; 
                   objectsToAnimate.push(dataObj);
              }
              else if (objectsToAnimate[i].movieClip == mc)
                   this.currentMcData = objectsToAnimate[i];
        }
    }
    private function animateThis(e:Event){
            //codes here
            trace(this.currentMcData.vars);
            // or run a loop and animate all the movie clips.
    }

}

希望这会有所帮助。

【讨论】:

  • 使用this.aVar 完成测试它在我的代码中不起作用。尽管函数接收到了this.aVar,但一旦激活了对initializer() 函数的立即调用,它就会被覆盖,我必须像它是两个函数的本地变量一样传递它(?)
  • 首先,感谢您的支持。其次,让我更好地了解您的需求。你希望这个变量只更新一次吗?更多细节当然可以帮助我更新我的答案。谢谢。
  • 我有大约 10 个变量,animateThis() 应该从 initializer() 访问。有些是更新的,而另一些是静态的。我在想,也许我可以将 ENTER_FRAME 事件转移到 initializer() 然后让它传递一个参数。但是我遇到了两个问题(1)我无法像从 setInterval() 那样传递参数;(2)我无法为每个午餐的addEventListener 设置一个int identifier
  • @DominicGuana 静态变量可以在你的类本身中定义,你可以在你的CubeEaseOut 本身中初始化它们,它们永远不会改变。但是,我不清楚您要更新的内容。您什么时候需要更新它们?如果您想在初始化方法中执行此操作,那么由于每次鼠标悬停都会调用该方法,因此它将更新。而你只有一个 CubeEaseOut 的类实例
  • 我现在对上面做了一些修改。
猜你喜欢
  • 1970-01-01
  • 2021-05-19
  • 2016-10-23
  • 2018-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 2017-10-03
相关资源
最近更新 更多