物体在模拟运动 [英] Movement of Objects in a simulation

查看:160
本文介绍了物体在模拟运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前做一个闪光池塘,在这鱼,我要补充食物颗粒到池塘的能力,我应该怎么做我的鱼的运动食品x,y位置?所以这将是平滑的,而不是设置的食物颗粒的x,我才对y位置的x,y位置。

也就是在那里学习如何让我的鱼儿将通过其自身的好地方? 类似 http://abowman.com/google-modules/fish/

鱼类

 包
{
    进口的flash.display.MovieClip;
    进口的flash.display.Stage;
    进口对象类型:flash.events.Event;
    进口flash.utils.Timer;
    进口flash.events.TimerEvent;
    进口flash.filters.DropShadowFilter;
    / **
     * ...
     * @authorž
     *鱼类,使鱼类的行为​​,寻找食物。
     * /
    公共类鱼扩展了影片剪辑
    {
        私人VAR stageReference:舞台;
        私人VAR foodArray:阵列=新的Array();
        公共职能鱼(stageReference:第一阶段)
        {
            this.stageReference = stageReference;
            X = stageReference.stageWidth / 2;
            Y = stageReference.stageHeight / 2;
            setUpShadow()

            //添加监听器
            的addEventListener(Event.ENTER_FRAME,环路,假,0,真正的);

        }

        公共职能环路(五:事件):无效
        {
            如果(Y  -  15℃; foodArray.pop.y和放大器;&安培; Y + 15> foodArray.pop.y)
                moveToFood();

        }
        公共职能moveToFood():无效
        {

        }
        公共职能addFoodTarget(foodTarget:食品):无效
        {
            foodArray.push(foodTarget);
        }
        公共职能setUpShadow():无效
        {
            //http://blog.0tutor.com/post.aspx?id=103
            //首先声明一个对象,一个阴影的过滤器,并命名为my_shadow以备将来参考。
            VAR my_shadow:的DropShadowFilter =新的DropShadowFilter();
            //现在我们应用一些属性到我们新的过滤器对象,这第一个属性是颜色,我们设置为黑色,因为大多数阴影。
            my_shadow.color = 0x000000处;
            //下面两个性质,我们设定,是我们的阴影效果相对的位置​​到目标,
            //这意味着从两个在x和y轴的物体8像素。
            my_shadow.blurY = 8;
            my_shadow.blurX = 8;
            //这里我们设置的角度决定了阴影,也相对的对象。
            my_shadow.angle = 150;
            //设置一个字母的影子。这是设置阴影,应该怎么黑是的力量。
            my_shadow.alpha = 0.5;
            //这里我们设置距离我们的影子对象。
            my_shadow.distance = 15;
            //现在我们定义的数组为我们的过滤器,它的属性来保存它。这将是我们参考的最终目的是,当我们需要将它应用到一些东西。
            VAR filtersArray:阵列=新阵列(my_shadow);
            //最后一步是把我们的电影剪辑,我们一开始做的,所以我们把我们的对象,并应用filtersArray。
            过滤器= filtersArray;

        }
    }

}
 

食品类

 包
{
    进口的flash.display.MovieClip;
    进口的flash.display.Stage;
    进口对象类型:flash.events.Event;
    进口flash.utils.Timer;
    进口flash.events.TimerEvent;
    进口flash.filters.DropShadowFilter;
    进口的flash.display.MovieClip;
    进口flash.events.MouseEvent;
    / **
     * ...
     * @authorž
     *食品类
     * /
    公共类食品扩展影片剪辑
    {
        私人VAR stageReference:舞台;
        私人VAR foodDisperseSpeed​​:数= 0.4;

        公共功能食品(stageReference:第一阶段,XPOS:号码,yPos:号)
        {
            X = XPOS;
            Y = yPos;
            this.stageReference = stageReference;
            的addEventListener(Event.ENTER_FRAME,环路,假,0,真正的);
        }

        私有函数环(五:事件):无效
        {
                如果(的Math.random()&0.5)
                x  -  = foodDisperseSpeed​​;
                其他
                Ÿ -  = foodDisperseSpeed​​;

        }

        公共职能removeSelf():无效
        {
            removeEventListener(Event.ENTER_FRAME,循环);

            如果(stageReference.contains(本))
                stageReference.removeChild(本);

        }

    }

}
 

