弹性字符串脚本使闪存崩溃

时间:2012-02-05 11:44:31

标签: actionscript-3 flash removechild

我在这个AS3项目中处于领先地位。

我不是动作脚本中的专家,当然也不是AS3中的专家,但我设法创建(在此帮助下:http://swamy-techtalk.blogspot.com/2011/07/elastic-string-to-mouse-pointer-effect.html)从一个点到一个可拖动的动画片段的元素效果。

问题是我测试时脚本似乎崩溃了闪存或浏览器。 (当我正在玩电影剪辑时,不是马上就开始了)

Sinse我在编写的脚本中已经超出了我的头脑我不确定是不是错了。 一些谷歌研究暗示它可能与removeChildAt()有关,我从removeChildAt(0)更改为removeChildAt(1)以防止它删除我的movieclip。

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

此处示例: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;

} 

1 个答案:

答案 0 :(得分:0)

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

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