Dragging onto the wrong object
Hi there,
Simple request, I have some code that I got from here originally which lets me drag objects onto baskets
// mouse down event
function onMouseDown1(evt){
var item = evt.currentTarget;
item.offset = {x:0, y:0};
var pt = item.parent.globalToLocal(evt.stageX, evt.stageY);
item.offset.x = pt.x - item.x;
item.offset.y = pt.y - item.y;
item.drag = true;
}
// mouse up event
function onMouseUp1(evt){
var item = evt.currentTarget;
item.drag = false;
var pt = item.localToLocal(item.dot.x, item.dot.y, this.basket2.hitBox);
var pt2 = item.localToLocal(item.dot.x, item.dot.y, this.basket1.hitBox);
if(this.basket2.hitBox.hitTest(pt.x, pt.y) ){
item.x = this.basket2.x;
item.y = this.basket2.y;
item.gotoAndPlay('endState');
createjs.Tween.get(item)
.wait(500)
.to({alpha:0, visible:false}, 1000);
item.mouseEnabled = false; // prevents object from being move when place correctly
finish();
} else if (this.basket1.hitBox.hitTest(pt2.x, pt2.y) ){
item.gotoAndPlay('wrongBox');
}
}
// mouse move event
function onMouseMove1(evt){
var item = evt.currentTarget;
if (item.drag){
var pt = item.parent.globalToLocal(evt.stageX, evt.stageY);
item.x = pt.x - item.offset.x;
item.y = pt.y - item.offset.y;
}
}
In this code, I have added my own else if which checks to see if the item has hit the wrong basket. If it has, it plays its own movie clip.
The part where I need help - how would I get the item to snapback to it's original position?
Cheers