InventorySlot (the class) is a subclass of DragTarget. Inside DragTarget, the function configUI() adds dragBegin and dragEnd as event listeners to these slots. When the event dragBegin occurs, it fires off the function dragBegin() found in DragTarget.
Now, I moved your trace statement inside this function at the bottom, and it worked properly. The trace statement occurs (multiple times however - couldn't tell you why), and the original inventory slot is not disabled. Here is the code from DragTarget as I modified the class. The last command is your trace statement:
Code:
private function dragBegin(event:Object):Void {
if (!allowDrop(event.data)) { return; }
trackAsMenu = _visible = true;
gotoAndStop("up");
gotoAndStop("dragUp");
trace("This drag works!!");
}
However, you could also put the trace statement inside the onPress() function that lives in InventorySlot, and it will work just as well, and will only fire once.
Code:
private function onPress(controllerIdx:Number):Void {
if (_data == null) { return; }
Selection.setFocus(this, controllerIdx);
icons._alpha = 20;
dragOrigin = true;
DragManager.instance.startDrag(this, "icons", data, this, icons).gotoAndStop(_data.asset);
trace("This drag works!!");
_data = null;
}
So, long story short - you're going to want to modify the class for your needs, rather than trying to do code in the Flash timeline. Otherwise, you're going to break things.
Now - if you comment out the line _data = null in onPress(), you will in fact create an instance after dragging an object to a new slot, because this one line of code in onPress() tells the inventory slot you just dragged from to be made empty, once the function update() executes. Notice in update() it checks the value of _data. If it is null, then it sets that inventory slot (represented by _data) to frame 1, which is the empty icon slot.
But this is a hack that you don't want to use, as it creates an instance of the object in every inventory slot you drag from infinitely. So you're going to need to figure out how to make it work only in a specific instance - probably with an if statement of some kind.
Take this example 'if' statement I just added to the onPress() function:
Code:
private function onPress(controllerIdx:Number):Void {
if (_data == null) { return; }
Selection.setFocus(this, controllerIdx);
icons._alpha = 20;
dragOrigin = true;
DragManager.instance.startDrag(this, "icons", data, this, icons).gotoAndStop(_data.asset);
if (_data.quantity == 1 || _data.quantity == null) {
_data = null;
}
else {
_data.quantity -= 1;
}
}
This code checks to see if a dragged item has a quantity of 1 or null. If it does then it clears the data and does not allow an instance to be created. But if the quantity is higher than 1, when you drag the item to a new slot, it will leave behind an instance with a quantity of 1 less than before. The only problem with this code is that it does not give the correct quantity of the new instance that was dropped in another slot, but it illustrates the point I'm making.
What I would do is create a new subclass of InventorySlot - something along the lines of MyInventorySlot. Or I would just modify InventorySlot. Then I would modify it to my hearts content. Any function I wanted to modify from DragTarget (such as dragBegin()) I would override by copying it into my subclass and modifying it there.
Bookmarks