How to make drag and drop in action script3.0 doesnt overlapping - actionscript-3

So I am making it like a drag and drop quiz, there is two answer box where you can drop your answer in it, so my problem is that when I drop an answer on one answer box, I want the other answer to not be able to drop into the same answer box.
if can, I want when I drop the answer, when I try to drop the other answer into it, it will make the answer back to its place
pics of the quiz
stop();
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
var skor4:int = 0;
skor3.text = String(skor4);
function jumlah2(){
skor4=skor4+1;
}
tick1.visible=false;
tick2.visible=false;
cross1.visible=false;
cross.visible=false;
meso1.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
function fl_ClickToDrag(event:MouseEvent):void
{
meso1.startDrag();
}
meso1.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
meso1.stopDrag();
if(meso1.hitTestObject(ruang))
{
jumlah2();
skor3.text=String(skor4);
meso1.x=269.4;
meso1.y=359.45;
tick1.visible=true;
meso1.mouseEnabled=false;
}
else if(meso1.hitTestObject(ruangg))
{
meso1.x=581.95;
meso1.y=359.45;
cross1.visible=true;
Mesir1.mouseEnabled=false;
Indus1.mouseEnabled=false;
}
else{
meso1.x=648;
meso1.y=455.5;
}
}
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
Indus1.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_2);
function fl_ClickToDrag_2(event:MouseEvent):void
{
Indus1.startDrag();
}
Indus1.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_2);
function fl_ReleaseToDrop_2(event:MouseEvent):void
{
Indus1.stopDrag();
if(Indus1.hitTestObject(ruang))
{
Indus1.x=269.4;
Indus1.y=359.45;
cross.visible=true;
Indus1.mouseEnabled=false;
}
else if(Indus1.hitTestObject(ruangg))
{
Indus1.x=581.95;
Indus1.y=359.45;
cross1.visible=true;
Indus1.mouseEnabled=false;
Mesir1.mouseEnabled=false;
meso1.mouseEnabled=false;
}
else{
Indus1.x=433.95;
Indus1.y=455.5;
}
}
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
Mesir1.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag_4);
function fl_ClickToDrag_4(event:MouseEvent):void
{
Mesir1.startDrag();
}
Mesir1.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop_4);
function fl_ReleaseToDrop_4(event:MouseEvent):void
{
Mesir1.stopDrag();
if(Mesir1.hitTestObject(ruang))
{
Mesir1.x=269.4;
Mesir1.y=359.45;
cross.visible=true;
Mesir1.mouseEnabled=false;
}
else if(Mesir1.hitTestObject(ruangg))
{
jumlah2();
skor3.text=String(skor4);
Mesir1.x=581.95;
Mesir1.y=359.45;
tick2.visible=true;
meso1.mouseEnabled=false;
Indus1.mouseEnabled=false;
}
else{
Mesir1.x=226.85;
Mesir1.y=455.5;
}
}
/* Click to Go to Frame and Play
Clicking on the specified symbol instance moves the playhead to the specified frame in the timeline and continues playback from that frame.
Can be used on the main timeline or on movie clip timelines.
Instructions:
1. Replace the number 5 in the code below with the frame number you would like the playhead to move to when the symbol instance is clicked.
*/
cont.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndPlayFromFrame_31);
function fl_ClickToGoToAndPlayFromFrame_31(event:MouseEvent):void
{
gotoAndPlay(2);
}
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
/* Click to Go to Scene and Play
Clicking on the specified symbol instance plays the movie from the specified scene and frame.
Instructions:
1. Replace "Scene 3" with the name of the scene you would like play.
2. Replace 1 with the frame number you would like the movie to play from in the specified scene.
*/
home3.addEventListener(MouseEvent.CLICK, fl_ClickToGoToScene_76);
function fl_ClickToGoToScene_76(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "Scene 2");
}

Related

Moving A MovieClip to Left/Right Using Touch Events with simultaneous touches

