0

我在这个 AS3 项目中不知所措。

我不是动作脚本专家,当然也不是 AS3 专家,但我设法创建(在此帮助下:http ://swamy-techtalk.blogspot.com/2011/07/elastic-string-to-mouse-pointer-effect .html ) 从点到可拖动影片剪辑的弹性字符串效果。

问题是脚本在我测试时似乎使 Flash 或浏览器崩溃。(不是在我玩电影剪辑时立即)

辛西我在我编译的脚本中不知所措,我不完全确定出了什么问题。一些谷歌研究暗示它可能与 removeChildAt() 有关,我从 removeChildAt(0) 更改为 removeChildAt(1) 以防止它删除我的电影剪辑。

希望有人有耐心通读我的脚本,看看我做错了什么。

此处的示例:http: //www.madsringblom.dk/flash/pullstring.html (请注意它可能会使您的浏览器崩溃)下面的代码:

Object(this).leaf_mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); 

var origX:int = Object(this).leaf_mc.x + 1; 
var origY:int = Object(this).leaf_mc.y + 2; 
var pullbackX:int = Object(this).leaf_mc.x; 
var pullbackY:int = Object(this).leaf_mc.y; 
var dragging:Boolean = false; 

//speed of pulling and rotating back when stop dragging. 

var speed:int = 15; 
var addX:int = 2 
var addY:int = 3 

function mouseDownHandler(e:MouseEvent):void 
{ 
    var obj = e.target; 
    obj.startDrag(); 
    dragging = true; 

} 

function mouseUpHandler(e:MouseEvent):void 
{  
    var obj = e.target; 
    Object(this).leaf_mc.stopDrag(); 
    dragging = false; 

} 

import flash.display.*; 
import flash.events.MouseEvent; 
import flash.events.Event; 

var haschild:Boolean = false; 
var gotonodes:Array = new Array(); 
var currentnodes:Array = new Array(); 

var posX:int = Object(this).leaf_mc.x + addX; 
var posY:int = Object(this).leaf_mc.y + addY; 
gotonodes = Interpolate(posX,posY,origX,origY,25); 
currentnodes = gotonodes; 
stage.addEventListener(Event.ENTER_FRAME, onmove1); 

function onmove1(e:Event)
{ 
    for (var node = 0; node < gotonodes.length - 1; node++)
    { 
        currentnodes[node].xco=currentnodes[node].xco+(gotonodes[node].xco-currentnodes[node].xco)/(node*node/30+1); 
        currentnodes[node].yco=currentnodes[node].yco+(gotonodes[node].yco-currentnodes[node].yco)/(node*node/30+1); 
    }

    var posX:int = Object(this).leaf_mc.x + addX; 
    var posY:int = Object(this).leaf_mc.y + addY; 
    gotonodes=Interpolate(posX,posY,origX,origY,25); 

    // pull leaf_mc back to starting point when released. And rotate back. 

    if (dragging == false)
    { 
        Object(this).leaf_mc.x-=(Object(this).leaf_mc.x-pullbackX)/speed; 
        Object(this).leaf_mc.y-=(Object(this).leaf_mc.y-pullbackY)/speed; 
        Object(this).leaf_mc.rotation+=Object(this).leaf_mc.rotation/speed; 
    } 

    // rotating the leaf_mc according to the point (origX,origY) 

    var theX:int = origX - Object(this).leaf_mc.x; 
    var theY:int = (origY - Object(this).leaf_mc.y) * -1; 
    var angle = Math.atan(theY/theX)/(Math.PI/180); 
    Math.atan( -5 / 10) / (Math.PI / 180); 

    if (theX < 0) 
    { 
        angle += 180; 
    } 

    if (theX >= 0 && theY < 0) 
    { 
        angle += 360; 
    } 

    Object(this).leaf_mc.rotation = (angle*-1) + 90; 

    DrawNodes(currentnodes); 

} 

function FindAngle(x1, x2, y1, y2):Number 
{ 
    return Math.atan2(y2-y1, x2-x1); 
}; 

function Distance(x1, x2, y1, y2):Number
{ 
    return Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2)); 
} 

function Interpolate(x1, y1, x2, y2, n):Array
{ 
    var dist= Distance(x1,x2,y1,y2); 
    var ang = FindAngle(x1,x2,y1,y2); 
    var points = [];

    for (var l = 0; l <= dist; l += dist / n)
    { 
        var x3 =x1+l*Math.cos(ang); 
        var y3 = y1+l*Math.sin(ang); 
        points.push({xco:x3,yco:y3}); 
    } 

    points.push( { xco:x1, yco:y1 } ); 

    return points; 

} 

function DrawNodes(array):void
{ 
    if(haschild) 
    { 
        this.removeChildAt(1); 
        haschild=false; 
    } 

    var shape:Shape = new Shape(); 
    shape.graphics.lineStyle(1,0x331100,40); 
    shape.graphics.moveTo(array[0].xco, array[0].yco);

    for (var i = 0; i < array.length - 1; i++)
    { 
        shape.graphics.lineTo(array[i].xco,array[i].yco); 
    } 

    shape.graphics.beginFill(0xFBFFA4,1); 
    shape.graphics.drawCircle(array[0].xco,array[0].yco,1); 
    shape.graphics.endFill(); 
    this.addChild(shape); 
    haschild = true;

} 
4

1 回答 1

0

这段代码放在哪里?从你粘贴的内容来看,我在舞台上思考。

脚本中的stop();某个地方可能是一个开始并有很大帮助 - 否则 Flash 电影将循环播放,并且每次它到达这一帧(如果你只有一个帧,则每一帧),你将添加新的事件处理程序等。最终你'将耗尽内存,onmove1事件处理程序将耗尽你的 CPU,在 50 帧后每帧运行 50 次,在 200 帧后运行 200 次等。

于 2012-02-05T20:54:50.423 回答