Hi all
I got sort o stuck with a simple interaction I'm trying to do... Hope somebody can help me out
I have a button that everytime it is clicked, creates an instance of a MovieClip, puts it in an array and display it on screen
menu_event.addEventListener(MouseEvent.CLICK, clickHandler);
function newMcEvent():void
{
var clip:MovieClip = new mc_event(); //mc_event is the name of the Mc in the library
addChild(clip);
clip.x = 50;
clip.y = 350;
eventArray.push(clip);
trace(eventArray+clip.name);//this will show the array list plus the name of the last in, just for feedback
}
Keep in mind that the mc_event movieClip is draggable...
On stage I also have movieClip called bin which should delete whatever clip i'm dragging on it. The simplest I guess would be to have a function that says:
If mouse.target hits the bin, then removeChild(mouse.target)
Does it make sense?
here is may failed ***** of code for the above
bin.addEventListener(MouseEvent.CLICK, hit);
function hit(event:MouseEvent):void{
if(event.currentTarget.hitTestObject(bin)){
bin.myText.text = "ok";
}
}
When you assign the listener to the bin, that will end up being the currentTarget of the event, not the object you are dragging
A CLICK event involves two events occuring... a mousedown followed by a mouse up. I don't think you are clicking on the bin under these circumstances.
If the idea is to have the dragged object go away when it hits the bin, then you should assign a listener that constantly checks to see if that's the case, such as a MOUSE_MOVE listener along with your hitTest code. Assign the listener to the object your are dragging.
If you want to drop the object on the bin and then have it disappear, then use a dropTarget test instead of a hit test
@Ned: I just read you message I'll try the dropTarget test in a second...
In the meantime I thought this might work as well... every new event_obj I create in the main timeline, will also automatically create an area on screen called bin (which is in fact another Movieclip)...
Then I create a if statement inside event_obj that says
if event_obj (the mc I am dragging around) hits the bin movieClip, then remove event_obj...
something like this
// Register mouse event functions
this.addEventListener(MouseEvent.MOUSE_DOWN, drag);
this.addEventListener(MouseEvent.MOUSE_UP, noDrag);
var bin:MovieClip = new mc_bin();
addChild(bin);
bin.x = 350;
bin.y = 350;
// Define a mouse down handler (user is dragging)
function drag(event:MouseEvent):void {
var dragged = event.target;
dragged.startDrag();
if(dragged.hitTestObject(bin)){
trace("ok");
}
}
does that make sense?
addition...
// Register mouse event functions
this.addEventListener(MouseEvent.MOUSE_DOWN, drag);
this.addEventListener(MouseEvent.MOUSE_UP, noDrag);
var bin:MovieClip = new mc_bin();
addChild(bin);
bin.x = 350;
bin.y = 350;
// Define a mouse down handler (user is dragging)
function drag(event:MouseEvent):void
{
var dragged = event.target;
// we should limit dragging to the area inside the canvas
dragged.startDrag();
}
function noDrag(event:MouseEvent):void
{
var notDragging = event.target;
notDragging.stopDrag();
while (notDragging.hitTestObject(bin))
{
trace(notDragging.x);
removeChild(notDragging);
removeChild(bin);
}
}
The above code works...
@ned: still trying your suggestion!
kudos again
It won't work the way you have it because it is not repeatedly looking for the hitTest. It only checks that one time which is likely before you get a chance to even move the object. To do the hitTest such that it registers as soon as a hit occurs, you need to be constantly checking. Doing the hitTest in the stopDrag end of things could work that way but not at the starting end before the object gets moved.
yes! I did it!!!!!!
I revised the code and decided to remove the code inside the event_obj movie clip and put all in the main time line... I did this because I couldn't access the right informations outside the MovieClip itself to remove movieClips... The new code is simpler...
So, now, each time I create an event_Obj by clicking on a button on stage:
1)it places the new movieClip inside an array (eventArray)
2) it goes through the array again and adds three eventListener, respectively for dragging, notDragging and Touching
The Touch function simply checks wether the movie clip that is currently selected is actually touching an OBJ on stage called bin....if yes it returns a string in the output window (again just for feedback)...
Now I'll have to add a line in it to remove the targeted obj... shouldn't be hard...
//event's public methods;
var eventArray = new Array();
function newMcEvent():void
{
var clip:MovieClip = new mc_event();
addChild(clip);
clip.x = 50;
clip.y = 350;
eventArray.push(clip);
for (var i:int = 0; i<eventArray.length; i++)
{
eventArray[i].addEventListener(MouseEvent.MOUSE_UP, touch);
eventArray[i].addEventListener(MouseEvent.MOUSE_DOWN, drag);
eventArray[i].addEventListener(MouseEvent.MOUSE_UP, noDrag);
trace(eventArray);
}
}
function drag(e:MouseEvent):void
{
var dragged = e.target;
e.target.startDrag();
}
function noDrag(e:MouseEvent):void
{
var notDragging = e.target;
notDragging.stopDrag();
}
function touch(e:MouseEvent):void
{
trace("grrrR");
if (e.target.hitTestObject(this.bin))
{
removeChild
trace("ok");
}
}
here it is.... Works liek a charm
function touch(e:MouseEvent):void
{
if (e.target.hitTestObject(this.bin))
{
//trace("FUOCO!!!!");
var len:int = this.numChildren;
for (var i:int = 0; i < len; i++)
{
var display:DisplayObject = this.getChildAt(i);
trace("display "+ display.name);
}
var g:String = e.target.name;//get the name of e.target to g
var nameOf:DisplayObject = this.getChildByName(g);//finds g inside list
trace("eve is in position "+this.getChildIndex(nameOf));
//trace("g is "+ g);
var indx:int = this.getChildIndex(nameOf);
//trace(indx);
removeChildAt(indx);
}
else
{
trace("acqua");
}
}
North America
Europe, Middle East and Africa
Asia Pacific