I am Developing a game, AS3 Adobe air targeted for both android and ios, in which you have a movie clip in the center, and two buttons ( left and right ) that should move that movie clip. My goal is to make the shift between the left/right as smooth as possible :
- if the player is touching the left button, the movie clips moves left. if he removes he's finger from the left button, and touches immediately the right button, the movie clip won't move, it's until he re-touches the right button , that the movie clips moves right. I have tried to implement multitouch events, but i seem to have something wrong since this is the behavior i'm getting.
- if the player is touching the left button, the movie clips moves left as expected, if he touches the right button, the movie clip stops as expected, but if he removes his finger from the left button while keeping it on the right button, the movie clip still freezes and don't move, it should move then to the right
This is the code i am using :
leftButtonCreated.addEventListener(TouchEvent.TOUCH_BEGIN,mouseDown);
rightButtonCreated.addEventListener(TouchEvent.TOUCH_BEGIN,mouseDown2);
stage.addChild(leftButtonCreated);
stage.addChild(rightButtonCreated);
function mouseDown(e:TouchEvent):void
{
stage.addEventListener(TouchEvent.TOUCH_END,mouseUp1);
//listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.;
addEventListener(Event.ENTER_FRAME,myButtonClick);//while the mouse is down, run the tick function once every frame as per the project frame rate
}
function mouseUp1(e:TouchEvent):void
{
removeEventListener(Event.ENTER_FRAME,myButtonClick);//stop running the tick function every frame now that the mouse is up
stage.removeEventListener(TouchEvent.TOUCH_END,mouseUp1);
}
function mouseDown2(e:TouchEvent):void
{
stage.addEventListener(TouchEvent.TOUCH_END,mouseUp2);
//listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.;
addEventListener(Event.ENTER_FRAME,stopDragging);//while the mouse is down, run the tick function once every frame as per the project frame rate
}
function mouseUp2(e:TouchEvent):void
{
removeEventListener(Event.ENTER_FRAME,stopDragging);//stop running the tick function every frame now that the mouse is up
stage.removeEventListener(TouchEvent.TOUCH_END,mouseUp2);
}
function stopDragging(ev2:Event):void
{
if (MC.x <= rightButtonCreated.x)
{
MC.x = MC.x + 10;
}
}
function myButtonClick(ev:Event):void
{
if (MC.x > leftButtonCreated.x)
{
MC.x = MC.x - 10;
}
else
{
}
}
The Code was initially set for mouseEvents, so i tried to shift to touch events so i could fix this problem, and the code above is what i got. Thank you for your time.
EDIT:
And, i use the following code :
var leftButtonCreated:leftB= new leftB();
Your current problem is that both mouseUp1 and mouseUp2 are triggered once you release the left button, as they are both attached to stage. But your actual problem is deeper. You should first move the object left and right if corresponding buttons are pressed, and use TouchEvent.touchPointID to track which touch has been released to understand which button was released.
Also a potential caveat: If you touch both left and right button, then swap fingers while retaining both touches, then release the finger that's over the right button - where should your object move, left or right? I say the correct answer is to the right, as the finger released corresponds to the left button.
leftButtonCreated.addEventListener(TouchEvent.TOUCH_BEGIN,touchDown);
rightButtonCreated.addEventListener(TouchEvent.TOUCH_BEGIN,touchDown);
leftButtonCreated.addEventListener(TouchEvent.TOUCH_END,touchUp);
rightButtonCreated.addEventListener(TouchEvent.TOUCH_END,touchUp);
addEventListener(Event.ENTER_FRAME,moveObject);
function touchDown(e:TouchEvent):void {
var button:DisplayObject=e.currentTarget as DisplayObject;
if (!button) return; // can't fail typecast to DisplayObject in this context, but leave for good measure
if (button==leftButtonCreated) {
leftButtonPressed=true;
leftButtonTouchID=e.touchPointID;
return;
}
if (button==rightButtonCreated) {
rightButtonPressed=true;
rightButtonTouchID=e.touchPointID;
return;
}
}
function touchUp(e:TouchEvent):void {
var button:DisplayObject=e.currentTarget as DisplayObject;
if (!button) return;
var ti:int;
if (button==leftButtonCreated) {
ti=leftButtonTouchID;
if (ti==e.touchPointID) {
leftButtonPressed=false;
}
}
if (button==rightButtonCreated) {
ti=rightButtonTouchID;
if (ti==e.touchPointID) {
rightButtonPressed=false;
}
}
}
function moveObject(e:Event):void {
if (leftButtonPressed) MC.x-=10;
if (rightButtonPressed) MC.x+=10;
if (MC.x<leftButtonCreated.x) MC.x=leftButtonCreated.x;
if (MC.x>rightButtonCreated.x) MC.x=rightButtonCreated.x;
}
EDIT: Apparently SimpleButtons don't allow the events to propagate outside to their parents. Okay, this can still be remedied, but you will have to store the required properties in your Main class.
var leftButtonPressed:Boolean=false;
var rightButtonPressed:Boolean=false;
var leftButtonTouchID:int=0;
var rightButtonTouchID:int=0;
The above code has been updated. Please return to using SimpleButtons directly, as you were using with var leftButtonCreated:leftB= new leftB();.

