【问题标题】:Drag and drop multiple items to one target将多个项目拖放到一个目标
【发布时间】:2016-12-07 01:54:46
【问题描述】:

我找到了一个 swf 文件,它可以做我想做的事情。所以我下载了它并反编译了它,这样我就可以学习如何正确地做到这一点。然后把它应用到我正在做的事情上。 应该有很多项目,个人将其拖放到正确的类别中。如果释放时正确,则保留,如果不正确,则返回开始。What it looks like

我对原始文件进行了一些更改,因为它不起作用。您可以在 (onClipEvent) 中看到原始代码的位置。 我似乎已经解决了大多数问题,但我无法将拖动的项目释放到正确的位置。 我还为精灵添加了动作脚本。每个彩色三角形从左到右从 1 到 5 编号(希望这是正确的) 谢谢你的帮助。一个沮丧的科学老师。

    // Action script…

// [onClipEvent of sprite 2 in]
//onClipEvent (load)
this.addEventListener(Event.ENTER_FRAME, this.loading);
function loading(e:Event)
{
this.numItems = 2;
}

// [onClipEvent of sprite 2 in]

//onClipEvent (load)
this.addEventListener(Event.ENTER_FRAME, loading);
function loading(event:Event){
this.numItems = 3;
}

// [onClipEvent of sprite 2 in]

//onClipEvent (load)
this.addEventListener(Event.ENTER_FRAME, loading);
function loading(event:Event){
this.numItems = 3;
}

// [onClipEvent of sprite 8 in]

//onClipEvent (load)
this.addEventListener(Event.ENTER_FRAME, loading);
function loading(event:Event){
    this.defx = _x;
    this.defy = _y;
    if (this.theText.text.length > 20)
    {
        this.theText._height = 31;
        this.theBox._height = 27;
    }
    else
    {

        this.theText._height = 19;
        this.theBox._height = 19;
    } // end else if
}

// [onClipEvent of sprite 8 in frame 1]

//on (press)
this.addEventListener(MouseEvent.MOUSE_DOWN, pressed);
function pressed(event:Event){
    if (this.noDrag != true)
    {
        startDrag (this, false);
    } // end if
}

// [onClipEvent of sprite 8 in frame 1]

//on (release)
this.addEventListener(MouseEvent.MOUSE_UP, relea);
function relea(event:Event){
    if (this.noDrag != true)
    {
        stopDrag ();
        if(this.hitTest(_root["dz" + this.answer]))
        {
            totalHeight = 0;
            for (v = 0; v < _root.dbCount; v++)
            {
                if (_root["dbutton" + v].answer == this.answer)
                {
                    totalHeight = totalHeight + _root["button" + v].theBox._height ;
                } // end if
            } // end of for
            ++_root.dbCount;
            this .duplicateMovieClip("dbutton" + _root.dbCount, _root.dbCount * 100);
            _root["dbutton" + _root.dbCount]._x = this.defX;
            _root["dbutton" + _root.dbCount]._y = this.defY;
            _root["dbutton" + _root.dbCount].answer = _root.answerdest[_root.dbCount + 1];
            _root["dbutton" + _root.dbCount].theText.text = _root.answername[_root.dbCount +1];
            if (_root["dbutton" + _root.dbCount].theText.text == "undefined")
            {
                _root["dbutton" + _root.dbCount].theText.text = "Finished!";
                _root["dbutton" + _root.dbCount].noDrag = true;
            } // end if
            this.noDrag = true;
            this._y = _root["dz" + this.answer]._y + totalHeight;
            this._x = _root["dz" + this.answer]._x - _root["dz" + this.answer]._width / 2;
            ++_root["dz" + this.answer].numItems;
            _root.glamp1.gotoAndPlay (1);
        }
        else
        {
            this.x = this.defX;
            this._y = this.defY;
            _root.rlamp1.gotoAndPlay(1);
        } // end if
    } // end else if
}

// [onClipEvent of sprite 2 in frame 1]

//onClipEvent (load)
this.addEventListener(Event.ENTER_FRAME, loading);
function loading(event:Event){
this.numItems = 2;
}

// [onClipEvent of sprite 2 in frame 1]

//onClipEvent (load)
this.addEventListener(Event.ENTER_FRAME, loading);
function loading(event:Event){
this.numItems = 3;
}

// [Action in Frame 1]

answername = Array();

answerdest = Array();

answername[0] = "gravel";

answerdest[0] = "1";
answername[1] = "water";

answerdest[1] = "2";

dbCount = 0;

dbutton.duplicateMovieClip("dbutton" + dbCount,dbCount * 100);

dbutton.visible = false;

dbutton0.answer = answerdest[dbCount];

dbutton0.theText.text = answername[dbCount];

【问题讨论】:

  • "我无法将拖动的项目释放到正确的位置。"这无助于我们理解问题。告诉我们与您观察到的情况相比究竟发生了什么。有错误信息吗?您是否尝试使用调试器运行它?此外,听起来您想要的目标是一个非常简单的目标。为什么要反编译这个?从头开始制作脚本执行您所描述的操作很容易。我会从头开始,而不是对别人的代码进行逆向工程。但任何适合你的。
  • 当我释放鼠标时,它不会将拖动的项目放到目标上。相反,它与鼠标保持一致。其次,我想从头开始,但找不到如何将三个项目设置为一个目标并将另一个不同的 3 个项目设置为不同目标的示例。这是我能找到的最接近的例子。我在动作脚本编码方面相对较新。

标签: actionscript-3 drag-and-drop targets


【解决方案1】:
//on (release)
this.addEventListener(MouseEvent.MOUSE_UP, relea);
function relea(event:Event){

应该是

//on (release)
this.addEventListener(MouseEvent.MOUSE_UP, relea);
function relea(event:MouseEvent){ // **** MouseEvent, not Event

你看到了吗?您告诉relea 函数期待Event。它应该期待MouseEvent。这样能解决吗?

【讨论】:

  • 感谢您发现该错误,但不幸的是它并没有解决问题。当我松开按钮时仍然不会释放拖动的对象。
  • 关于“this.addEventListener”什么是“this”?它应该是你要发布的东西。您可以在调试模式下添加一个跟踪(此)并告诉我它在输出窗口中显示的内容。
  • 在输出下是显示 _level0 这对你有意义吗?我以为“这个”是我正在移动的文本框
  • 有没有一种方法可以共享实际的 fla 文件,因为这可能会有所帮助,以防万一错误出现在我放置在其中一个精灵上的代码中?我是这个网站的新手,不确定是否允许这样做?
  • _level0 是主时间线或类似的东西。所以代替 this.addEventListener 做 myTextBox.addEventListener
【解决方案2】:

感谢您的帮助,我发现这个问题的答案是我最终将相同的代码放在精灵动作框架和主时间线动作框架中,它应该只在精灵中。

【讨论】:

    猜你喜欢
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-29
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    相关资源
    最近更新 更多