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

Drag and drop doesn't work, CS4 actionscript 3.0

New Here ,
May 16, 2012 May 16, 2012

Hello everybody

I am busy with a Drag and Drop-excercise. The following is weird. Maybe someone can help me.

There are no faults at the actionscript 3.0 code.

When i play the movie, the dragged items does'nt drag to the drop items.

By loading the movie, it takes 10 seconds. I think it's too long.

The .fla file is on my Desktop.

Here is the code:

package {

          import flash.display.Sprite;

          import flash.events.MouseEvent;

          public class Main extends Sprite {

                    var xPos:int;

                    var yPos:int;

                    public function Main():void {

                              addListeners(broodr, man, tafell);

                    }

                    private function getPosition(target:Object):void {

                              xPos=target.x;

                              yPos=target.y;

                    }

                    private function dragObject(e:MouseEvent):void {

                              getPosition(e.target);

                              e.target.startDrag(true);

                    }

                    private function stopDragObject(e:MouseEvent):void {

                              if (e.target.hitTestObject(getChildByName(e.target.name+"Target"))) {

                                        e.target.x=getChildByName(e.target.name+"Target").x;

                                        e.target.y=getChildByName(e.target.name+"Target").y;

                              } else {

                                        e.target.x=xPos;

                                        e.target.y=yPos;

                              }

                              e.target.stopDrag();

                    }

                    private function addListeners(... objects):void {

                              for (var i:int = 0; i < objects.length; i++) {

                                        objects.addEventListener(MouseEvent.MOUSE_DOWN, dragObject);

                                        objects.addEventListener(MouseEvent.MOUSE_UP, stopDragObject);

                              }

                    }

          }

}

TOPICS
ActionScript
3.3K
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 ,
May 16, 2012 May 16, 2012

The code looks fine as long as you have instances named broodr, man and tafell on the screen as well as broodrTarget, manTarget and tafellTarget.

Try setting the addListeners type explicitly, e.g.:

private function addListeners(... objects:Array):void { ....

If it's casting it as anything else you could be iterating on a bunch of things you don't intend to.

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 ,
May 16, 2012 May 16, 2012

Thanks for your quick answer. I did what you say, :Array added.

After play the movie, nothing changed.

You can see a picture from the movie with the information about the things. Maybe i did something wrong.

The strange thing is that i don't know what my mistake is and i try to find it for a long time.

http://i45.tinypic.com/2qm3sk6.png

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 ,
May 16, 2012 May 16, 2012

In dragObject(e:MouseEvent), place a trace to see if the clip is even firing the function off.

e.g.

function dragObject(e:MouseEvent):void

{

     trace("Pressed: " + e.currentTarget.name);

     getPosition(e.currentTarget);

     e.currentTarget.startDrag(true);

}

Make sure you see the trace statements in output, otherwise your listeners are not being applied. One reason why is you may be assigning them before the object get a chance to hit the screen. In that case modify your constructor like so:

public function Main()

{

     addEventListener(Event.ENTER_FRAME, _handleEnterFrame);

}

private function _handleEnterFrame(e:Event):void

{

     removeEventListener(Event.ENTER_FRAME, _handleEnterFrame);

    

     // now that a frame has rendered add the listeners

     addListeners(broodr, man, tafell);

}

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 ,
May 16, 2012 May 16, 2012

I add the trace and indeed there were mistakes.

----------------------------------------------------------------------

private function dragObject(e:MouseEvent):void {

                              trace("Pressed: "+e.currentTarget.name);

                              getPosition(e.currentTarget);

                              e.currentTarget.startDrag(true);

                    }

                              e.target.startDrag(true);

                    }

--------------------------------------------------------------------------------------------------------------------------------------------

The attribute can only be used in definitions from class-properties. There is a reference to line 27:



private function stopDragObject(e:MouseEvent):void {

and 39:



private function addListeners(... objects:Array):void {

---------------------------------------------------------------------------------------------------------------------------------------------

The code

public function Main():void {

                              addListeners(broodr, man, tafell);

                    }

must be changed in

public function Main()

{

     addEventListener(Event.ENTER_FRAME, _handleEnterFrame);

}

and below that

private function _handleEnterFrame(e:Event):void

{

     removeEventListener(Event.ENTER_FRAME, _handleEnterFrame);

     // now that a frame has rendered add the listeners

     addListeners(broodr, man, tafell);

}

Or isn't this ok?

Message was edited by: Penlite76

Message was edited by: Penlite76

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 ,
May 16, 2012 May 16, 2012

Do you see below where you close out the function and then have an extra e.target.startDrag(true); } below it? You need to remove those 2 lines. I'll highlight them below:

Penlite76 wrote:

I add the trace and indeed there were mistakes.

----------------------------------------------------------------------

private function dragObject(e:MouseEvent):void {

                              trace("Pressed: "+e.currentTarget.name);

                              getPosition(e.currentTarget);

                              e.currentTarget.startDrag(true);

                    }

                              e.target.startDrag(true); // REMOVE

                    } // REMOVE

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 ,
May 16, 2012 May 16, 2012

When i do this:

private function dragObject(e:MouseEvent):void {

                              trace("Pressed: "+e.currentTarget.name);

                              getPosition(e.currentTarget);

                              e.currentTarget.startDrag(true);

                    }

                              e.target.startDrag(true); // REMOVE

                    } // REMOVE