swipe gesture is not smooth for frame jumping

I have made an as3 android app where I have used swipe gesture for going next frame and previous frame. but I want when I jump to next or previous frame it swipes smoothly.I have tried to use tween max but it is not working.I have found that tween plugins always work for movie clips.So how can I make the swipe smooth.Can I make it smooth without tween plugins?here is my code....
Multitouch.inputMode = MultitouchInputMode.GESTURE;
stage.addEventListener (TransformGestureEvent.GESTURE_SWIPE, SwipeHandler);
function SwipeHandler(event:TransformGestureEvent):void
{
switch(event.offsetX)
{
// swiped right
case 1:
{
prevFrame();
break;
}
// swiped left
case -1:
{
if(currentFrame == 10)
{
stop();
}
else
{
nextFrame();
break;
}
}
}
}
In order for you to be able to smooth swap you need to have both frames on your screen, which is not the case with genuine Flash frames. Smooth transitions are animated via ENTER_FRAME handlers, with a potential hide of actual frame change process. So, in order to do a smooth transition, you need to move a picture of one frame to the left, and for the other frame to move in from the right instead of plain nextFrame(). Let's say you have a 60fpa stage, and try to smooth transition left. You will need two screen-sized objects, one depicting current frame and one the frame to be displayed, ready to be displayed as a single transition. An example:
var transition:Sprite;
var bitmaps:Array;
var leftSide:Bitmap
var rightSide:Bitmap;
// initialization code, best placed in constructor
leftSide=new Bitmap();
rightSide=new Bitmap();
bitmaps=[];
transition=new Sprite();
transition.addChild(leftSide);
transition.addChild(rightSide);
rightSide.x=stage.stageWidth; //left side and right side should be aside each other
This is the declaration of the needed structures. The plan is to show the transition, giving it two Bitmaps that will be linked to two different BitmapData objects that'll hold pictures of new frame and old frame. We will draw our current frame on current frame's bitmapdata, then take a stored next frame bitmap data and the do transition.
function swipeHandler((event:TransformGestureEvent):void {
var doSwitch:Boolean=false;
var targetFrame:int=currentFrame;
switch(event.offsetX) {
// swiped right
case 1: {
if (currentFrame>2) {
// let's say we're not allowed to swipe right from frame 2
targetframe=currentFrame-1;
doSwitch=true;
}
break;
}
// swiped left
case -1: {
if(currentFrame < 10) {
targetFrame=currentFrame+1;
doSwitch=true;
}
break;
}
}
if (!doSwitch) return;
// prepare transition
if (!bitmaps[targetFrame]) bitmaps[targetFrame]=new BitmapData(stage.stageWidth,stage.stageHeight,false,0xffffff);
// ^ make a new bitmap if there's none for target frame
if (!bitmaps[currentFrame]) bitmaps[currentFrame]=new BitmapData(stage.stageWidth,stage.stageHeight,false,0xffffff);
// the same for source frame
bitmaps[currentFrame].fillRect(bitmaps[currentFrame].rect,0xffffff);
bitmaps[currentFrame].draw(stage); // draw current frame on the bitmap
// with this and stored bitmaps, old frame would remain drawn on the cached bitmap
// and able to be used as a transition image
if (targetFrame>currentFrame) {
leftSide.bitmapData=bitmaps[currentFrame];
rightSide.bitmapData=bitmaps[targetFrame];
transition.x=0;
// here is the place to initialize TweenMax tween to move "transition"
// and don't forget to removeChild(transition) at the end of the tween
} else {
rightSide.bitmapData=bitmaps[currentFrame];
leftSide.bitmapData=bitmaps[targetFrame];
transition.x=-1*stage.stageWidth;
// same here for tweening
}
stage.addChild(transition);
gotoAndStop(targetFrame);
}
This places a prepared transition object on screen above all the underlying items, effectively masking the exact frame switch, done by gotoAndStop() call. This object's x coordinate can be tweened afterwards, and the object should be removed from stage once the tween is ofer.
Hope this helps.

Stop Drag, Add Up Release for a separate function

I have a movie clip where I've placed start drag and stop drag actions, and I'd like to activate a button action (run a movie clip named person_mo) with the ReleaseToDrop function. However, I cannot code it correctly. Here is the AS I have for the drag:
stop();
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
SCOPE2.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
function fl_ClickToDrag(event:MouseEvent):void
{
SCOPE2.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
SCOPE2.stopDrag();
}
How can I play the person_mo movie clip and stop the drag?
After I tested your code, your stopDrag function is working well. You just need to fire your event/function/ command when you fire the ReleaseToDrop function.
stop();
/* Drag and Drop
Makes the specified symbol instance moveable with drag and drop.
*/
SCOPE2.addEventListener(MouseEvent.MOUSE_DOWN, fl_ClickToDrag);
function fl_ClickToDrag(event:MouseEvent):void
{
SCOPE2.startDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP, fl_ReleaseToDrop);
function fl_ReleaseToDrop(event:MouseEvent):void
{
SCOPE2.stopDrag();
person_mo.play(); // if you want to run the movie clip
yourFunction(); //or else if you want a function to fire, simply add yourFunction which fires the button action.
}
I hope this helps.

Problems disabling movieclip button flash/as3

I've got a screen which involves a movie-clip where the object has a outline to symbolize that it can be clicked. Upon clicking the object, I'm requesting it to do numerous functions, disable itself and then go to another frame which removes the outline symbolizing it cannot be clicked anymore. But once you disable an object it goes to the original frame.
The object itself consists of these 3 frames.
Frame 1: Original State (Glow)
Frame 2: Hover over giving stats
Frame 3: No glow
To summerise i'd like to click the object and for it to go to the no glow frame and disable the movieclip.
The movieclip enabled = 1 is for when the user returns to the this frame, so the scene is aware of the button press.
Movieclip.addEventListener(MouseEvent.CLICK, Fun_Movieclip);
Movieclip.addEventListener(MouseEvent.MOUSE_OVER, Fun_MovieclipMouseOver);
Movieclip.addEventListener(MouseEvent.MOUSE_OUT, Fun_MovieclipMouseOut);
function Movieclip(event:MouseEvent):void
{
MovieclipEnabled = 1;
Movieclip.gotoAndStop(1);
Movieclip.mouseEnabled = false;
}
function Fun_MovieclipMouseOver(event:MouseEvent):void
{
Movieclip.gotoAndStop(2);
}
function Fun_MovieclipMouseOut(event:MouseEvent):void
{
Movieclip.gotoAndStop(3);
}
For some reason when ever the movieclip is disabled, it always reverts back to the glow state. Does anyone have a solution for this? Cheers
Edit: Inside the movieclip, the first frame has Stop();. Don't know if this would interfere with it.
mc.addEventListener(MouseEvent.CLICK, clickHandler);
mc.addEventListener(MouseEvent.MOUSE_OVER, mouseoverHandler);
mc.addEventListener(MouseEvent.MOUSE_OUT, mouseoutHandler);
function clickHandler(event:MouseEvent):void
{
mc.gotoAndStop(3);
mc.removeEventListener(MouseEvent.CLICK, clickHandler);
mc.removeEventListener(MouseEvent.MOUSE_OVER, mouseoverHandler);
mc.removeEventListener(MouseEvent.MOUSE_OUT, mouseoutHandler);
}
function mouseoverHandler(event:MouseEvent):void
{
mc.gotoAndStop(2);
}
function mouseoutHandler(event:MouseEvent):void
{
mc.gotoAndStop(1);
}
Not entirely sure what you meant by:
The movieclip enabled = 1 is for when the user returns to the this frame, so the scene is aware of the button press.
My suggestion for getting the scene to recognize the button click is to have the scene also listen to the mouse click handler

Proceeding in Movieclip After Animation Finishes AS3

I have a Movieclip on my stage which contains two buttons: Back and Next. Also on the timeline, I have another Movieclip which contains an animation. After the next button is clicked, I'd like to have the animation transition into the next animation without jumping. So, is there any way I can listen for the animation to be finished so that I can time the transitions seamlessly?
Here is the code for my Next and Back buttons (which are movieclip buttons which is why there is all of the extra code) which just switch between frames as of now:
//NEXT BUTTON
nextBtn.buttonMode = true;
nextBtn.addEventListener(MouseEvent.ROLL_OVER, nextBtnOver);
nextBtn.addEventListener(MouseEvent.ROLL_OUT, nextBtnOut);
nextBtn.addEventListener(MouseEvent.MOUSE_DOWN, nextBtnDown);
nextBtn.addEventListener(MouseEvent.MOUSE_UP, nextBtnUp);
function nextBtnOver(e:MouseEvent):void
{
nextBtn.gotoAndPlay("over");
}
function nextBtnOut(e:MouseEvent):void
{
nextBtn.gotoAndPlay(9- (nextBtn.currentFrame-1));
}
function nextBtnDown (e:MouseEvent):void
{
nextBtn.gotoAndPlay("down");
}
function nextBtnUp (e:MouseEvent):void
{
nextBtn.gotoAndPlay(5);
MovieClip(root).nextFrame();
}
//BACK BUTTON
backBtn.buttonMode = true;
backBtn.addEventListener(MouseEvent.ROLL_OVER, backBtnOver);
backBtn.addEventListener(MouseEvent.ROLL_OUT, backBtnOut);
backBtn.addEventListener(MouseEvent.MOUSE_DOWN, backBtnDown);
backBtn.addEventListener(MouseEvent.MOUSE_UP, backBtnUp);
function backBtnOver(e:MouseEvent):void
{
backBtn.gotoAndPlay("over");
}
function backBtnOut(e:MouseEvent):void
{
backBtn.gotoAndPlay(9- (backBtn.currentFrame-1));
}
function backBtnDown (e:MouseEvent):void
{
backBtn.gotoAndPlay("down");
}
function backBtnUp (e:MouseEvent):void
{
backBtn.gotoAndPlay(5);
MovieClip(root).prevFrame();
}
Thanks, any help is appreciated.
If your animations are timelined, call an animationComplete function in the last frame of your animation movieClip. This function would live at the same scope as your posted code above, so you just have to be able to path to it properly from within the animation clip. For instance, if the code is one level outside the animation movieClip (the mc's parent) then you might call this.parent.animationComplete(), which would contain the code you want to execute when the animation finishes.
According to your comment,
I tried this, and it works - except in order for
it to advance to the next point you have
to click the next button at exactly the right frame.
I'd like to make it so you can click it at any point in the Movieclip, and it
will play the remainder of the Movieclip before proceeding
to the next frame. Here's my code for what I've added: function
nextBtnUp(e:MouseEvent):void {
nextBtn.gotoAndPlay(5); if (MovieClip(root).animationTest.currentFrame ==
MovieClip(root).animationTest.totalFrames) MovieClip(root).nextFrame();
}
Thank you for the help so far
You have it most of it figured out, so to finish can add stop() in the code at the last frame of your MovieClips, or you can do this (the more complex version):
yourClip.yourFirstClip.addEventListener(Event.ENTER_FRAME, stopAtLastFrame);
yourClip.yourSecondClip.addEventListener(Event.ENTER_FRAME, stopAtLastFrame);
function stopAtLastFrame(e:Event):void
{
if(e.currentTarget.currentFrame === e.currentTarget.totalFrames)
// ^Note that no conversion is being made, so you can have 3 equal signs
{
e.currentTarget.stop();
}
}