Skip to main content
Known Participant
October 4, 2012
Answered

Click and Drag issue (beginner)

  • October 4, 2012
  • 1 reply
  • 1792 views

Hello, so I have a really simple program where I'd like the user to be able to drag the statue object, and place it on top of a pedestal. I started with the click and drag, but the code didn't work when I used it. I tried using the direct code snippets code, and that didn't work either. After putting a trace in the click function, I found that it wasn't being called. They are named the same, and the mouse_up function was working properly, so I'm not sure what the problem is. Any help would be greatly appreciated~

Here's the code, the important bits are bolded. I am recieving no other errors~

package

{

          import flash.display.*;

          import flash.events.*;

          import flash.ui.*;

          public class retry extends flash.display.MovieClip

          {

                    var rightIsPressed:Boolean = false;

                    public function retry()

                    {

                              //add drag listeners

                              trace("it's working");

                              //make the instructions invisible

                              controlText.alpha = 0;

                              //call event listeners

                              nextBtn.addEventListener(MouseEvent.CLICK, seeManga);

                              startBtn.addEventListener(MouseEvent.CLICK, beginManga);

                              controlText.addEventListener(MouseEvent.MOUSE_OVER, showText);

                              controlText.addEventListener(MouseEvent.MOUSE_OUT, hideText);

                              walkinganimation.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_3);

                              stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_3);

                              stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_3);

                              statue.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);

                              statue.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);

                    }

 

function fl_ClickToDrag(event:MouseEvent)

                    {

                              var object = (event.currentTarget);

                              object.startDrag();

                    }

                    function fl_ReleaseToDrop(event:MouseEvent)

                    {

                              var object = (event.currentTarget;)

                              object.stopDrag();

 

                    }

 

                    //begin functions

                    function showText(event:MouseEvent)

                    {

                              trace("mouse in");

                              controlText.alpha = 1;

                    }

                    function hideText(event:MouseEvent)

                    {

                              trace("mouse out");

                              controlText.alpha = 0;

                    }

                    function beginManga(event:MouseEvent)

                    {

                              gotoAndStop(2);

                    }

                    function seeManga(event:MouseEvent)

                    {

                              trace("2nd button");

                              nextFrame();

                    }

                    function fl_MoveInDirectionOfKey_3(event:Event)

                    {

 

                              if (rightIsPressed)

                              {

                                        trace("keyboard pressed");

                                        walkinganimation.gotoAndStop(2);

                                        foreground.x -= 3.5;

                                        text1.x -= 2;

                                        text2.x -= 2;

                                        text3.x -= 2;

                                        text4.x -= 2;

                                        pedastal.x -= 3.0;

                                        statue.x-=3.0;

 

                              }

                    }

                    function fl_SetKeyPressed_3(event:KeyboardEvent):void

                    {

                              switch (event.keyCode)

                              {

                                        case Keyboard.RIGHT :

                                                  {

                                                            rightIsPressed = true;

                                                            break;

                                                  }

                              }

                    }

                    function fl_UnsetKeyPressed_3(event:KeyboardEvent):void

                    {

                              switch (event.keyCode)

                              {

                                        case Keyboard.RIGHT :

                                                  {

                                                            rightIsPressed = false;

                                                            walkinganimation.gotoAndStop(1);

                                                            break;

                                                  }

                              }

                    }

          }

}

This topic has been closed for replies.
Correct answer Ned Murphy

Yes, the other traces show up. I added the listener to the stage instead for the mouse_up, but that didn't work either. The traces in the mouse_up function work fine though, it's the mouse_down that doesn't seem to be registering. Is there something else I need to do in order for that function to be called, or is there any other reason it wouldn't work?


You said originally that you are getting no other errors, but are you getting any error messages at all?

How are you implementing this class file into your Flash file? 

Do you happen to have anything that might be blocking access to the statue object?

Do you happen to animate the statue object into place such that it is not named in frames before the frame where your code tries to work with it?

1 reply

Participating Frequently
October 4, 2012

Hi, I think this code will help; it's way less complicated. This code is acutally in the code snipets window.

_________________Do not include this line.

/* Drag and Drop

Makes the specified symbol instance moveable with drag and drop.

*/

statue.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);

function fl_ClickToDrag(event:MouseEvent):void

{

statue.startDrag();

}

stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);

function fl_ReleaseToDrop(event:MouseEvent):void

{

          statue.stopDrag();

}

_________________Do not include this line.

Simply change " statue" to the symbol you would like to move. Your code is missing the stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);

Just in case this doesn't work or you can't figure out what to edit, just compare with this: http://www.mediafire.com/?rk133h80za3354l

Known Participant
October 4, 2012

Thank you, but that didn't work either. I'm at a loss here, from what I can see the code looks correct, and I have no errors to go off of...

Here's what I changed:

package

{

          import flash.display.*;

          import flash.events.*;

          import flash.ui.*;

          public class retry extends flash.display.MovieClip

          {

                    var rightIsPressed:Boolean = false;

                    public function retry()

                    {

                              //add drag listeners

                              trace("it's working");

                              //make the instructions invisible

                              controlText.alpha = 0;

                              //call event listeners

                              nextBtn.addEventListener(MouseEvent.CLICK, seeManga);

                              startBtn.addEventListener(MouseEvent.CLICK, beginManga);

                              controlText.addEventListener(MouseEvent.MOUSE_OVER, showText);

                              controlText.addEventListener(MouseEvent.MOUSE_OUT, hideText);

                              walkinganimation.addEventListener(Event.ENTER_FRAME, fl_MoveInDirectionOfKey_3);

                              stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed_3);

                              stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed_3);

                              statue.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);

                              statue.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);

                    }

 

                    function fl_ClickToDrag(event:MouseEvent):void

                    {

                              statue.startDrag();

                    }

                    function fl_ReleaseToDrop(event:MouseEvent):void

                    {

                              statue.stopDrag();

 

                    }

 

                    //begin functions

                    function showText(event:MouseEvent)

                    {

                              trace("mouse in");

                              controlText.alpha = 1;

                    }

                    function hideText(event:MouseEvent)

                    {

                              trace("mouse out");

                              controlText.alpha = 0;

                    }

                    function beginManga(event:MouseEvent)

                    {

                              gotoAndStop(2);

                    }

                    function seeManga(event:MouseEvent)

                    {

                              trace("2nd button");

                              nextFrame();

                    }

                    function fl_MoveInDirectionOfKey_3(event:Event)

                    {

 

                              if (rightIsPressed)

                              {

                                        trace("keyboard pressed");

                                        walkinganimation.gotoAndStop(2);

                                        foreground.x -= 3.5;

                                        text1.x -= 2;

                                        text2.x -= 2;

                                        text3.x -= 2;

                                        text4.x -= 2;

                                        pedastal.x -= 3.0;

                                        statue.x-=3.0;

 

                              }

                    }

                    function fl_SetKeyPressed_3(event:KeyboardEvent):void

                    {

                              switch (event.keyCode)

                              {

                                        case Keyboard.RIGHT :

                                                  {

                                                            rightIsPressed = true;

                                                            break;

                                                  }

                              }

                    }

                    function fl_UnsetKeyPressed_3(event:KeyboardEvent):void

                    {

                              switch (event.keyCode)

                              {

                                        case Keyboard.RIGHT :

                                                  {

                                                            rightIsPressed = false;

                                                            walkinganimation.gotoAndStop(1);

                                                            break;

                                                  }

                              }

                    }

          }

}

Participating Frequently
October 4, 2012

Completely switch out your code for the one I posted, I see what you did wrong, it needs to have the command for the stage to respond.