0
votes

Can anybody please tell me how I can stop a timer by fulfilling two conditions:

  1. All objects on stage should hit their TestObjects
  2. Only when the last object hits its test object the timer will stop.

The player drags and drops movie clips scattered on the stage into its correct position, in a particular order, as in an alphabet learning game, and when the last alphabet is dropped into its appropriate place, the timer stops.

I tried several approaches including the "&&" approach but it does not seem to work.

I am new to as3, so please don't answer using the Object Oriented Programming method.

3
thanx Touki for editing my post :)user2835788
set counter to be equal to the number of objects, when object will hit the "TestObject" reduce the counter, once counter equals 0 stop timer.Lukasz 'Severiaan' Grela

3 Answers

0
votes

I would try and drive this via a mouseUP, seeing as that will always have to happen when you stop dragging. Possibly something like this:

var timer:Timer = new Timer(10000, 1);

var alphabetMembers:Array = [letterA,
                             letterB,
                             letterC,
                             //Stick the rest of your letter vars in here
                             ]

var correctLocations:Dictionary = new Dictionary();

correctLocations[letterA] = hitTestA;
correctLocations[letterB] = hitTestB;

//do the same for each character

timer.start();

this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp, true);

function onMouseUp(e:MouseEvent):void
{
    var correctLocation:uint = 0;

    for(var i:int = 0; i < alphabetMembers.length; i++)
    {
        if(alphabetMembers[i].hitTestObject(correctLocations[alphabetMembers[i]]))
        {
            correctLocation++;
        }
    }

    if(correctLocation >= alphabetMembers.length)
    {
        timer.stop();
    }
}
0
votes

The simplest way would be to check every frame if the objects are in the correct position and them stopping the timer:

var objects:Vector.<DisplayObject> = new Vector.<DisplayObject>();

private function onEnterFrame(ev:Event):void {
   //check positions of objets
   var allObjectsOK:Boolean = true;
   for each( var do:DisplayObject in objects ) {
       //check if do is in place by checking its x and y properties
       // in this exampel, if x and y are above 10, object is not in place
       if (do.x > 10 && do.y > 10) {
            allObjectsOK = false;
       }
   }

   if (allObjectsOK) {
       timer.stop();
   }
}

stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
0
votes

One way to do this would be to have the target location for each MovieClip stored as a property on the piece. (Assuming you are using MovieClips which are dynamic, so you can add a property to them)

Each frame or however often you want to test for this situation just loop through the movieclips and check if the x,y of each MovieClip matches the targetX and targetY that you created on the MovieClip.

for example :

public function areWeDoneYet():Boolean
{

     for (var index:int = 0;index < container.numChildren;index++)
     {
         var curLetter:MovieClip = container.getChildAt(index) as MovieClip;

         // test if the curLetter is at target location or close enough for your needs
        // if not return false

     }
     return true; // return true if the loop completed 

     // if it did complete, it means all MovieClips are in right target location.

}

So each frame or whenever you want to check you can go :

if (areWeDoneYet())
{
    // do whatever you need to do.
    // stop the timer or whatever
}

This solution assumes that all your letters are children of a container MovieClip. You could utilize the same concept with an array containing those MovieClips and iterating through that instead.