可转换回调在Famous Engine中不起作用

时间:2015-07-06 22:01:44

标签: famous-engine

学习新的Famous Engine时的另一个问题:为可转换设置新状态时,可以设置回转完成时的回调。对我来说,转换补间到指定持续时间的最终数字,但回调没有运行。这是我的代码:

'use strict';

var famous = require('famous');
var DOMElement = famous.domRenderables.DOMElement;
var Transitionable = famous.transitions.Transitionable;
var FamousEngine = famous.core.FamousEngine;
var Node = famous.core.Node;

// Create scene.
var scene = FamousEngine.createScene();

var boxNode = scene.addChild();

boxNode.setSizeMode('absolute', 'absolute')
    .setAbsoluteSize(300, 300);

var boxElement = new DOMElement(boxNode);
boxElement.setProperty('background-color', 'lightblue');

var boxTransitionable = new Transitionable(0);

// Callback is specified but it never successfully runs.
boxTransitionable.set(100, { duration: 1000 }, function () { console.log('this never runs'); });

FamousEngine.init();

1 个答案:

答案 0 :(得分:1)

没有调用callback函数的原因是Transitionableget()上更新,并且在队列项中的所有迭代完成之前不会完成。

  

来自指南:请务必注意,Transitionables不会自行更新,而是在计算当时的状态.get()被调用。

尝试使用此代码(取消注释from-to line并再次尝试):

var DOMElement = famous.domRenderables.DOMElement;
var Transitionable = famous.transitions.Transitionable;
var FamousEngine = famous.core.FamousEngine;
var Node = famous.core.Node;

// Create scene.
var scene = FamousEngine.createScene();

var boxNode = scene.addChild();

boxNode.setSizeMode('absolute', 'absolute')
    .setAbsoluteSize(300, 300);

var boxElement = new DOMElement(boxNode);
boxElement.setProperty('background-color', 'lightblue');

FamousEngine.init();
var clock = FamousEngine.getClock();

var boxTransitionable = new Transitionable(0);

// Callback is ran at end of state.
function done() { 
  console.log('it runs at the end'); 
}
function start() {
  console.log(boxTransitionable.get(), boxTransitionable.isActive());
  if (boxTransitionable.isActive()) {
    clock.setTimeout(start, 200);
  }
}

//boxTransitionable.from(1).to(100, 'linear', 2000,  done);
boxTransitionable.set(1, { duration: 2000, curve: 'linear' },  done);
start();

Transitionable是两个州之间随时间的过渡(补间)。以下代码段是使用Transitionable使用自定义组件定位节点的示例。



var DOMElement = famous.domRenderables.DOMElement;
var Transitionable = famous.transitions.Transitionable;
var FamousEngine = famous.core.FamousEngine;
var Node = famous.core.Node;

// Create scene.
var scene = FamousEngine.createScene();
FamousEngine.init();

var boxNode = scene.addChild();

boxNode.setSizeMode('absolute', 'absolute')
    .setAbsoluteSize(100, 100)
    .setPosition(0,0,0);

boxNode.addUIEvent('click');

var boxElement = new DOMElement(boxNode);
boxElement.setProperty('background-color', 'lightblue');
boxElement.setContent('Click Me');

var clock = FamousEngine.getClock();

var boxTransitionable = new Transitionable(0);

var myComponent = {
  id: null,
  node: null,
  onMount: function (node) {
      this.id = node.addComponent(this);
      this.node = node;
  },
  onReceive(type, event) {
    if (type === 'click') {
      this.node.requestUpdate(this.id);
      boxTransitionable.from(0).to(300, 'outBounce', 2000,  done);
      boxTransitionable.set(0, { duration: 2000, curve: 'outBounce' },  done);
    }
  },
  onUpdate: function() {
    if (boxTransitionable.isActive()) {
      var xpos = boxTransitionable.get();
      console.log(xpos, boxTransitionable.isActive());
      this.node.setPosition(xpos,0,0);
      this.node.requestUpdateOnNextTick(this.id);
    }
  }
};
boxNode.addComponent(myComponent);

// Callback is specified but it never successfully runs.
function done() { 
  console.log('at the end'); 
}

            html, body {
                width: 100%;
                height: 100%;
                margin: 0px;
                padding: 0px;
              
            }
            body {
                position: absolute;
                -webkit-transform-style: preserve-3d;
                transform-style: preserve-3d;
                -webkit-font-smoothing: antialiased;
                -webkit-tap-highlight-color: transparent;
                background-color: black;
                -webkit-perspective: 0;
                perspective: none;
                overflow: hidden;
            }

<!DOCTYPE html>
<html>
    <head>
      <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Famous0.6.2</title>
        <link rel="icon" href="favicon.ico?v=1" type="image/x-icon">
        <meta name="description" content="Transition callback Famous@0.6.2">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="http://code.famo.us/famous/0.6.2/famous.min.js"></script>
    </head>
    <body>
    </body>
</html>
&#13;
&#13;
&#13;