2014-10-02 125 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(); 
    }