Copy link to clipboard
Copied
Hello, I'm making simple drag and drop game for several letters. Some of the letters appear in the wrong frame and generate an error when clicked.
I'm attaching an example of the file i'm working on, if someone can help would be great (Test2.fla - Google Drive ). A, B, C are fine but XYZ carry on to the next frame. what can be done to stop this.
stop();
var orig1X:Number=A1.x;
var orig1Y:Number=A1.y;
A1.addEventListener(MouseEvent.MOUSE_DOWN, dragTheObject);
A1.addEventListener (MouseEvent.MOUSE_UP, item1Release);
function dragTheObject(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.startDrag();
var topPos:uint=this.numChildren-1;
this.setChildIndex(item, topPos);
}
function item1Release(event:MouseEvent):void {
var item:MovieClip=MovieClip(event.target);
item.stopDrag();
if (pos1.hitTestPoint(item.x,item.y)) {
item.x=pos1.x;
item.y=pos1.y;
} else {
item.x=orig1X;
item.y=orig1Y;
}
};
A1.buttonMode = true;
So this is the code im using for the drag and drop game
Copy link to clipboard
Copied
Look at your timeline. In the first three frames your letters layer is under the box layer. In the next three frames those box frames are empty. This allows the letters to show up. The reason that the letters are showing in frames where you haven't put them is because you have positioned those letters with actionscript. Once an object is controlled by actionscript it will persist on the stage until it is removed by actionscript.
Copy link to clipboard
Copied
hello rob, thanks for the reply, and how do you remove them with action script?
i've tried to use the following command
if (contains(A1))
{
removeChild(A1);
}
works fine but once I hit the back button to go back, then the code is broken
anyway to solve this problem?
Copy link to clipboard
Copied
That's where the messy part begins. The simplest, at least to me, solution is to place and remove each of the letters using actionscript. Doing that means that you will only need one frame.
Depending on what your goal is for this project, you could make the movieclip that is dragged one object and then add the letter movieClip to that dragged clip. Then change the letter on every successful drag.
Copy link to clipboard
Copied
phew, after playing with it for many hours, I think I have something that works.
here is what the code looks like placed on the frame of the objects
addEventListener(Event.ENTER_FRAME, function(e:Event)
{
if((currentFrame!= 5)&&(contains(A1)))
{
removeChild(A1)
A1.x = 726.0;\\\ the object will still appear on your stage unless moved to somewhere else
}
else
{
addChild(A1)
}
})
Copy link to clipboard
Copied
You are adding an enter_frame event to the frame. This code will execute over and over again at the frame rate. You don't need or want that to happen. If you want to remove the object from the stage, just do it once. If the object stays on the stage after the removeChild() call then you are targeting the wrong object or something else is wrong. If moving the object off the visible area of the stage solves the problem then just use that.
Copy link to clipboard
Copied
that is true, but then if I use my back button, it is all over, code is broken. so I cannot think of a better way than this.
Copy link to clipboard
Copied
From your original file:
1. Storing the original location of the dragged object is fine as is the dragging and dropping code.
2. The problem is with the way that you are trying to change the letter. To break it down, you have two things that can happen when the letter is dropped. If the letter is over the target, then the letter is snapped to the center of the target. If the letter is not over the target it is snapped to its original position.
3. You have a next and previous button to change the letter that is dragged. In your example file, changing the letter is independent of the dragging and dropping action. I'm guessing that you really only want the letter to be changed if a drop is successful for a particular letter.
As I said earlier, once you control an object in Animate using Actionscript then that object continues to be controlled by Actionscript and pays no attention to the timeline. In your first three frames the forward works because one letter is concealed by the target in the next frame. The letter is still there, it just doesn't show. Your code that moves the letter off the visible area of the stage sorta, kinda, solves that problem, sorta.
A simpler method for changing the letter is to place a dynamic textField on the moveable object and use an array of letters. You can then iterate through the array's contents using the next or previous buttons. You can use a boolean variable to control whether or not the next of previous buttons change the letter based on the success or failure of the latest letter drag. If the drag is successful, the user can go to the next or previous button.
It might look something like this: http://www.ddg-designs.com/downloads/letters.zip
Find more inspiration, events, and resources on the new Adobe Community
Explore Now