Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 循环中的随机数,如何阻止它每帧获得一个新的数?_Actionscript 3 - Fatal编程技术网

Actionscript 3 循环中的随机数,如何阻止它每帧获得一个新的数?

Actionscript 3 循环中的随机数,如何阻止它每帧获得一个新的数?,actionscript-3,Actionscript 3,学习动作脚本和编程,所以我很抱歉解决方案很简单,我只是不知道它是什么。我想做的是通过我想从屏幕上掉下来的物体。我通过e:Event(来自舞台上的事件监听器)和speed传入对象,speed得到一个介于高值和低值之间的随机数 当我运行它时,它当然会在每一帧得到一个新的随机数。(因为它是由Event.ENTER_FRAME调用的。) 我该如何实现我的目标 这是我的密码: private function moveIt(e:Event, speed):void { if ( e

学习动作脚本和编程,所以我很抱歉解决方案很简单,我只是不知道它是什么。我想做的是通过我想从屏幕上掉下来的物体。我通过e:Event(来自舞台上的事件监听器)和speed传入对象,speed得到一个介于高值和低值之间的随机数

当我运行它时,它当然会在每一帧得到一个新的随机数。(因为它是由Event.ENTER_FRAME调用的。)

我该如何实现我的目标

这是我的密码:

private function moveIt(e:Event, speed):void
    {
        if ( e.currentTarget.y <= 400 )
        {
            var objSpeed = speed;
            e.currentTarget.y = e.currentTarget.y + speed;
            trace("speed = "+ speed);
        }
        else
        {
             //Do other stuff
        }
    }//moveit
在这里打电话

iconPsd.addEventListener(Event.ENTER_FRAME, animate)
然后穿过这里(没有人需要,但这是我的尝试)


var-speed:Number=randomNum(55,0.1);
从动画功能中取出

这样写:

iconPsd.speed = randomNum(55, 0.1);
iconPsd.addEventListener(Event.ENTER_FRAME, animate);

private function animate(e:Event):void
    {
        moveIt(e);
        trace ("speed in animate = "+ e.currentTarget.speed);

    }//animate

private function moveIt(e:Event):void
    {
        if ( e.currentTarget.y <= 400 )
        {
            e.currentTarget.y = e.currentTarget.y + e.currentTarget.speed;
            trace("speed = "+ e.currentTarget.speed);
        }
        else
        {
             //Do other stuff
        }
    }//moveit

您应该发布代码中生成随机数速度的部分。我已经添加了代码。我没有错误,我只需要知道如何在循环中正确使用随机数,这样它就不会在每一帧都得到一个新的随机数。Perfict,谢谢!这正是我想要的。我不知道速度属性。@jonshariat很高兴看到它w为您工作:),但速度不是默认属性,您可以用任何东西替换速度。因为iconPsd是一个对象,你可以向它添加任何你想要的属性(把它看作是存储变量,你可以使用这个对象作为作用域来访问)。啊,我明白了。很高兴知道!谢谢
private function animate(e:Event):void
    {
        var speed:Number = randomNum(55, 0.1);
        moveIt(e, speed);
        trace ("speed in animate = "+ speed);

    }//animate
iconPsd.speed = randomNum(55, 0.1);
iconPsd.addEventListener(Event.ENTER_FRAME, animate);

private function animate(e:Event):void
    {
        moveIt(e);
        trace ("speed in animate = "+ e.currentTarget.speed);

    }//animate

private function moveIt(e:Event):void
    {
        if ( e.currentTarget.y <= 400 )
        {
            e.currentTarget.y = e.currentTarget.y + e.currentTarget.speed;
            trace("speed = "+ e.currentTarget.speed);
        }
        else
        {
             //Do other stuff
        }
    }//moveit
iconPsd.speed = randomNum(55, 0.1);
iconPsd.addEventListener(Event.ENTER_FRAME, animate);

private function animate(e:Event):void
    {
        if ( e.currentTarget.y <= 400 )
        {
            e.currentTarget.y = e.currentTarget.y + e.currentTarget.speed;
            trace("speed = "+ e.currentTarget.speed);
        }
        else
        {
             //Do other stuff
        }
    }//animate