我正在制作一个具有多个级别和一个主页按钮的拖放动作脚本游戏,并且我将级别拖放到特定的目标站点,但是当我单击主页按钮时,被拖动的项目会留在屏幕上。我添加了 removeChild 但它仅适用于 1 个级别,当您单击另一个级别时它停止工作。
我拥有的代码是: 第 1 页
stop()
home.addEventListener(MouseEvent.CLICK, click_home);
btn1.addEventListener(MouseEvent.CLICK, click_btn1);
btn2.addEventListener(MouseEvent.CLICK, click_btn2);
btn3.addEventListener(MouseEvent.CLICK, click_btn3);
function click_home(event:MouseEvent):void
{
gotoAndStop(1);
removeChild (Qld);
removeChild (Nsw);
removeChild (Sa);
removeChild (Wa);
removeChild (Nt);
removeChild (Vic);
removeChild (Tas);
removeChild (Butter1);
removeChild (Butter2);
removeChild (Butter3);
removeChild (Butter4);
removeChild (Butter5);
removeChild (Dog);
removeChild (Cat);
removeChild (Squeek);
}
function click_btn1(event:MouseEvent):void
{
gotoAndStop(5);
}
function click_btn2(event:MouseEvent):void
{
gotoAndStop(10);
}
function click_btn3(event:MouseEvent):void
{
gotoAndStop(15);
}
第2页
var objectoriginalX:Number;
var objectoriginalY:Number;
buttonMode = true;
Qld.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Qld.addEventListener(MouseEvent.MOUSE_UP, dropObject);
Nsw.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Nsw.addEventListener(MouseEvent.MOUSE_UP, dropObject);
Sa.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Sa.addEventListener(MouseEvent.MOUSE_UP, dropObject);
Nt.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Nt.addEventListener(MouseEvent.MOUSE_UP, dropObject);
Wa.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Wa.addEventListener(MouseEvent.MOUSE_UP, dropObject);
Vic.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Vic.addEventListener(MouseEvent.MOUSE_UP, dropObject);
Tas.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Tas.addEventListener(MouseEvent.MOUSE_UP, dropObject);
Tas.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Tas.addEventListener(MouseEvent.MOUSE_UP, dropObject);
function pickupObject(event:MouseEvent):void {
event.target.startDrag(true);
event.target.parent.addChild(event.target);
objectoriginalX = event.target.x;
objectoriginalY = event.target.y;
}
function dropObject(event:MouseEvent):void {
event.target.stopDrag();
var matchingTargetName:String = "target" + event.target.name;
var matchingTarget:DisplayObject = getChildByName(matchingTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == matchingTarget){
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
event.target.buttonMode = false;
event.target.x = matchingTarget.x;
event.target.y = matchingTarget.y;
} else {
event.target.x = objectoriginalX;
event.target.y = objectoriginalY;
}
}
第 3 页
var objoriginalX:Number;
var objoriginalY:Number;
buttonMode = true;
Butter1.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Butter1.addEventListener(MouseEvent.MOUSE_UP, dropObject);
Butter2.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Butter2.addEventListener(MouseEvent.MOUSE_UP, dropObject);
Butter3.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Butter3.addEventListener(MouseEvent.MOUSE_UP, dropObject);
Butter4.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Butter4.addEventListener(MouseEvent.MOUSE_UP, dropObject);
Butter5.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Butter5.addEventListener(MouseEvent.MOUSE_UP, dropObject);
function pickupObj(event:MouseEvent):void {
event.target.startDrag(true);
event.target.parent.addChild(event.target);
objoriginalX = event.target.x;
objoriginalY = event.target.y;
}
function dropObj(event:MouseEvent):void {
event.target.stopDrag();
var matchingTargetName:String = "target" + event.target.name;
var matchingTarget:DisplayObject = getChildByName(matchingTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == matchingTarget){
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
event.target.buttonMode = false;
event.target.x = matchingTarget.x;
event.target.y = matchingTarget.y;
} else {
event.target.x = objoriginalX;
event.target.y = objoriginalY;
}
}
第4页
import flash.events.Event;
import flash.media.SoundChannel;
import flash.media.SoundMixer;
stop();
targetSqueek.addEventListener(MouseEvent.ROLL_OVER,playSqueek);
targetCat.addEventListener(MouseEvent.ROLL_OVER, playCat);
targetDog.addEventListener(MouseEvent.ROLL_OVER, playDog);
function playSqueek(event:Event){
SoundMixer.stopAll();
var mySound:SqueekSound=new SqueekSound();
var myChannel:SoundChannel=mySound.play();
}
function playCat(event:Event){
SoundMixer.stopAll();
var mySound2:CatSound=new CatSound();
var myChannel:SoundChannel=mySound2.play();
}
function playDog(event:Event){
SoundMixer.stopAll();
var mySound1:DogSound=new DogSound();
var myChannel:SoundChannel=mySound1.play();
}
var obj_1originalX:Number;
var obj_1originalY:Number;
buttonMode = true;
Dog.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Dog.addEventListener(MouseEvent.MOUSE_UP, dropObject);
Cat.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Cat.addEventListener(MouseEvent.MOUSE_UP, dropObject);
Squeek.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
Squeek.addEventListener(MouseEvent.MOUSE_UP, dropObject);
function pickupObj_1(event:MouseEvent):void {
event.target.startDrag(true);
event.target.parent.addChild(event.target);
obj_1originalX = event.target.x;
obj_1originalY = event.target.y;
}
function dropObj_1(event:MouseEvent):void {
event.target.stopDrag();
var matchingTargetName:String = "target" + event.target.name;
var matchingTarget:DisplayObject = getChildByName(matchingTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == matchingTarget){
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
event.target.buttonMode = false;
event.target.x = matchingTarget.x;
event.target.y = matchingTarget.y;
} else {
event.target.x = obj_1originalX;
event.target.y = obj_1originalY;
}
}
这是我单击主页时出现的错误 TypeError: Error #2007: Parameter child must be non-null. 在 flash.display::DisplayObjectContainer/removeChild() 在 Main/click_home()