问题
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);
}
}
}
回答1:
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.
回答2:
This is a quick and easy way to do what you're after:
public function moveToFood(food:Food):void
{
x -= (x - food.x) / 40;
y -= (y - food.y) / 40;
}
For something a little nicer you'll probably need to look at your own small artificial intelligence for the fish (something like picking the nearest food, moving toward it at a constant pace and then starting to feed on it).
回答3:
You could use a couple of Tween objects to adjust your fishes' x/y coords so that they move toward the food. What's nice is that you could get an ease in/ease out effect pretty easily this way.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/transitions/Tween.html
回答4:
Download the tweenLite library at http://www.greensock.com/tweenlite/ and add it to your project.
you can then call for tweenlite like this:
TweenLite.to(fish, timeToGoToFood, {x: food.x, y: food.y, ease: yourEasing});
The easing can be chosen from some preprogrammed easingfunctions of tweenLite. Cubic.easeout or Cubic.easein will work best for your fishes i think. :)
来源:https://stackoverflow.com/questions/6247730/movement-of-objects-in-a-simulation