如何向上和向下补间文本,并使其对触摸事件做出反应

时间:2014-07-10 17:20:45

标签: actionscript-3 tweenlite

我在actionscript中有一个Tweenlite动画,我想在Touch事件上执行一个会中断补间的方法。实际上它会暂停它,执行方法然后继续补间 我不知道该怎么做。有人可以帮忙吗?

谢谢!

所以,我知道这不起作用。我的问题是如何让它发挥作用。

package { 

imports... 

public class App extends Sprite {

private var textField:TextField = new TextField(220, 35, "Tap to flip the text!", "Roboto", 22, 0xf1f1f1, false);

public function App() {
    super();
    addEventListener(Event.ADDED_TO_STAGE, textPlay);
}

private function textPlay(event:Event):void {
    removeEventListener(Event.ADDED_TO_STAGE, textPlay);

    textField.border = 1;
    textField.hAlign;
    textField.vAlign;

    textField.x = stage.stageWidth / 2 - textField.width / 2;
    textField.y = stage.stageHeight / 2 - textField.height / 2;

    addChild(textField);

    addEventListener(TouchEvent.TOUCH, turn);

    down();
}

private function down():void {
    addEventListener(starling.events.Event.ENTER_FRAME, check);
    TweenLite.to(textField, 5, {y:(stage.stageHeight - textField.height)});
    up();
}

private function up():void {
    TweenLite.to(textField, 5, {y:0});
    down()
}

private function turn(event:TouchEvent):void {
    TweenLite.to(textField, 0, {rotation:180});
}

}
}

1 个答案:

答案 0 :(得分:2)

您需要将补间存储在变量中,以便以后可以对其进行操作

var myTween:TweenLite = TweenLite.to(...);

function myTouchEventHandler(e:TouchEvent):void {
    myTween.pause(); //pause the tween
}

function someOtherFunction():void {
    myTween.resume(); //resume tweening from where it left off
}

修改

以下是我认为您正在尝试做的事情(与暂停或停止补间事件无关)

package { 

imports... 

public class App extends Sprite {

    private var textField:TextField = new TextField(220, 35, "Tap to flip the text!", "Roboto", 22, 0xf1f1f1, false);

    public function App() {
        super();
        addEventListener(Event.ADDED_TO_STAGE, textPlay);
    }

    private var moveSpeed:int = 2; //how many pixels to move the textField every frame

    private function textPlay(event:Event):void {
        removeEventListener(Event.ADDED_TO_STAGE, textPlay);

        textField.border = 1;
        textField.hAlign;
        textField.vAlign;

        textField.x = stage.stageWidth / 2 - textField.width / 2;
        textField.y = stage.stageHeight / 2 - textField.height / 2;

        addChild(textField);

        addEventListener(TouchEvent.TOUCH, turn);

        //this instead of using a tween, just use the enterframe method and move the textField up (or down) every frame
        addEventListener(starling.events.Event.ENTER_FRAME, moveText);
    }

    private function moveText(event:flash.events.Event):void {
        if(textField.y + moveSpeed > (stage.stageHeight - textField.height) || textField.y + moveSpeed < 0){
            moveSpeed *= -1; //change direction
        };

        textField.y += moveSpeed;
    }

    private function turn(event:TouchEvent):void {
        TweenLite.to(textField, 0, {rotation:textField.rotation == 180 ? 0 : 180}); //assuming you want to toggle 180 or 0, not just always set to 180
    }
}
}

如果您仍想使用补间,那么就是这样:(使用tweenLite的onComplete参数)

function up():void {
    TweenLite.to(textField, 5, {y:0, onComplete: down});
}

function down():void {
    TweenLite.to(textField, 5, {y:(stage.stageHeight - textField.height), onComplete: up});
}

function turn(event:TouchEvent):void {
    //need the overwrite property so it doesn't cancel the other tween on this same object
    TweenLite.to(textField, 0, {overwrite: 0, rotation:textField.rotation == 180 ? 0 : 180}); //assuming you want to toggle 180 or 0, not just always set to 180
}

或者,使用TweenMax并将yoyo属性设置为true,只使用一个补间。