AS3:graphics.drawTriangle方法奇怪的行为

时间:2014-10-17 18:04:32

标签: actionscript-3 graphics draw warp

总之,我使用以下圆形动画片段作为图形类的drawTriangles方法的顶点参数(vector。)。并在第二张图片上使用该方法。

first picture second picture

现在事情是前6点工作正常,但之后所有连续点都没有任何区别。

with numbering with grid

好的,这是代码。 m跳过circle-shaped-movieclips-drag-and-move-part

//cape is instance name of movieclip
//cape width and height
var cWidth:Number = cape.width;
var cHeight:Number = cape.height;

var verteces:Vector.<Number> = new Vector.<Number>();
var indeces:Vector.<int> = new Vector.<int>();
var uvtData:Vector.<Number> = new Vector.<Number>();

//populating parameters of draw triangels
var point:Point = new Point();
for(var i:int = 1; i<=capeSkel.numChildren; i++)
{
    point.x = capeSkel["pt"+i].x;
    point.y = capeSkel["pt"+i].y;
    verteces.push(point.x,point.y);
    uvtData.push(point.x/cWidth,point.y/cHeight,1);
}
indeces.push(0,10,5,  0,1,10,  1,2,10,  2,10,7,  5,6,10,  6,7,10,  2,11,7,  2,3,11,  3,4,11,       
4,11,9,  7,8,11,  8,9,11);

var capeBmd:BitmapData = new BitmapData(cWidth, cHeight, true);
capeBmd.draw(cape);
var sprite:Sprite = new Sprite();
addChild(sprite);
sprite.x = 0;
sprite.y = 260;

//this function is called everytime those movieclips are repositioned
function updateVerteces()
{
    var tempVerteces:Vector.<Number> = new Vector.<Number>();
    var point:Point = new Point();
    //capeSkel is the instance name of circle-shaped moveiclip's parent
    for(var i:int = 1; i<=capeSkel.numChildren; i++)
    {
        point.x = capeSkel["pt"+i].x;
        point.y = capeSkel["pt"+i].y;
        tempVerteces.push(point.x,point.y);
    }

    for(var k:int = 0; k<capeSkel.numChildren; k++)
    {
        verteces[k] = tempVerteces[k];
    }
}
function renderView()
{
    sprite.graphics.clear();
    sprite.graphics.beginBitmapFill(capeBmd);
    sprite.graphics.drawTriangles(verteces,indeces,uvtData);
    sprite.graphics.endFill();
}

1 个答案:

答案 0 :(得分:3)

在updateVertices()中,您创建一个临时向量,然后用新数据填充它,以便在下一步中覆盖原始数据? 无论如何,在第一个循环中,您将两个值推入容器中每个“顶点句柄”的tempVerteces(顶点是正确的单词),因此,此向量的长度为numChildren * 2但是当您从临时向量复制顶点到原始向量时你仍然使用numChildren。

因此,只有你需要做的就是改变这一行:

for(var k:int = 0; k<capeSkel.numChildren; k++)

到这个

for(var k:int = 0; k < tempVerteces.length; k++)

你做了很多事情,然后你真的需要做。 我对你的代码感到很困惑,我不得不重新构建它以便找到问题。 不知道你想要实现什么,但在这个简单的情况下,当你只移动一个顶点时,你不需要重写整个顶点矢量 - 你可以简单地替换一个项目(或者更确切地说是两个情况)。

以下是我的编辑,可以执行相同的任务以及拖动处理:

import flash.events.MouseEvent;
import flash.display.Sprite;

var cV:Sprite; //currently draged vertex
var cI:uint; //index of draged vertex
var verteces:Vector.<Number> = new Vector.<Number>();
var indeces:Vector.<int> = new Vector.<int>();
var uvtData:Vector.<Number> = new Vector.<Number>();
var capeBmd:BitmapData = new BitmapData(cape.width, cape.height, true);
var sprite:Sprite = new Sprite();

init();
renderView();

function init(){
    capeBmd.draw(cape);
    sprite.x = cape.x;
    sprite.y = cape.y;
    removeChild(cape);
    addChildAt(sprite,0);

    for(var i:int = 0; i<capeSkel.numChildren; i++){
        cV = capeSkel.getChildAt(i) as Sprite;
        cV.addEventListener(MouseEvent.MOUSE_DOWN, grabVert);
        verteces.push(cV.x, cV.y);
        uvtData.push(cV.x / cape.width, cV.y / cape.height);
    }
    indeces.push(0,10,5,  0,1,10,  1,2,10,  2,10,7,  5,6,10,  6,7,10,  2,11,7,  2,3,11,  3,4,11,       
    4,11,9,  7,8,11,  8,9,11);

    cV = null;
    cape = null;
    stage.addEventListener(MouseEvent.MOUSE_MOVE, moveVert);
    stage.addEventListener(MouseEvent.MOUSE_UP, clearVert);
}

function grabVert(e:MouseEvent){
    cV = Sprite(e.currentTarget);
    cI = cV.parent.getChildIndex(cV)*2;
}

function moveVert(e:MouseEvent){
    if(!cV) return;
    verteces[cI] = cV.x = cV.parent.mouseX;
    verteces[cI+1] = cV.y = cV.parent.mouseY;
    renderView();
}

function clearVert(e:MouseEvent){
    cV = null;
}

function renderView()
{
    var g:Graphics = sprite.graphics;
    g.clear();
    g.lineStyle(2, 0x00FF00);
    g.beginBitmapFill(capeBmd);
    g.drawTriangles(verteces,indeces,uvtData);
    g.endFill();
}