解决方案

这应该解释究竟是如何来移动你的对象。显然,你需要将雪碧变成什么类你用你的鱼/食品/等。你的鱼对象要么需要速度的属性。如:公共变种速度:INT = 5;或者,你可以在你的主要模拟定义一个用于所有的鱼。 (我想$有变速鱼自己p $ PFER)

  //这是可以做到更快,但这是向你展示它是如何工作的。
    公共职能moveObject(OBJ:雪碧,目标:雪碧):无效
    {
        //我们首先获得的距离沿x和y轴
        变种DX:数= target.x  -  obj.x;
        变种DY:数= target.y  -  obj.y;

        //基本距离检查与勾股定理。我们不需要
        //获得平方根的,因为如果它是较小的平方它
        //将仍然较小,一旦我们得到的平方根,从而
        //将只是浪费时间。
        如果(DX * DX + DY * DY< obj.speed * obj.speed)
        {
            //由于我们是在速度的一个时间步,我们将走过去吧
            //如果我们继续下去所以才锁定到位的位置。
            obj.x = target.x;
            obj.y = target.y;
        }
        其他
        {
            //我们现在还没有那么让摇滚。

            //取得在给定的沿x轴和y轴的距离弧度的角度。
            VAR angleRads:数= Math.atan2(DY,DX);

            //让我们沿x轴和y轴速度
            //注:记得闪光灯坐标系的布局。
            //我们需要calc下沿x轴和罪恶沿y COS。
            VAR VX:数= Math.cos(angleRads)* obj.speed;
            VAR VY:数= Math.sin(angleRads)* obj.speed;

            //现在,我们有我们的速度,我们可以将它添加到对象
            //当前位置,从而创造每一个很好的平滑移动
            // 帧。
            obj.x + = VX;
            obj.y + = VY;

            //这将锁定对象的旋转的方向
            //对象行驶在。
            //注意:当你做你的对象,不要忘记闪存
            零//旋转=指向右侧(+ x)​​的
            obj.rotation = angleRads * 180 / Math.PI;
        }
    }
 

希望这有助于任何问题,只是问。

编辑: 你并不需要添加一个引用到舞台的Sprite或MovieClip他们已经获得了舞台。

i am currently making a flash "pond" with fishes in it, and i have the ability to add food pellets onto the pond, how should i make the movement of my fishes to the food x,y location ? so it will be smooth , rather than setting the x,y location of my fishes to the x,y location of the food pellets.

Also is there a good place to learn how to make my fishes move by its own? similar to http://abowman.com/google-modules/fish/

Fish class

package  
{
    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.filters.DropShadowFilter;
    /**
     * ...
     * @author Z
     * Fish class, makes fishes behaviors, find food.
     */
    public class Fish extends MovieClip
    {
        private var stageReference:Stage;
        private var foodArray :Array = new Array();
        public function Fish(stageReference:Stage) 
        {
            this.stageReference = stageReference;
            x = stageReference.stageWidth / 2;
            y = stageReference.stageHeight / 2;
            setUpShadow()

            //adding listener
            addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

        }

        public function loop(e:Event):void
        {
            if (y - 15 < foodArray.pop.y && y + 15 > foodArray.pop.y)
                moveToFood();

        }
        public function moveToFood():void
        {

        }
        public function addFoodTarget(foodTarget:Food):void
        {   
            foodArray.push(foodTarget);
        }
        public function setUpShadow():void
        {
            //http://blog.0tutor.com/post.aspx?id=103
            //First we declare an object, a dropshadow filter and name it my_shadow for further reference.
            var my_shadow:DropShadowFilter = new DropShadowFilter();
            //Now we apply some properties to our new filters object, this first property is the color, and we set that to black, as most shadows are.
            my_shadow.color = 0x000000;
            //These next two properties we set, are the position of our dropshadow relative to the object,
            //This means 8 px from the object on both the x and y axis.
            my_shadow.blurY = 8;
            my_shadow.blurX = 8;
            //And here we set an angle for the dropshadow, also relative to the object.
            my_shadow.angle = 150;
            //Setting an alpha for the shadow. This is to set the strength of the shadow, how "black" it should be.
            my_shadow.alpha = .5;
            //and here we set the distance for our shadow to the object.
            my_shadow.distance = 15; 
            //Now we define an array for our filter with its properties to hold it. This will be the final object we refer to when we need to apply it to something.
            var filtersArray:Array = new Array(my_shadow);
            //The last step is to take our movie clip that we made at the beginning, so we take our object and apply the filtersArray.
            filters = filtersArray;

        }
    }

}

