2014-10-02 94 views
0

我正在構建一個簡單的Flash遊戲,其中用戶可以從工具箱中拖動對象並拖動到某個區域。在拖動對象時,我創建了該對象的克隆並將其放置在該區域上。這裏是我的代碼的作品有點細..當我拖累該地區一個對象,然後我拖到一個更上一個對象上Actionscript3克隆和拖動對象其他

package { 

imports... 

public class DocumentClass extends MovieClip { 

    //variables... 

    var someArray:Array = new Array(); 
    var totalObjs = 5; 
    var objOnStage:Array = new Array(); 
    var ogx; 
    var ogy; 


    public function DocumentClass() { 
     // constructor code 
     stage.scaleMode = StageScaleMode.NO_SCALE; 
     stage.align = StageAlign.TOP_LEFT; 
     _width = stage.stageWidth; 
     _height = stage.stageHeight; 
     setGameObjects(); 
    }//constructor close 

    //scaling Functions 
    public function scalingheight(clip:MovieClip, percent:Number){ 
     var ratio = clip.width/clip.height; 
     clip.height = percent * _height; 
     clip.width = clip.height * ratio; 
    } 

    public function scalingwidth(clip:MovieClip, percent:Number){ 
     var ratio = clip.height/clip.width; 
     clip.width = percent * _width; 
     clip.height = clip.width * ratio; 
    } 

    public function setGameObjects():void{ 
     mc_toolbox = new mctoolbox; 
     addChild(mc_toolbox); 
     mc_toolbox.x = mc_toolbox.y = 0; 
     scalingwidth(mc_toolbox, 0.08); 
     mc_toolbox.height = _height; 

     mc_board = new mcboard; 
     addChild(mc_board); 
     mc_board.x = mc_toolbox.width; 
     mc_board.y = 0; 
     scalingwidth(mc_board, 0.75); 
     mc_board.height = _height; 

     mc_optbox = new mcoptbox; 
     addChild(mc_optbox); 
     scalingwidth(mc_optbox, 0.10); 
     mc_optbox.height = _height; 
     mc_optbox.x = _width - mc_optbox.width; 
     mc_optbox.y = 0;     

     setobjects(); 
    } 

    public function setobjects():void{ 
     for(var i = 1; i <= totalObjs; i++){ 
     var className:String = "obj" + i; 
     var ClassReference:Class = getDefinitionByName(className) as Class; 
     var myInstance = new ClassReference; 
     addChild(myInstance);   
     scalingwidth(myInstance, 0.08); 
     someArray.push(myInstance); 
     myInstance.x = stage.stageWidth - 0.09 * _width; 
     myInstance.y = myInstance.height * (i-1);   
     } 

     for(var i = 1; i <= totalObjs; i++){ 
     someArray[i-1].addEventListener(MouseEvent.MOUSE_DOWN, startMove);      
     someArray[i-1].addEventListener(MouseEvent.MOUSE_UP, stopMove);   
     } 
    } 

    function startMove(evt:MouseEvent):void { 
     //clone your movieclip here    
     ogx = evt.currentTarget.x; 
     ogy = evt.currentTarget.y; 
     evt.currentTarget.startDrag(); 
    } 

    function stopMove(evt:MouseEvent):void { 
     var newobj = new evt.currentTarget.constructor; 
     addChild(newobj); 
     newobj.width = evt.currentTarget.width; 
     newobj.height = evt.currentTarget.height; 
     newobj.x = evt.currentTarget.x; 
     newobj.y = evt.currentTarget.y; 
     evt.currentTarget.x = ogx; 
     evt.currentTarget.y = ogy; 
     evt.currentTarget.stopDrag(); 
     objOnStage.push(newobj); 
    } 

}//class close 
}//package close 

我的問題就出現了。在這種情況下,mouse up事件不會被調用,因此對象movieclip不會自行克隆。我想我犯了一些愚蠢的錯誤,請指導。

+0

這是否一貫發生拖動,或者當你的第二個目的是更接近陣列的頂部它只是發生比第一個? – Brian 2014-10-02 15:50:23

+0

我解決了這個問題。只有當我第一個拖動第二個影片剪輯然後離開鼠標時纔會發生。需要使用numChildren() – 2014-10-03 04:29:08

回答

0

嘗試,掉落物體在舞臺上像這樣之後,除去MouseEvent.MOUSE_UP

evt.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, stopMove);

+0

不會幫助:( – 2014-10-02 15:05:54

1

我得到這個解決。第二個影片剪輯問世的第一後面,所以我只是把它在所有其他影片剪輯之前,而採用這種

function startMove(evt:MouseEvent):void { 
     //clone your movieclip here    
     ogx = evt.currentTarget.x; 
     ogy = evt.currentTarget.y; 
     var myobj = evt.currentTarget as DisplayObject; 
     setChildIndex(myobj, numChildren - 1); // < The solution 
     evt.currentTarget.startDrag(); 
    }