Skip to main content
Inspiring
February 7, 2017
Answered

Draggable object back to original position when already there is an object at the drop area.

  • February 7, 2017
  • 2 replies
  • 1503 views

I'm a newbie about flash, especially actionscript.

So the scenario is: I have 3 pairs of shoes, 2 ties, and 4 scouts.

I already made the 3 pairs of shoes and 2 ties draggable to any scouts (as the drop area).

But the problem is, I want to make it clear, that the user only may put a pair of shoes and a tie on 1 scout.

Because what I have now is, when already exist a pair of shoes and a tie on a scout, the user still can drop other on it.

Please help me, I've been surfing through internet and many forums but still can't solve this one. Many thanks before.

[Animate replaced Flash Pro]

[Moved from non-technical Forum Lounge to specific Program forum... Mod]

[Here is the list of all Adobe forums... https://forums.adobe.com/welcome]

This topic has been closed for replies.
Correct answer ClayUUID

this so far the code i have //this code is on tie1 on(press) { if (!this.isLocked) startDrag(this); } on(release){stopDrag(); if(this._droptarget=="/anak1")//anak1 is scout1 { this._x=163,95; this._y=401,45; var isLocked = true; var efeksound:Sound = new Sound(); efeksound.attachSound("plopp"); efeksound.start(); } else if(this._droptarget=="/anak2") { this._x=293,95; this._y=401,45; } else{ this._x=867,50; this._y=408,95; } }


Oh dear lord, let's clean that up...

// this code is on tie1

on (press) {

    if (!this.isLocked) {

        startDrag(this);

    }

}

on (release) {

    stopDrag();

    // anak1 is scout1

    if (this._droptarget == "/anak1") {

        this._x = 163.95;

        this._y = 401.45;

        var isLocked = true;

        var efeksound:Sound = new Sound();

        efeksound.attachSound("plopp");

        efeksound.start();

    }

    else if (this._droptarget == "/anak2") {

        this._x = 293.95;

        this._y = 401.45;

    }

    else {

        this._x = 867.50;

        this._y = 408.95;

    }

}

First problem I see is var isLocked = true;. That should be this.isLocked = true;.

2 replies

kglad
Community Expert
Community Expert
February 7, 2017

one way to do this:

each scout (assuming that's a movieclip) could have its own clothes object (eg, clothesObj) with a shoes property and a tie property.  if that property already exits return it to its start point and replace that clothesObj property with the just dropped one.

eg, initialize your objects:

for(var i:int=0;i<4;i++){

this['scout'+i].clothesObj={};

}

and in your drop function, if you drop shoes1 on scout3:

if(scout3.clothesObj.shoes){

scout3.clothesObj.shoes.x=scout3.clothesObj.shoes.initX;  // assuming you assigned initX and initY for your draggables

scout3.clothesObj.shoes.y=scout3.clothesObj.shoes.initY;

}

scout3.clothesObj.shoes=shoes1;

Inspiring
February 7, 2017

thanks for your help

but i see this code is about to place/snap the draggable object to the shoes position, isn;t it?

if yes, i already done with this, but still confuse about detect if an object is already exist on the drop zone or not.

if(scout3.clothesObj.shoes){

scout3.clothesObj.shoes.x=scout3.clothesObj.shoes.initX;  // assuming you assigned initX and initY for your draggables

scout3.clothesObj.shoes.y=scout3.clothesObj.shoes.initY;

}

scout3.clothesObj.shoes=shoes1;

Inspiring
February 7, 2017

copy and paste here your drop function code.


this so far the code i have //this code is on tie1 on(press) { if (!this.isLocked) startDrag(this); } on(release){stopDrag(); if(this._droptarget=="/anak1")//anak1 is scout1 { this._x=163,95; this._y=401,45; var isLocked = true; var efeksound:Sound = new Sound(); efeksound.attachSound("plopp"); efeksound.start(); } else if(this._droptarget=="/anak2") { this._x=293,95; this._y=401,45; } else{ this._x=867,50; this._y=408,95; } }

Legend
February 7, 2017

Should be trivial. Just set a variable on the dropped-on object to true when something is dropped on it. Then have your drop code check for that variable. If it's set, put it back.

Inspiring
February 7, 2017

alright, thanks, i get the logic from your idea, but i don't know what variable i should use

Legend
February 7, 2017

.idontknowwhatvariableishoulduse = true;

It literally doesn't matter. It's a variable name. You just make up whatever makes sense to you.