1

我正在制作一个具有多个级别和一个主页按钮的拖放动作脚本游戏,并且我将级别拖放到特定的目标站点,但是当我单击主页按钮时,被拖动的项目会留在屏幕上。我添加了 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()

4

1 回答 1

0

在查看您的代码时(除非您省略了某些内容),我认为您正在使用 WYSIWYG 放置视觉元素。有时,删除子元素可能会让人头疼。我几乎遇到了同样的问题。

我不知道您的项目,但这只是一种可能使其更容易的替代方法。喜欢就拿走,不喜欢就放手……

将主屏幕的所有元素放入一个影片剪辑中,并在您的库中将其命名为“Home_Screen”。(这里假设是 Flash Professional。这在 Flash Builder 中会有所不同。)类似地,将游戏的所有元素(或者可能是每个级别)放在另一个名为“Level_One”或“Main_Game”的 MovieClip 中.

现在,将这些影片剪辑放在舞台上,并连接您的按钮以在这些较大的影片剪辑之间切换。

我公司的标准是在每个屏幕电影剪辑上都有两个功能。一个函数,通常称为“display()”,包含该行this.visible = true;并调用启动声音所需的任何代码行等。另一个“hide()”包含该行this.visible = false;,并停止声音、动画等以释放内存。

当您的程序第一次启动时,它应该在您的主屏幕对象上调用“display()”,并在所有其他对象上调用“hide()”。设置您的按钮以在屏幕对象上使用 display() 和 hide() 函数。

(注意:如果您通过代码绘制所有内容,则可以采用类似的方法,但由于我在视觉上做大多数事情是为了我的动画师,这是我所知道的方法。)

(注意#2:有一个普遍的神话,将代码放在时间轴上是“不好的做法”。这个原则是 AS2 时代的遗物,如果您创建所见即所得的视觉元素,则不再严格相关。一般规则经验之谈:如果直接修改了一个symbol,就放到那个symbol的时间线上。如果是纯代码,就放到一个类里。)

于 2014-01-04T01:19:18.080 回答