Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 画一个箭头,然后围绕其起点3旋转_Actionscript 3_Flash - Fatal编程技术网

Actionscript 3 画一个箭头,然后围绕其起点3旋转

Actionscript 3 画一个箭头,然后围绕其起点3旋转,actionscript-3,flash,Actionscript 3,Flash,我有两门课:箭头和旋转鼠标。Arrow接受两个点并在这两个点之间绘制一个箭头。RotateToMouse应该围绕其起点旋转箭头以始终指向鼠标,但它似乎围绕原点旋转。如何使其移动其旋转点?箭头采用a1、a2(点)和col(a颜色) 这是旋转鼠标: package{ import flash.display.Sprite; import flash.events.Event; import flash.geom.Point; public class RotateToMouse extends Sp

我有两门课:箭头和旋转鼠标。Arrow接受两个点并在这两个点之间绘制一个箭头。RotateToMouse应该围绕其起点旋转箭头以始终指向鼠标,但它似乎围绕原点旋转。如何使其移动其旋转点?箭头采用a1、a2(点)和col(a颜色)

这是旋转鼠标:

package{
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
public class RotateToMouse extends Sprite{
private var arr:Arrow;
}
public function RotateToMouse(arr:Arrow){
this.arr=arr;
init();
}

private function init():void{
addChild(arr);
addEventListener(Event.ENTER_FRAME, onEnterFrame;
}

public function onEnterFrame(evt:Event):void{
    var diffx:Number=mouseX-arr.a2.x;
    var diffy:Number=mouseX-arr.a2.y;
    var radians:Number=Math.atan2(diffy, diffx);
    arr.rotation=radians*180/Math.PI;
}
我还有另一个文件,arrowdrawing.fla,它在第1帧的actionscript上显示:

    var arr:Arrow=new Arrow(new Point(40, 50), new Point(450, 400), 0x000000);
    var rotarr:RotateToMouse(arr);
    addChild(rotarr);
如所问,下面是Arrow类: 包装{ 导入flash.display.Sprite; 导入flash.geom.Point

public class Arrow extends Sprite{
public var a1:Point;
public var a2:Point;
private var col:uint;
private var dx:Number;
private var dy:Number;
private var angle:Number;
private var thdiff:Number=30; //angle of arrowhead is 30
private var len:Number=20; //length of arrowhead is 20
private var bangle:Number;
private var cangle:Number;

public function Arrow(a1:Point, a2:Point, col:uint){
    //constructor code
    this.col=col;
    this.a1=a1;
    this.a2=a2;
    dx=a2.x-a1.x;
    dy=a2.y-a1.y;   
}

public function init():void{
//draws a line between a1 and a2 of color col
    graphics.lineStyle(3, col);
    graphics.beginFill(col);
    graphics.moveTo(a1.x, a1.y);
    graphics.lineTo(a2.x, a2.y);
    graphics.endFill();
    drawArrowHead();
}

public function getCos(n:Number){
    //n will be given in degrees; we need to convert it to radians
    return Math.cos(n*Math.PI/180);
}
public function getSin(n:Number){
    //n will be given in degrees; we need to convert it to radians
    return Math.sin(n*Math.PI/180);
}
//arrow head consists of 2 points, b=(b1, b2) and c=(c1, c2)
public function drawArrowHead():void{
    //find the angle of the line we start with
    angle=Math.atan2(dx, dy);
    //for c,b we are going to figure out what the angle is of the lines to either side
    bangle=angle*(180/Math.PI)-thdiff;
    cangle=angle*(180/Math.PI)+thdiff;
    //point b, c are flipped around so that they can start from a2 (the endpoint)
    var b1:Number=a2.x-(len*getCos(bangle));
    var b2:Number=a2.y-(len*getSin(bangle));
    var c1:Number=a2.x-(len*getCos(cangle));
    var c2:Number=a2.y-(len*getSin(cangle));
//draw arrohead from a2 to b, and c
    graphics.lineStyle(3, col);
    graphics.beginFill(col);
    graphics.moveTo(a2.x, a2.y);
    graphics.lineTo(b1, b2);
    graphics.lineTo(c1, c2);
    graphics.lineTo(a2.x, a2.y);
    graphics.endFill();
    drawArrowHead();

}
}
}
}

Arrow
类中,您必须以旋转点位于(0,0)的方式绘制它。我不知道您现在做得如何(查看
Arrow
类声明会很有用),但您必须记住,每个对象都有不同的原点位置

最可能的错误是,您正在舞台位置(0,0)创建箭头对象(或只是创建而不移动)

其中,
*
是箭头位置(x,y)=(0,0)。在这种情况下,您需要做的是将箭头移动到起始位置,并在绘制时从所有点中减去该值:

 --------------
|              |
|              |
|   *----->    |
|              |
|              |
|              |
 --------------

它看起来完全一样,但旋转行为会有所不同。

我所知道的围绕轴心点旋转的最简单方法是根据目标的父对象记录轴心点的位置,进行旋转,因为您知道局部轴心点不应该相对于父对象发生更改,所以可以通过区别

import flash.display.Sprite;
import flash.geom.Point;
import flash.geom.Matrix;

/**
 * Sets the rotation of the given sprite to [newRotation], rotating around [pivotPoint].
 * @param target The sprite to set the rotation.
 * @param pivotPoint The local point to rotate around. This is in the coordinate space of the target.
 * @param newRotation (Degrees) the rotation in degrees to apply to target.
 */
function pivotRotate(target:Sprite, pivotPoint:Point, newRotation:Number) {
    var pivotPointParent:Point = target.transform.matrix.transformPoint(pivotPoint);
    target.rotation = newRotation;
    var pivotPointParent2:Point = target.transform.matrix.transformPoint(pivotPoint);
    var diff:Point = pivotPointParent2.subtract(pivotPointParent);
    target.x -= diff.x;
    target.y -= diff.y;
}

pivotRotate(arrow, new Point(15, 30), 90);

这几乎起作用了。我仍在尝试让它指向我的鼠标,但是(例如)如果箭头最初指向下方,指向左侧,鼠标指向右侧,它将开始持续旋转。这与我更改x和y的方式有关吗?
import flash.display.Sprite;
import flash.geom.Point;
import flash.geom.Matrix;

/**
 * Sets the rotation of the given sprite to [newRotation], rotating around [pivotPoint].
 * @param target The sprite to set the rotation.
 * @param pivotPoint The local point to rotate around. This is in the coordinate space of the target.
 * @param newRotation (Degrees) the rotation in degrees to apply to target.
 */
function pivotRotate(target:Sprite, pivotPoint:Point, newRotation:Number) {
    var pivotPointParent:Point = target.transform.matrix.transformPoint(pivotPoint);
    target.rotation = newRotation;
    var pivotPointParent2:Point = target.transform.matrix.transformPoint(pivotPoint);
    var diff:Point = pivotPointParent2.subtract(pivotPointParent);
    target.x -= diff.x;
    target.y -= diff.y;
}

pivotRotate(arrow, new Point(15, 30), 90);