Some minor changes can take care of it. This is related to the issue I mentioned about the mouseDown listener and currentTarget. If you make each of your draggable things have the mouseDown listener instead of the stage, you can use currentTarget instead of target, and be assured that the currentTarget should be draggable.
After making that change, you'd have to keep two arrays: one which contains the things that are draggable, and one which contains everything which must be tested against. Note that the second should include all of the first.
I don't have time to make these changes right now, but I may be able to get you something over the weekend. In the meantime, I encourage you to experiment and try to get it working yourself, you may just surprise yourself.
i appreciate your help greatly im sorry if i sound like im nagging.
I believe i did everything you said but now it doesn't seem to be dragging any item. I added the mouseUp event listener inside the mouseDown event listener and also add the new array and push the object names i didn't want draggable inside that array. Maybe im doing something wrong.
I don't get any compile errors or anything it just doesn't drag any item when mouse down and doesn't obey notDraggable array.
I've updated your last code with what I intended. I have not been able to test it because of all the references to undefined things.
The major changes are:
1. changed nonDraggableItems to collidableItems, and made it a class property
2. moved init to respond to ADDED_TO_STAGE so there's a chance to put children on the Main before initializing it.
3. populated collidableItems to include all draggableItems as well as the others.
4. added the mouseDown listener to each of the draggables.
5. changed the mouseMove function to check against collidableItems instead of draggableItems
the code works pretty much now however the dragging doesn't seem to be the same as it was? if go and drag the object "verticalBtns" or "previewObj" and it wont let me drag it past a certain posiition cause essentially i have a hidden element above it around x=427.45, y=174.95
The reason i think this is cause i have an object called horizontalBtns hidden when loaded and if selected from the drop down menu then verticalBtns hides and horizontalBtns is visible. When the object is hidden i basically don't want it to detect the collision?
Do i just add that in an if statement or something? Any help would be appreciated. Where should i add it?
The way the code is written, you cannot drag anything over something in the collideableItems array, regardless of whether that thing is visible or not. If you want something no longer to be collideable, the neatest way to do it would be to take it out of the collideableItems array. However, if you just want to prevent things with alpha==0 from being collideable, you could put that test in the mouseMove function. Just test whether d is invisible just like you test whether d is the currentDragged.
As I said, a more flexible way to do it would be to manipulate the collidableItems array as you hide and show new collidable items.
What im trying to do is take out the item in the array like you stated when i select an item and add a new one in its place. So basically verticalBtns, and horizontalBtns wont be in the array to begin with and should be added and atken out based on the item you self in my if/else statement.
If you've left the code mostly as is, then it looks like you are defining that btnGroupHandler function inside the init function. That won't work, move it outside, like any other function.
The idea of your code seems sound, but instead of having a hardcoded index for the verticalBtns or horizontalBtns, you can use indexOf to find them.
Code:
public function btnGroupHandler(event:Event):void {
var toSwapOut:DisplayObject;
var toSwapIn:DisplayObject;
if (btnGroupCB.selectedLabel== "Vertical") {
trace("selected Vertical");
toSwapIn = verticalBtns;
toSwapOut = horizontalBtns;
} else if (btnGroupCB.selectedLabel == "Horizontal") {
trace("selected Horizontal");
toSwapIn = horizontalBtns;
toSwapOut = verticalBtns;
}
toSwapOut.visible = false;
toSwapIn.visible = true;
var where:int = collidableItems.indexOf(toSwapOut);
if (where != -1){ //if it IS in there, remove it
collidableItems.splice(where, 1);
}
where = collidableItems.indexOf(toSwapIn);
if (where == -1){ //if it is NOT in there, add it.
collidableItems.push(toSwapIn);
}
trace("collidableItems now: "+collidableItems);
}
Also, not to criticize too much, but whenever you are asking for help and you say something like "it doesn't work", you need to provide as much relevant information about why it doesn't work as you can. Any error messages whether at compile or runtime, or even just saying that there are no error messages.
my function wasnt inside the init function. I did however do the method indexOf for the startIndex property in splice and that seems to work i think but it doesn't seem to be removing the item.
When i trace out the array after i have spliced it i get the following:
,,,[object MovieClip],[object MovieClip],[object vertical_8],[object MovieClip],[object horizontal_48]
why does it say vertical_8 under it? and also horizontal_48? Those are not the names i gave it or the types i assigned it in the array? Should it say object MovieClip for all of them? Also why the three commas before?
This is the code i am using:
Code:
public function btnGroupHandler(event:Event):void {
if (btnGroupCB.selectedLabel== "Vertical") {
//trace("selected Vertical");
verticalBtns.visible = true;
horizontalBtns.visible = false;
} else if (btnGroupCB.selectedLabel == "Horizontal") {
//trace("selected Horizontal");
verticalBtns.visible = false;
horizontalBtns.visible = true;
collidableItems.splice(collidableItems.indexOf("verticalBtns"), 1, getChildByName("horizontalBtns"));
trace(collidableItems)
}
}
Any help to get this to remove the item from the array so i can drag it overtop the hidden item would be great.
The three commas indicate that there are 3 empty entries in the array before the first thing you actually see. This could be because they were null when added to the array, or because the array's content has been set to null since then.
It probably says [object vertical_8] because it's a symbol in your library. All such symbols get a class, whether you specify it or not. "vertical_8" is the classname that flash generated for that symbol.
I notice you're not splicing in the Vertical case. That could be why you see both in the array. I suggest tracing collidableItems before the splice too, so you can see exactly how the splice affects it.
OH! I just noticed that in your splice statement, you have index of the String "verticalBtns". The string isn't in collidableItems; the actual verticalBtns movie is. Remove the quotes. And while you're at it, get rid of the "getChildByName" call because you already have a reference to it.
I notice you have a call to collidableItems.splice in the init function. Edit: now that it's formatted, I see a different issue. The splice call you have there will result in verticalBtns being replaced by horizontalBtns. That's all fine and dandy, except that now you'll have horizontalBtns in there twice, which will mess you up later. That's partially why I used a separate remove and add in my code which manipulated the collidableItems array, so that it wouldn't depend on the state of that array beforehand.
I don't see why previewObj would behave differently from any of the other draggables, unless of course the preview isn't added yet when the Main is. Might want to check for typos wherever you've defined/added previewObj.
Last edited by 5TonsOfFlax; 12-03-2007 at 07:37 PM.