我正在尝试创建一个屏幕保护程序,在内容中它有一个让我们称之为'主要的鱼'只是游来游去。我设法做了主要鱼只游泳的编码部分。现在我已经添加了较小的鱼跟随我能够做的主要鱼类,但是我怎样才能使小鱼留在主鱼后面,因为它不会与它或主鱼之后的其他鱼类发生碰撞。这就是我到目前为止所做的事情
这是鱼类(跟随的鱼类)
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.geom.Point;
public class Fish extends MovieClip
{
var speed:Number = 3;
var target:Point;
public function Fish()
{
// constructor code
addEventListener(Event.ENTER_FRAME, update);
}
function update(e:Event)
{
//Point fish at main fish
var dx = MovieClip(parent).mainfish01.x - x;
var dy = MovieClip(parent).mainfish01.y - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;
//Move in the direction the fish is facing
x = x+Math.cos(rotation/180*Math.PI)*speed;
y = y+Math.sin(rotation/180*Math.PI)*speed;
//Calculate the distance to target
var hyp = Math.sqrt((dx*dx)+(dy*dy));
}
}
}
答案 0 :(得分:0)
设置最大距离,然后检查更新位置之前的距离
function update(e:Event)
{
//Point fish at main fish
var dx = MovieClip(parent).mainfish01.x - x;
var dy = MovieClip(parent).mainfish01.y - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;
//Calculate the distance to target
var hyp = Math.sqrt((dx*dx)+(dy*dy));
// calculate based on size
var minDist = 10;
if(hyp > minDist){
//Move in the direction the fish is facing
x = x+Math.cos(rotation/180*Math.PI)*speed;
y = y+Math.sin(rotation/180*Math.PI)*speed;
}
}