Kinetic.js动画包含形状和文本的多个组

时间:2013-11-06 20:00:10

标签: javascript html5 canvas kineticjs

我是Kinetic.js的新手。我正在尝试创建一系列随机移动的组,每组包含一个星和一个标题。这是当前的草图:http://jsfiddle.net/dCw9e/

我无法弄清楚为什么每个组对象在动画中都有一组唯一的边界。任何人都可以解释我在这里缺少的东西吗?为什么每个组的边界不一样?即父容器的边界。

这是动画:

var anim = new Kinetic.Animation(function(frame){

newtime = frame.time;

// Acceleration due to gravity via time delay (chunks miliseconds)
if((newtime - oldtime) > framerate) { // every N milliseconds... (this emulated a frame rate)
    oldtime = newtime;

    var angularSpeed = Math.PI / 2;
    var angleDiff = [];


    var stage = MilestonesGame.stage;
    var stageKids = stage.getChildren();
    var starsLayer = stageKids[1];
    var stars = [];
    stars = starsLayer.getChildren();
    //console.log(stars[1].getX());

    for(var n= 0; n < groups.length; n++){
        angleDiff[n] = frame.timeDiff * angularSpeed / 10000 * rotDir[n];
    }   


    for(var j = 0; j < groups.length; j++) {



    /* Bounce stars off all stage parameter boundaries */

    // floor boundary
    if(groups[j].getY() > floor) {groups[j].setY(floor);}
    if(groups[j].getY() == floor) {

     $(window).resize(_.debounce(function(){
        floor = MilestonesGame.stage.getHeight()-7;
     }, 300));

        yvel[j] *= -1;

    }

    //Ceiling boundary
    if(groups[j].getY() < cieling){groups[j].setY(cieling);}
    if(groups[j].getY() == cieling){

        yvel[j] *= -1;

    }
    // right wall
    if(groups[j].getX() > rightwall) {groups[j].setX(rightwall);}
    if(groups[j].getX() == rightwall) {

        $(window).resize(_.debounce(function(){
            rightwall = MilestonesGame.stage.getWidth()-7;
        }, 300));

        xvel[j] *= -1;
    }
    // left wall
    if(groups[j].getX() < leftwall) {groups[j].setX(leftwall);}
    if(groups[j].getX() == leftwall) {

        xvel[j] *= -1;
    }

    groups[j].setX(groups[j].getX() + xvel[j]);                                 
    groups[j].setY(groups[j].getY() + yvel[j]);
    //stars[i].rotate(angleDiff[i]);
    }
}

},this.starsLayer); anim.start();

谢谢, 乔恩

1 个答案:

答案 0 :(得分:2)

任何组的origin [x,y]相对于其父组默认为[0,0]。

在您的情况下,每个父母群体都是舞台。

所以你的所有小组的起源 - 他们的x / y,都与舞台有关。

一些观察......

在任何循环之外使用调整大小事件处理程序(如果在循环内部,则执行错误操作)。

// resizing handler -- never put this in a loop!

$(window).resize(_.debounce(function(){
    rightwall = MilestonesGame.stage.getWidth();
    floor = MilestonesGame.stage.getHeight();
}, 300));

让您的边界变得全尺寸,以便日后轻松调整星星大小

// boundaries -- leave boundaries full width/height
// which lets you resize each star later

var leftwall = 0;
var rightwall = MilestonesGame.stage.getWidth();
var floor = MilestonesGame.stage.getHeight();
var cieling = 0;

您的边界命中测试可以像这样重构,以提高性能和清晰度:

// Bounce stars off all stage parameter boundaries 

    for(var j = 0; j < groups.length; j++) {


    // temp save often used array references in vars

    var group=groups[j];
    var x=group.getX();
    var y=group.getY();
    var r=group.getOuterRadius();

    // ceiling boundary

    if(y-r<=cieling) {
        y=cieling+r;
        yvel[j] *= -1;
    }

    // floor boundary

    if(y+r>=floor) {
        y=floor-r;
        yvel[j] *= -1;
    }

    // left boundary

    if(x-r<=leftwall) {
        x=leftwall+r;
        xvel[j] *= -1;
    }

    // left and right boundary

    if(x+r>=rightwall) {
        x=rightwall-r;
        xvel[j] *= -1;
    }

    // move this star
    group.setX(x + xvel[j]);                                    
    group.setY(y + yvel[j]);

    }