任何人都可以告诉我如何通过满足两个条件来停止计时器:
TestObjects
播放器按照特定顺序将分散在舞台上的电影剪辑拖放到正确的位置,如在字母学习游戏中,并且当最后一个字母被放入其适当的位置时,计时器停止。
我尝试了几种方法,包括"&&"方法,但它似乎不起作用。
我是 as3 的新手,所以请不要使用面向对象的编程方法来回答。
答案 0 :(得分:0)
最简单的方法是检查每个帧是否对象位于正确位置并停止计时器:
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);
答案 1 :(得分:0)
我会尝试通过mouseUP来驱动它,因为当你停止拖动时总会发生这种情况。可能是这样的:
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();
}
}
答案 2 :(得分:0)
执行此操作的一种方法是将每个MovieClip的目标位置存储为该块上的属性。 (假设您使用的是动态的MovieClip,那么您可以向它们添加属性)
每个帧或者经常要测试这种情况只是循环浏览影片剪辑并检查每个MovieClip的x,y是否与您在MovieClip上创建的targetX和targetY匹配。
例如:
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.
}
所以每一帧或每当你想检查时都可以去:
if (areWeDoneYet())
{
// do whatever you need to do.
// stop the timer or whatever
}
此解决方案假定您的所有字母都是容器MovieClip的子代。您可以将相同的概念与包含这些MovieClip的数组一起使用,然后迭代它。