Mr.Taurayi你给出的解决方案非常棒。但我不想在输入框架中这样做。我只是将对象补间旋转到随机位置。所以让我知道轮换的条件。我的意思是物体应朝随机点旋转。例如。 if(dx <0){
dx += 360;
dy += 360;
angl = Math.atan(dy/dx) + deg2rad(90)
InsectsVector[i].rotation = angl;
}
if(dx >= 360){
dx -= 360;
dy -= 360;
angl = Math.atan(dy/dx) + deg2rad(90)
InsectsVector[i].rotation = angl;
}
if ( dy > 180 )
{
dx -= 360;
dy -= 360
angl = Math.atan(dy/dx) + deg2rad(-90);
InsectsVector[i].rotation = angl;
}
if ( dx < -180 )
{
dx += 360;
dy += 360;
angl = Math.atan(dy/dx) + deg2rad(90)
InsectsVector[i].rotation = angl;
}
以上条件在以下编码中无法正常工作。所以请让我知道。
Please its very urgent dudes. And thanks in advance for replying.
private function InsectsRandPos():void{
randNum_1 = uint(Math.random()*50);
for (var i:uint=0; i < InsectsVector.length; i++){
while(InsecNode.indexOf(randNum_1) != -1){
randNum_1 = uint(Math.random()*50);
}
InsecNode[i] = randNum_1;
randPointForInsec = nodeContainer.getNodePostion(InsecNode[i]);
if(spNode != InsecNode[i]){
InsectsVector[i].visible = true;
nodeContainer.addChild(InsectsVector[i]);
var dx:Number = randPointForInsec.x - InsectsVector[i].x ;
var dy:Number = randPointForInsec.y - InsectsVector[i].y ;
var angl:Number = Math.atan(dy/dx) + deg2rad(90);
InsectsVector[i].rotation = angl; // (angl*-1)+180;
if( dx < 0){
dx += 360;
dy += 360;
angl = Math.atan(dy/dx) + deg2rad(90)
InsectsVector[i].rotation = angl;
}
if(dx >= 360){
dx -= 360;
dy -= 360;
angl = Math.atan(dy/dx) + deg2rad(90)
InsectsVector[i].rotation = angl;
}
if ( dy > 180 )
{
dx -= 360;
dy -= 360
angl = Math.atan(dy/dx) + deg2rad(-90);
InsectsVector[i].rotation = angl;
}
if ( dx < -180 )
{
dx += 360;
dy += 360;
angl = Math.atan(dy/dx) + deg2rad(90)
InsectsVector[i].rotation = angl;
}
var InsectTwee:Tween = new Tween(InsectsVector[i], 5, Transitions.LINEAR );
InsectTwee.animate("x", randPointForInsec.x);
InsectTwee.animate("y", randPointForInsec.y);
Starling.juggler.add(InsectTwee);
}
else if(spNode == InsecNode[i]){
var obj:Object = new Object();
obj = InsectsVector[i];
obj.visible = false;
trace("obj .name ..."+obj + " InsectsVector[i] :"+InsectsVector[i])
}
}
}
答案 0 :(得分:2)
理解你的问题有点难。我从我对你认为的要求的理解中做了一个例子:
<强> [增订] 强>
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.geom.Point;
import flash.utils.Timer;
/**
* ...
* @author
*/
public class Main extends Sprite
{
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}// end function
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
for (var i:int = 0; i < 20; i++) {
var npc:NPC = new NPC();
npc.addEventListener(NPC.MOVE_COMPLETE, onMoveComplete);
npc.setDelay(5000);
npc.setPoints(getRandomPoint(), getRandomPoint());
npc.updateRotation();
npc.move();
addChild(npc);
}// end for
}// end function
private function onMoveComplete(e:Event):void {
var npc:NPC = e.target as NPC;
npc.removeEventListener(NPC.MOVE_COMPLETE, onMoveComplete);
npc.setPoints(npc.getPoint(), getRandomPoint());
npc.addEventListener(NPC.ROTATION_COMPLETE, onRotationComplete);
npc.rotate();
}// end function
private function onRotationComplete(e:Event):void {
var npc:NPC = e.target as NPC;
npc.removeEventListener(NPC.ROTATION_COMPLETE, onRotationComplete);
npc.addEventListener(NPC.MOVE_COMPLETE, onMoveComplete);
npc.move();
}// end function
private function getRandomPoint():Point {
var randomX:Number = Math.random() * stage.stageWidth;
var randomY:Number = Math.random() * stage.stageHeight;
return new Point(randomX, randomY);
}// end function
}// end class
}// end package
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.geom.Point;
import flash.utils.Timer;
import flash.events.TimerEvent;
class NPC extends Sprite {
public static const MOVE_COMPLETE:String = "moveComplete";
public static const ROTATION_COMPLETE:String = "rotationComplete";
private var _to:Point;
private var _from:Point;
private var _timer:Timer;
private var _delay:Number;
public function get from():Point {
return _from;
}// end function
public function get to():Point {
return _to;
}// end function
public function NPC() {
addShape();
}// end function
public function setDelay(delay:Number):void {
_delay = delay;
}// end function
public function setPoints(from:Point, to:Point):void {
this._from = from;
this._to = to;
this.x = from.x;
this.y = from.y;
}// end function
public function move():void {
if (!this._to || !this._from)
throw new Error("to and from points must be set");
if (!_delay)
throw new Error("delay must be set");
addTimer();
}// end function
public function rotate():void {
this.addEventListener(Event.ENTER_FRAME, onRotationEnterFrame);
}// end function
private function onRotationEnterFrame(e:Event):void
{
var dAngle:int = (getAngle() - this.rotation) % 360;
if (dAngle < -180) {
dAngle += 360;
}
else if (dAngle > 180) {
dAngle -= 360;
}// end if
if (dAngle < 0) {
--this.rotation;
} else {
++this.rotation;
}// end if
if (dAngle == 0) {
removeEventListener(Event.ENTER_FRAME, onRotationEnterFrame);
dispatchEvent(new Event(NPC.ROTATION_COMPLETE));
}// end if
}// end function
private function addShape():void {
var shape:Shape = new Shape();
shape.graphics.lineStyle(1, 0x000000);
shape.graphics.drawRect(-10, -10, 20, 20);
shape.graphics.endFill();
shape.graphics.beginFill(0x000000);
shape.graphics.moveTo(-10, -10);
shape.graphics.lineTo(0,-10);
shape.graphics.lineTo(-10, 0);
shape.graphics.lineTo( -10, -10);
shape.rotation = 45;
addChild(shape);
}// end function
private function addTimer():void {
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
_timer = new Timer(_delay, 1);
_timer.addEventListener(TimerEvent.TIMER, onTimer);
_timer.start();
}// end function
private function onTimer(e:Event):void {
removeTimer();
}// end function
private function removeTimer():void {
_timer.removeEventListener(TimerEvent.TIMER, onTimer);
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
dispatchEvent(new Event(NPC.MOVE_COMPLETE));
}// end function
private function onEnterFrame(e:Event):void {
var dPoint:Point = getDPoint();
this.x += dPoint.x;
this.y += dPoint.y;
if (_to.equals(getPoint())) {
removeTimer();
}// end if
}// end function
private function getDPoint():Point {
var point:Point = _to.subtract(_from);
point.normalize(1);
return point;
}// end function
public function getPoint():Point {
return new Point(this.x, this.y);
}// end function
public function updateRotation():void {
this.rotation = getAngle();
}// end function
public function getAngle():Number {
var dPoint:Point = getDPoint();
return Math.atan2(dPoint.y, dPoint.x) * (180 / Math.PI) + 90;
}// end function
}// end class
尝试运行它,看看它是否符合您的需要。如果是这样,我将逐步解释这个例子。
以下是运行示例应用程序的屏幕截图:
<强> [UPDATE] 强>
@Siva我刚刚添加了公共rotate()
方法和私有onRotationEnterframe
事件处理程序。
rotate()
方法只会向Event.ENTER_FRAME
对象添加NPC
侦听器。
public function rotate():void {
this.addEventListener(Event.ENTER_FRAME, onRotationEnterFrame);
}// end function
当调用onRotationEnterFrame()
事件处理程序时,第一个语句获取NPC
对象将旋转到的角度与NPC
对象的当前角度之间的差异。
var dAngle:int = (getAngle() - this.rotation) % 360;
根据角度是小于-180度还是大于180度,我们增加或减去360度。
if (dAngle < -180) {
dAngle += 360;
}
else if (dAngle > 180) {
dAngle -= 360;
}// end if
然后我们使用改变的角度来确定我们是顺时针还是逆时针旋转。
if (dAngle < 0) {
--this.rotation;
} else {
++this.rotation;
}// end if
当角度达到0时,我们会删除onRotationEnterFrame
事件侦听器并发送NPC.ROTATION_COMPLETE
事件。
if (dAngle == 0) {
removeEventListener(Event.ENTER_FRAME, onRotationEnterFrame);
dispatchEvent(new Event(NPC.ROTATION_COMPLETE));
}// end if
最后,在客户端,我们通过添加侦听onMoveComplete()
的事件侦听器来更改NPC.ROTATION_COMPLETE
事件处理程序。我们为NPC
对象将遵循的新行设置新点,然后调用NPC
对象的rotate()
。