mardi 6 décembre 2016

Drag and drop multiple items to one target

I found a swf file that does what I am trying to do. So I down loaded it and decompiled it so i could learn how to do it correctly. Then apply that to what I am working on. There are supposed to many items and the individual is to drag and drop it in to the correct catagory. If correct when it is released it stays, if incorect it moves back to start.What it looks like

I have made a couple of changes to the original file as it did not work. You can see where there was original code as in (onClipEvent). I seem to have fixed most problems but I can not release the dragged item onto the correct spot. I have also added action script to the sprites. Each colored triangle numbering them from one to 5 from left to right(hope this was correct) Thanks for your help. A frustrated science teacher.

    // 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];

Aucun commentaire:

Enregistrer un commentaire