Dragging more than one item in Adobe Flash CS3 Actionscript
I've been trying to program a game in my class on Digital Media, and have run up to a huge roadblock concerning draggable items. I first managed to make one item that could be dragged with the mouse using this code (the movie clip is called 'honey'):
//set listeners
honey.addEventListener(MouseEvent.MOUSE_DOWN, startHoneyDrag);
stage.addEventListener(MouseEvent.MOUSE_UP, stopHoneyDrag);
honey.addEventListener(Event.ENTER_FRAME, dragHoney);
//offset between sprite location and click
var clickOffset:Point = null;
//user clicked
function startHoneyDrag(event:MouseEvent) {
clickOffset = new Point(event.localX, event.localY-7);
}
//user released
function stopHoneyDrag(event:MouseEvent) {
clickOffset = null
}
//run every frame
function dragHoney(event:Event) {
if (clickOffset != null) { // must be dragging
honey.x = mouseX - clickOffset.x;
honey.y = mouseY - clickOffset.y;
}
}
However,making more than one moveable movie clip has proven impossible so far. Using the same code multiple times creates an error due to conflicting parts of the code. When I asked one of the better programming students, he told me to create an array, which I did;
var honeyBall:Array = new Array();
honeyBall = ["honey, honey1, honey2"];
But now I don't know how to make a code that would refer to a movie clip refer to an 开发者_运维知识库array of them. Help would be seriously appreciated, as these problems have been giving me serious trouble.
honeyBall = ["honey, honey1, honey2"];
This is array containing one string. You should get your instances of clips into array, something like this:
honeyBall = [honey1, honey2, honey3];
Then you drag it with
function dragHoney(event:Event) {
if (clickOffset != null) {
for each (var honey:DisplayObject in honeyBall) {
honey.x = mouseX - clickOffset.x;
honey.y = mouseY - clickOffset.y;
}
}
}
(will drag everything in array, be it one or several clips.)
精彩评论