为什么TweenLite.to和hitTestObject不起作用?

时间:2013-12-05 14:47:17

标签: actionscript-3

这是我的代码:

stop();
import com.greensock.*; 
import com.greensock.easing.*;
import com.greensock.TweenMax;
import com.greensock.TweenLite;
import flash.events.Event;
import com.greensock.TweenLite

stage.addEventListener(MouseEvent.CLICK, rijden); // Add the button click

    function rijden(e:MouseEvent):void {
        TweenLite.to(auto, 4, {x:666.15, y:375.6});
    }

    addEventListener(Event.ENTER_FRAME, einde1);

    function einde1(e:Event){
        if(auto.hitTestObject(stopauto)){
            var myTween=TweenLite.to(auto, 4, {x:666.15, y:375.6});
            myTween.kill();     //here code for tween killing
            trace("works")
            //
            auto.x = 241;
            auto.y = 375;
            removeEventListener(Event.ENTER_FRAME, einde1)
        }
    }

我希望如果自动点击stopauto,则自动转到

auto.x = 241;
auto.y = 375; 

它会跟踪,但它不会转到x和y我希望它去的地方

2 个答案:

答案 0 :(得分:0)

你有两个补间,其中一个永远不会被杀死。我想这很简单,并且会覆盖你最终的汽车目的地的x / y。

//tween one
function rijden(e:MouseEvent):void {
  TweenLite.to(auto, 4, {x:666.15, y:375.6});
}

//tween two
function einde1(e:Event){
  if(auto.hitTestObject(stopauto)){
    var myTween=TweenLite.to(auto, 4, {x:666.15, y:375.6});
    myTween.kill();     //here code for tween killing
    //REST OF YOUR CODE
  }
}

答案 1 :(得分:0)

@Fygo绝对正确。在enterframe处理程序中,您实际上创建了一个新的Tween,而不是获得对click处理程序中启动的Tween的引用,然后立即将其杀死,而不是继续执行的原始Tween

我认为以下内容将解决它:

// ... Rest of your code

function rijden(e:MouseEvent):void {
    // start the tween on mouse click
    TweenLite.to(auto, 4, {x:666.15, y:375.6});
}

addEventListener(Event.ENTER_FRAME, einde1);

function einde1(e:Event){
    if(auto.hitTestObject(stopauto)){

        // Objects have collided so stop the tween
        TweenLite.killTweensOf(auto);    

        // Place the object somewhere else
        auto.x = 241;
        auto.y = 375;

        removeEventListener(Event.ENTER_FRAME, einde1)
    }
}