【发布时间】:2011-09-09 00:23:51
【问题描述】:
我目前正在制作一个带有鱼的闪光“池塘”,并且我能够将食物颗粒添加到池塘中,我应该如何让我的鱼移动到食物 x,y 位置?所以它会很平滑,而不是将我的鱼的 x,y 位置设置为食物颗粒的 x,y 位置。
还有学习如何让我的鱼自己移动的好地方吗? 类似于http://abowman.com/google-modules/fish/
鱼类
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;
}
}
}
食品类
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);
}
}
}
【问题讨论】:
标签: flash actionscript-3 flash-cs4