Then two mistakes:

Line 30:



private function stopDragObject(e:MouseEvent):void {

and line 42:



private function addListeners(... objects:Array):void {

With the same description as earlier:

The attribute can only be used in definitions from class-properties. Earlier it was line 27 and 39.

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 ,
May 16, 2012 May 16, 2012

You removed the lines I bolded and commented with // REMOVE?

Paste all your code again from top to bottom or use www.pastebin.com and paste the link here.

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 ,
May 16, 2012 May 16, 2012
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 ,
May 16, 2012 May 16, 2012

What is the mistake?

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 ,
May 16, 2012 May 16, 2012
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 ,
May 16, 2012 May 16, 2012

Thanks a lot. I copy/paste this in my actionscript and the script was off course correct.

But when the movie play i can't drag anything. The draggable items did nothing.

I have try it with my mousepad and with a separate computer mouse.

Really strange this.

The .fla and the .swf file are on my Desktop.

Is it possible that i have to create a folder somewhere else on the computer and need to place the files there?

The size of the 'green' movieclips are not the same as the others. Can this be the problem or not?

The green movieclips: broodr > 93 width 47 height. man > 67 width 99 height. tafell > 51 width 69,95 height.

The other movieclips: broodr > 78 width 50 height. man > 80 width 120 height. tafell > 50 width 58 height.

Now i have changed the green movieclips. Even width and height as the other movieclips.

Play the movie goes fast, but the drag and drop function don't work. I feel that i come closer and closer to the solution.

Message was edited by: Penlite76

Message was edited by: Penlite76

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 ,
May 17, 2012 May 17, 2012

The question is, are you seeing the traced output when you click the clip? It should say: "Pressed: [someclipname]" every time you click one of the clips. Do you see it saying that? If not then the listeners are still not being applied to the clips.

You DO have the instance names set up properly correct? The names of the clips in the library means nothing. I'll post an old image that doesn't have to do with your project but points out what an instance name is:

Example of an Instance Name

Note in the image the clip is selected, then in the properties panel a red arrow points to the instance name of that clip.

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 ,
May 17, 2012 May 17, 2012

When i clicked at one of the movieclips, nothing happened. No text.

I have looked at the instancename by the properties of each movieclip. For example broodr, without _mc. I improve that to broodr_mc. Also with the other movieclips.

Do i need to change the Actionscript code because i add _mc at the movieclips?

In the librare are 9 items. Broodrooster.png, Mannetje.png, Tafel.png and 6 Symbols.

I import the images Broodrooster.png, Mannetje.png and Tafel.png in the library. Then from the library to the background. Then convert to symbol.

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 ,
May 17, 2012 May 17, 2012

Take the _mc off the instance names. You are using the instance names in the code so yes it will affect it.

The Event.ENTER_FRAME is missing from the code. Here I've returned it and added an extra trace that you should see if ActionScript cannot see your instance names:

http://pastebin.com/Vr4uB4KQ

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 ,
May 17, 2012 May 17, 2012

I did it, but when i play the movie nothing changed. When i clicked a few times on one of the movieclips, nothing changed.

Really strange. Exactly the code you give and off course no mistakes. It was correct.

_mc is delete from the six movieclips.

My .fla is on the Desktop, as i earlier say. But why nothing changed? It's nearly unbelievable.

Maybe it's better that is send you a zipfile with the .fla file. Maybe you can see something what is wrong.

Edit: Now i try it to work on another way. I don't change the code, but the Actionscript-code i put it in a .as-file in Dreamweaver.

When i play the movie the following happened: i can drag the broodr, man and tafell-movieclips.

For example: i drag the broodr-movieclip, when the broodr-movieclip is on the half of the broodrTarget-movieclip, the item will be dropped.

When i try the broodr-movieclip to the man-movieclip or the tafell-movieclip, he doesn't drop the movieclip. So that is good.

But strange is that a movieclip only dropped when it's on the half of the "green" movieclip. When i drag the item until after the half of the movieclip, the item comes behind the "green" movieclip.

When i click, At the output-tab comes the following: Pressed: broodr ( or the others)

THANK YOU VERY MUCH. Your help was absolute amazing. Strange that this not work when the Actionscript-code is in the actions at the .fla file.

I'm sure this can also work when the Actionscript-code is in the .fla file. In the next time, i keep trying that this worked without Dreamweaver.

When you have patience with me, and i believe this, it will be working without Dreamweaver.

I am a beginner, but i'll have so much learned from you.

Message was edited by: Penlite76

Message was edited by: Penlite76

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 ,
May 17, 2012 May 17, 2012

You're most welcome. If you can mark the thread as answered I'd be even happier . Good luck to you.

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 ,
May 18, 2012 May 18, 2012

Thanks a lot sinious. Absolute ingenious you are about Actionscript and others.

I click on correct helpfull. in your last answer. But the thread as answered. Where can i do that? Probably i look over here.

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 ,
May 18, 2012 May 18, 2012

You can mark any post as helpful or the answer you can mark as correct

Good luck

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 ,
May 18, 2012 May 18, 2012
LATEST

Thanks. You also good luck with everything.

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