Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.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
C# 基于持续时间将对象从一点移动到另一点_C#_Javascript_Actionscript 3 - Fatal编程技术网

C# 基于持续时间将对象从一点移动到另一点

C# 基于持续时间将对象从一点移动到另一点,c#,javascript,actionscript-3,C#,Javascript,Actionscript 3,我对这个问题绞尽脑汁已经有一段时间了,在网上寻找解决方案却毫无效果 基本上,如果我有一个位于10,10的精灵,我想把它移动到50100,整个过程需要2秒或者我指定的任何时间。这背后的确切数学是什么?我使用基于距离的解决方案来确定速度,但只是使用随机修改器来控制过程。我需要更精确的东西,以便在设定的持续时间内准确执行 在此问题上的任何帮助都将不胜感激 这只是一个插值和时间的问题。 有线性、正弦、二次 下面是actionscript中的更多信息和示例:这只是一个插值和时间问题。 有线性、正弦、二次

我对这个问题绞尽脑汁已经有一段时间了,在网上寻找解决方案却毫无效果

基本上,如果我有一个位于10,10的精灵,我想把它移动到50100,整个过程需要2秒或者我指定的任何时间。这背后的确切数学是什么?我使用基于距离的解决方案来确定速度,但只是使用随机修改器来控制过程。我需要更精确的东西,以便在设定的持续时间内准确执行


在此问题上的任何帮助都将不胜感激

这只是一个插值和时间的问题。 有线性、正弦、二次


下面是actionscript中的更多信息和示例:

这只是一个插值和时间问题。 有线性、正弦、二次


下面是actionscript中的一些更多信息和示例:

假设线性插值,即以恒定速率沿直线从起始位置移动到结束位置:

从开始到目的地的向量是目的地-开始,即,对于您的示例40,90

如果你想在两秒钟内发生这种情况,你需要将其除以2,得到每秒行驶的距离,例如20,45秒

要获取任何给定时间的位置,首先记录开始时间并计算当前时间减去开始时间(以秒为单位)。因此,如果动画从12:01:30.000开始,现在是12:01:31.500,那么自动画开始以来已经过去了1.5秒

要获取当前位置,请将开始位置添加到每秒移动向量*经过的时间,因此在我的示例中:


10,10+20,45*1.5=10,10+30,67.5=40,77.5

假设线性插值,即以恒定速率沿直线从起始位置移动到结束位置:

从开始到目的地的向量是目的地-开始,即,对于您的示例40,90

如果你想在两秒钟内发生这种情况,你需要将其除以2,得到每秒行驶的距离,例如20,45秒

要获取任何给定时间的位置,首先记录开始时间并计算当前时间减去开始时间(以秒为单位)。因此,如果动画从12:01:30.000开始,现在是12:01:31.500,那么自动画开始以来已经过去了1.5秒

要获取当前位置,请将开始位置添加到每秒移动向量*经过的时间,因此在我的示例中:


10,10+20,45*1.5=10,10+30,67.5=40,77.5。。。也许你可以使用一些代码


搜索自定义:作为起点。

仔细查看jQuery的动画算法。。。也许你可以使用一些代码


搜索自定义:作为起点。

您需要一些信息来完成此操作,包括开始位置、结束位置、持续时间和经过的时间

下面是actionscript中的一个示例:

package {
    import flash.utils.getTimer;
    import flash.events.Event;
    import flash.display.Shape;
    import flash.geom.Point;
    import flash.display.Sprite;

    public class Mover extends Sprite {

        private var circle      :Shape;
        private var start       :Point;
        private var end         :Point;
        private var duration    :int;

        public function Mover() {

            // first we create something to move, like, a circle
            circle = new Shape();
            circle.graphics.beginFill(0xff00ff);
            circle.graphics.drawCircle(0, 0, 20);
            addChild(circle);

            // start and end positions
            start = new Point(0, 0);
            end = new Point(100, 100);

            // and finally, the duration, i'm using milliseconds
            duration = 2000;

            // this event handler will run each frame
            addEventListener(Event.ENTER_FRAME, handleEnterFrame);
        }

        private function handleEnterFrame(event:Event):void {
            // we figure out how much of the animation has elapsed by using getTimer
            // should you want to use a start time, add it here 
            var progress:Number = getTimer() / duration;

            // we need to clamp our progress so we don't under- or overshoot the target
            if(progress < 0) progress = 0;
            if(progress > 1) progress = 1;


            // then it's a matter of setting the position
            // we use the start position as a base and then gradually add in 
            // the difference between the start and the end
            circle.x = start.x + (end.x - start.x) * progress;
            circle.y = start.y + (end.y - start.y) * progress;  
        }
    }
}

如果你对怎么做不感兴趣,只是想得到结果,我衷心推荐tweening引擎,或者其他无数的引擎。请远离flash附带的内容,这有点垃圾。

要做到这一点,您需要一些信息,开始位置、结束位置、持续时间和经过的时间

下面是actionscript中的一个示例:

package {
    import flash.utils.getTimer;
    import flash.events.Event;
    import flash.display.Shape;
    import flash.geom.Point;
    import flash.display.Sprite;

    public class Mover extends Sprite {

        private var circle      :Shape;
        private var start       :Point;
        private var end         :Point;
        private var duration    :int;

        public function Mover() {

            // first we create something to move, like, a circle
            circle = new Shape();
            circle.graphics.beginFill(0xff00ff);
            circle.graphics.drawCircle(0, 0, 20);
            addChild(circle);

            // start and end positions
            start = new Point(0, 0);
            end = new Point(100, 100);

            // and finally, the duration, i'm using milliseconds
            duration = 2000;

            // this event handler will run each frame
            addEventListener(Event.ENTER_FRAME, handleEnterFrame);
        }

        private function handleEnterFrame(event:Event):void {
            // we figure out how much of the animation has elapsed by using getTimer
            // should you want to use a start time, add it here 
            var progress:Number = getTimer() / duration;

            // we need to clamp our progress so we don't under- or overshoot the target
            if(progress < 0) progress = 0;
            if(progress > 1) progress = 1;


            // then it's a matter of setting the position
            // we use the start position as a base and then gradually add in 
            // the difference between the start and the end
            circle.x = start.x + (end.x - start.x) * progress;
            circle.y = start.y + (end.y - start.y) * progress;  
        }
    }
}
如果你对怎么做不感兴趣,只是想得到结果,我衷心推荐tweening引擎,或者其他无数的引擎。别碰flash附带的那个,它有点垃圾