質問

I have about 50 symbols that I want to make draggable. Nothing fancy, just the ability to click it and drag it to a different location.

I found as3 code for doing so but when I paste it into my file it gives me errors:

**Error** Scene=Scene 1, layer=Units, frame=1:Line 9: The class or interface 'MouseEvent'      could not be loaded.
 function mouseDownHandler(evt:MouseEvent):void {

That code is:

// Register mouse event functions
fighter_uk.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
fighter_uk.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

fighter_uk.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
fighter_uk.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

// Define a mouse down handler (user is dragging)
function mouseDownHandler(evt:MouseEvent):void {
var object = evt.target;
// we should limit dragging to the area inside the canvas
object.startDrag();
}

function mouseUpHandler(evt:MouseEvent):void {
var obj = evt.target;
    obj.stopDrag();
}

I'm using flash pro 8, so I tried finding as2 code but couldn't find it.

Also, is there an 'easy' way to code all 50 objects?

役に立ちましたか?

解決

I think you're trying to compile AS3 code with AS2 compiler. Try changing your compilation settings to target AS3. Also you may need to include the class import at the top of your code:

import flash.events.MouseEvent;

To drag 50 objects, add them all on the same container sprite and add the listener to the container sprite only:

var holder:Sprite = new Sprite();
for ( var i:int = 0, l:int = 50; i < l; i++ ) {
  var dragee:YOUR_CUSTOM_OBJECT = new YOUR_CUSTOM_OBJECT();
  holder.addChild(dragee);
}
addChild(holder);
holder.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
holder.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
holder.addEventListener(Event.MOUSE_LEAVE, mouseUpHandler);
var currentDragee:YOUR_CUSTOM_OBJECT = null;
function mouseDownHandler(evt:MouseEvent):void {
  currentDragee = evt.target as YOUR_CUSTOM_OBJECT;
  if ( currentDragee !== null ) {
    currentDragee.startDrag();
    holder.addChild(currentDragee); // bring current to front position
  }
}
function mouseUpHandler(evt:Event):void {
  if ( currentDragee !== null ) currentDragee.stopDrag();
  currentDragee = null;
}

YOUR_CUSTOM_OBJECT being the object class you need to drag. Hope it helps!

他のヒント

This page seems to have the answers you are looking for (AS2 drag and drop). If you've already seen it, you'll need to explain why it's not good enough for your needs.

If you want to drag/drop multiple instances in AS2, you can still add the code to the movieClip symbol, export it from the library and load the instances up using attachMovie (all 50 of them). If they are all different, then attach the code as necessary to the clips themselves, or to some function elsewhere that will capture all the clicks and decide what was clicked. This is all very doable in AS2.

Remember you can use your onClipEvent(load) function to set up a lot of the initial lifting.

Here's a sample I made in AS2 for making a node tree. It's all draggable (mouse drag) and zoomable (with mouse Wheel). You can add nodes by clicking on the little down arrow in the node box. Each node is listening for the mouse.

You'll want to look at this section for the most part:

// Enable drag on button press
on (press) 
{
    startDrag(this);
}
// Stop the drag on release of mouse button
on (release) 
{

    stopDrag();
}

Besides this, I'm not really sure how your setup looks, so I hope this helps get the ball rolling. (Check the link, there's lots of little gems in there).

Flash Professional 8 only supports ActionScript 2 & 1 You can follow this official URL and learn how to do that in ActionScript 2, but I extremely recommend you to work with ActionScript 3.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top