vendredi 16 décembre 2016

hittestobject multiple functions with Drag and drop

I have troubles making this code functionnal, i am pretty new to coding and learn everyday, so sorry if this question looks stupid.

Here's where I am so far :

  • a1 is a drag and drop object
  • a2 is an area, which when a1 is dropped on it, add the value 5 on a textfield
  • a3 is an area, which when a1 is dropped on it, substract the value 5 on a textfield

a1 start in the a3 area, when I drag a1 to a2, it shows 5, if I drag back to a3, it shows 0, so it's working as intended


Now what I struggle with :

  • a code that don't add 5 if I move a1 to a2, when a1 is ALREADY on a2.
  • a code that don't substract 5 if I move a1 to a3, when a1 is ALREADY on a3.

here's my code :

var myTextBox:TextField = new TextField();  

var myTextBox2:TextField = new TextField(); 
addChild(myTextBox2); 
myTextBox2.width = 200;    
myTextBox2.height = 100;    
myTextBox2.x = 100;    
myTextBox2.y = 20; 

a1.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);

function drag(e:MouseEvent):void
{
e.target.startDrag();
}

function drop(e:MouseEvent):void
{
stopDrag();
if (a1.hitTestObject(a2))
{
    myTextBox.text = "5";
    var r:String=String(uint(myTextBox.text)+uint(myTextBox2.text));
    myTextBox2.text=r;
}
if (a1.hitTestObject(a3))
{
    myTextBox.text = "5";
    var r2:String=String(uint(myTextBox2.text)-uint(myTextBox.text));
    myTextBox2.text=r2;
}

else
{
    trace("No collision.");
}
}

I don't think it's too complicated, but I don't have the knowledge yet to make it work, any assistance would be greatly appreciated ! thanks!

Aucun commentaire:

Enregistrer un commentaire