原谅邋code的代码,它处于初始阶段,因此没有被清理过多,请随意提出一些方法来清理代码或提高代码效率。
我正在构建一个拖放PC构建部分作为ICT教育Flash应用程序的一部分,而不是在整个时间轴上都有大量的框架和代码我希望将所有不同的部分作为独特的影片剪辑。
无论如何,当我在标准flash时间轴中使用它时,代码可以工作,但是当我将对象转换为影片剪辑并将代码放入影片剪辑中时,这些项目可以拖动但不会进入其插槽,或者“ dropZones“
这是我的代码:
import flash.events.MediaEvent;
import flash.display.MovieClip;
import flash.media.Sound;
var draggableObjects:Array = new Array(hdd, wifi, gpu, cpu, ram);
// variables for draggable objects positions
var hdd_x:Number = hdd.x;
var hdd_y:Number = hdd.y;
var wifi_x:Number = wifi.x;
var wifi_y:Number = wifi.y;
var gpu_x:Number = gpu.x;
var gpu_y:Number = gpu.y;
var cpu_x:Number = cpu.x;
var cpu_y:Number = cpu.y;
var ram_x:Number = ram.x;
var ram_y:Number = ram.y;
// creates event listner for every draggable object and sets them to use button mode
for each (var object in draggableObjects) {
object.addEventListener(MouseEvent.MOUSE_DOWN, dragObject);
object.addEventListener(MouseEvent.MOUSE_UP, objectRelease);
object.buttonMode = true;
}
function dragObject(event:MouseEvent):void {
var item:MovieClip = MovieClip(event.target);
item.startDrag();
var topPos:uint = this.numChildren-1;
this.setChildIndex(item, topPos);
}
function objectRelease(event:MouseEvent):void {
var item:MovieClip = MovieClip(event.target);
var click:Sound;
if (item == hdd) {
var original_x:Number = hdd_x;
var original_y:Number = hdd_y;
var dropZone:MovieClip = hddTarget;
} else if (item == wifi) {
var original_x:Number = wifi_x;
var original_y:Number = wifi_y;
var dropZone:MovieClip = wifiTarget;
} else if (item == gpu) {
var original_x:Number = gpu_x;
var original_y:Number = gpu_y;
var dropZone:MovieClip = gpuTarget;
} else if (item == cpu) {
var original_x:Number = cpu_x;
var original_y:Number = cpu_y;
var dropZone:MovieClip = cpuTarget;
} else if (item == ram) {
var original_x:Number = ram_x;
var original_y:Number = ram_y;
var dropZone:MovieClip = ramTarget;
} else {
return;
}
item.stopDrag();
if (dropZone.hitTestPoint(item.x,item.y)) {
click = new clickSound();
click.play();
item.x = dropZone.x;
item.y = dropZone.y;
} else {
item.x = original_x;
item.y = original_y;
}
}