Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Problem Cursor disappears / Drag and Drop doesn't work

New Here ,
Dec 05, 2009 Dec 05, 2009

Hey everybody,

I have an urgent problem!

Three butterflies are supposed to be dragged in an object.  My customized cursor is sort of a landing net to catch these butterflies.


For the cursor I have this code:

cursor_mc.startDrag("true");
Mouse.hide();

My drag and drop code is:

cursor_mc.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
redbutterfly_mc.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
violetbutterfly_mc.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
yellowbutterfly_mc.addEventListener(MouseEvent.MOUSE_DOWN, startMove);

cursor_mc.addEventListener(MouseEvent.MOUSE_UP, stopMove);
redbutterfly_mc.addEventListener(MouseEvent.MOUSE_UP, stopMove);
violetbutterfly_mc.addEventListener(MouseEvent.MOUSE_UP, stopMove);
yellowbutterfly_mc.addEventListener(MouseEvent.MOUSE_UP, stopMove);


function startMove(evt:MouseEvent):void {
   redbutterfly_mc.startDrag();
   cursor_mc.startDrag();
   violetbutterfly_mc.startDrag();
   yellowbutterfly_mc.startDrag();
}

function stopMove(evt:MouseEvent):void {
   redbutterfly_mc.stopDrag();
   cursor_mc.stopDrag();
   violetbutterfly_mc.stopDrag();
   yellowbutterfly_mc.stopDrag();
}

But when I'm clicking on a butterfly my cursor stops to move and stays on the position of the butterfly, which i'm dragging. After dropping it, there is no cursor at all so one doesn't know where the mouse is currently located.

Does someone know why this doesn't work? I'm working currently with a mousefollower, because at the moment this is the only way it works..

The mousefollower code is:

addEventListener(Event.ENTER_FRAME, enterFrameHandler)

function enterFrameHandler(event:Event):void {
    cursor_mc.x += cursor_mc.mouseX / 4;
    cursor_mc.y += cursor_mc.mouseY / 4;
}

But now I have the problem that only the yellow butterfly moves even if I'm clicking on the red or the violet...

Please help me!! Thank you in anticipation!

TOPICS
ActionScript
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Dec 06, 2009 Dec 06, 2009

Try using the following code for managing the cursor_mc...

Mouse.hide();
cursor_mc.mouseEnabled = false; // to keep cursor from interfering
this.addChild(cursor_mc); // to place cursor at top level

function moveCursor(evt:MouseEvent = null):void {
cursor_mc.x = mouseX;
cursor_mc.y = mouseY;
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveCursor);

moveCursor(); // to initially locate cursor under mouse

Translate
LEGEND ,
Dec 05, 2009 Dec 05, 2009

Part of your problem lies in trying to assigna startDrag to more than one item at a time.  If you look thru the documentation you'll find that it's strictly one at a time.  So in your case, the yellow is the last assigned so it gets the command.

Try the following functions in place of what you have now and then report on what problems remain...

function startMove(evt:MouseEvent):void {
   evt.currentTarget.startDrag();
}


function stopMove(evt:MouseEvent):void {
   evt.currentTarget.stopDrag();
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 05, 2009 Dec 05, 2009

Thank you so much! The drag and drop problem is solved, finally!

And the best part, the way you explained it, it all makes sense and is locigal to me and this is really a veritable miracle I'm still new to the whole actionscript thing and practically it's still a closed book to me.

But the problem with my custom cursor still exists, though it isn't as important as the drag and drop was. Yet, do you have any idea where this problem lies in? The net still stops and then there's no cursor at all.

I hope you have a solution for this problem too and thanks again for your previous help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 05, 2009 Dec 05, 2009

I'm not sure I understand the net bit entirely, but I recommend getting rid of the MOUSE_DOWN/UP listeners for the cursor_mc (assuming that's the net) so that it is not in the dragging loop at all.  See if that alone takes care of matters.  If not, explain again what's going on with that and what your code related to it is attempting to do.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 06, 2009 Dec 06, 2009

Yes, cursor_mc is the picture of the landing net type of thing to "catch" the butterflies. My intention was to replace the normal cursor (black arrow) with the picture of the net. But when I start to drag the red butterfly for example, the butterfly can be moved but the picture of the net stays at the original position of the red butterfly. After moving and dropping the red butterfly to another position, not even the normal cursor (black arrow) is shown.

The code i wanted to use for the replacement is...


cursor_mc.startDrag(true);
Mouse.hide();

Now I have removed the lines...

cursor_mc.addEventListener(MouseEvent.MOUSE_DOWN, startMove); and (MouseEvent.MOUSE_UP, stopMove);

...my code looks like this:

redbutterfly_mc.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
violetbutterfly_mc.addEventListener(MouseEvent.MOUSE_DOWN, startMove);
yellowbutterfly_mc.addEventListener(MouseEvent.MOUSE_DOWN, startMove);


redbutterfly_mc.addEventListener(MouseEvent.MOUSE_UP, stopMove);
violetbutterfly_mc.addEventListener(MouseEvent.MOUSE_UP, stopMove);
yellowbutterfly_mc.addEventListener(MouseEvent.MOUSE_UP, stopMove);


function startMove(evt:MouseEvent):void {
  evt.currentTarget.startDrag();
}


function stopMove(evt:MouseEvent):void {
   evt.currentTarget.stopDrag();
}

But the problem as described still remains. It isn't impossible to replace the normal cursor by own cursor pictures at all and make that work, is it?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 06, 2009 Dec 06, 2009

Try using the following code for managing the cursor_mc...

Mouse.hide();
cursor_mc.mouseEnabled = false; // to keep cursor from interfering
this.addChild(cursor_mc); // to place cursor at top level

function moveCursor(evt:MouseEvent = null):void {
cursor_mc.x = mouseX;
cursor_mc.y = mouseY;
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveCursor);

moveCursor(); // to initially locate cursor under mouse

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 06, 2009 Dec 06, 2009

You've solved the problem, thank you very much!

Since it seems you know all about actionscript or at least all answers to my questions, I try my luck a further time

If I wanted to collect the caught butterflies in a certain target, for example a flowerpot, where the butterflies can't be removed after dragged in, is this more or less the code for it? I haven't tried it yet but I'm searching the internet for something like that and maybe it is the right thing? And is in this case "myTarget" the name of the picture which is used as the target? Thank you in advance!

function pickUp(event:MouseEvent):void {
    event.target.startDrag(true);
    event.target.parent.addChild(event.target);
    startX = event.target.x;
    startY = event.target.y;
}
function dropIt(event:MouseEvent):void {
    event.target.stopDrag();
    var myTargetName:String = "target" + event.target.name;
    var myTarget:DisplayObject = getChildByName(myTargetName);
    if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
        event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
        event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
        event.target.buttonMode = false;
        event.target.x = myTarget.x;
        event.target.y = myTarget.y;
        counter++;
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 06, 2009 Dec 06, 2009
LATEST

Rather than trying your luck, try your code and see what it does.  It looks like it has what you need to get what you want... but the easiest way to find out is to try it out.

If you have any issues with the dropTarget aspect, then use a trace() command to see what the dropTarget actually is when you drop it on the flower pot... trace(event.target.dropTarget +"    "+);.  Then you can adjust your target info accordingly.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines