Skip to main content
Known Participant
January 21, 2010
Question

AS3 drag and drop - weird behavior

  • January 21, 2010
  • 1 reply
  • 2352 views

I'm using AS3. I have a timeline with 12 labeled frames (page 1...page12). On frames 1 to 10, I have 3 objects that users can drag and drop onto matching places. There is a timer (60 seconds) and a counter. Users should match 15 objects correctly. If so, they go to frame 12 (congratulations!). If not, they go to frame 11 (Sorry, try again), which returns them to frame 1 (page 1).

ISSUE

When users drag an object (mouseDown) and don't release it when time is over, the dragged object remains on the screen. When users go to frame 11 and game starts over, the object remains throughout the entire game.

How can I get rid of the dragged object that isn't dropped when the time is over?

Here ism y code:

// TIMER

var time1:int=60;
var myTimer1:Timer = new Timer(1000, time1);

myTimer1.addEventListener(TimerEvent.TIMER, timeRunning1);
myTimer1.addEventListener(TimerEvent.TIMER_COMPLETE, timeIsUp1);


function timeRunning1(event:TimerEvent): void
{
    timer_txt.text= String(time1 - myTimer1.currentCount);
}

function timeIsUp1(event:TimerEvent): void
{
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp1); // remove listener when time is over
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt1); // remove listener when time is over
gotoAndStop("tryAgain");
  
}

// FUNCTION DRAG


function pickUp1(event:MouseEvent):void {
event.target.startDrag(true);
event.target.parent.addChild(event.target);
startX1 = event.target.x;
startY1 = event.target.y;

// I tried this to stop the drag action when time is over, but didn't work

if (time1 ==1 ) {this.event.target.visible=false; this.event.target.buttonMode=false; this.event.target.x=event.target.x; this.event.target.y=event.target.y}
}

// FUNCTION DROP

function dropIt1(event:MouseEvent):void {
event.target.stopDrag();
var myTargetName:String = "btn_" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
  txt.text ="Good Job!";
  count++;
  score_txt.text= String(count);
  event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp1);
  event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt1);
  event.target.buttonMode = false;
  event.target.x = myTarget.x;
  event.target.y = myTarget.y;
  checker0++;
  myTimer1.start();
}
else {
  txt.text = "Try Again!";
  event.target.x = startX1;
  event.target.y = startY1;
}
if(checker0 == 3){
        txt.text = "Congratulations. Continue playing!";
    nav_0.visible=true;
   
   
    }

}

// ACTIVATING BUTTON

great_1.buttonMode=true;
less_1.buttonMode=true;
equal_1.buttonMode=true;

Sincerely,

German

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
January 21, 2010

Where you placed the code you indicate doesn't work, that only processes once as soon as that function executes.  I'm not sure what the goal is if the timer times out, but you could just have a timer that times out 60 seconds and calls a function much like the dropIt function you have already.  I'm not sure what that timer you're using now is for, but you can have another dedicated as a 60 second timer.

german01Author
Known Participant
January 21, 2010

Ned:

Thanks for replying.

This is a game. On each frame (page) there are three objects users need to place on matching holders. They have 60 seconds to match 15 objects correctly. As they match the first three correct on page 1, they move to page 2 where they have another three objects, and so for. For each correct match, they get a point. If they get 15 points in 60 seconds, they move to a different level. If they don't, they start over.

Can you please elaborate how to and where to place the function you suggested?

German

Ned Murphy
Legend
January 21, 2010

I'm not sure what to elaborate on.  You should know how a Timer works, so you just need to create one that has a 60 second timeout.  And when that Timer fires, it triggers a function that does whatever you intend for the piece that might have been present (it could time out between pieces).  The code you said didn't work, might be the code in that function, or it may be more like the code you have for the dropIt function where it tests where the piece is and gives a yes or a no as far as where that piece is relative to the right place.  THat's really up to you to decide how you want to manage the piece when time runs out.