food class

package  
{
    import flash.display.MovieClip;
    import flash.display.Stage;
    import flash.events.Event;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.filters.DropShadowFilter;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    /**
     * ...
     * @author Z
     * food class
     */
    public class Food extends MovieClip
    {
        private var stageReference:Stage;
        private var foodDisperseSpeed:Number = 0.4;

        public function Food(stageReference:Stage,xPos:Number,yPos:Number) 
        {
            x = xPos;
            y = yPos;
            this.stageReference = stageReference;
            addEventListener(Event.ENTER_FRAME, loop, false, 0, true);
        }

        private function loop(e:Event):void
        {
                if(Math.random() > 0.5)
                x -= foodDisperseSpeed;
                else
                y -= foodDisperseSpeed;

        }

        public function removeSelf():void
        {
            removeEventListener(Event.ENTER_FRAME, loop);

            if (stageReference.contains(this))
                stageReference.removeChild(this);

        }

    }

}

解决方案

This should explain exactly how to move your objects around. Obviously you will need to change the Sprite to whatever class your using for your fish/food/etc. Your fish object will either need a property for speed. such as: public var speed:int = 5; Or you can just define one in your main simulation for all the fish. ( I would prefer having variable speed fish myself)

    // This can be done faster but this is to show you how it works.
    public function moveObject(obj:Sprite, target:Sprite):void
    {
        // We start by getting the distances along the x and y axis
        var dx:Number = target.x - obj.x;
        var dy:Number = target.y - obj.y;

        // Basic distance check with pythagorean theorem. We don't need
        // to get the Square Roots because if it is smaller squared it
        // will still be smaller once we get the square roots and thus
        // will just be a waste of time.
        if (dx * dx + dy * dy < obj.speed * obj.speed)
        {
            // Since we are within one time step of speed we will go past it
            // if we keep going so just lock the position in place.
            obj.x = target.x;
            obj.y = target.y;
        }
        else
        {
            // we aren't there yet so lets rock.

            // get the angle in radians given the distance along the x and y axis.
            var angleRads:Number = Math.atan2(dy, dx);

            // get our velocity along the x and y axis
            // NOTE: remember how the flash coordinate system is laid out.
            // we need to calc cos along the x and sin along the y.
            var vx:Number = Math.cos(angleRads) * obj.speed;
            var vy:Number = Math.sin(angleRads) * obj.speed;

            // now that we have our speeds we can add it to the objects 
            // current position and thus create a nice smooth movement per
            // frame.
            obj.x += vx;
            obj.y += vy;

            // This will lock the rotation of the object to the direction
            // the object is traveling in.
            // NOTE: don't forget when you make your objects that for flash
            // rotation of Zero = pointing to the right (+x)
            obj.rotation = angleRads * 180 / Math.PI;
        }
    }

Hope this helps, any questions just ask.

Edit: You don't need to add a reference to the stage in a Sprite or MovieClip they already have access to the stage.

这篇关于物体在模拟运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