1

我正在尝试使用jQuery 1.9.1&制作动画jQuery-collision,我想在其中将一个对象与它所站立的对象一起拉起。站立平台会上升,它会带走站在上面的物体。但到目前为止,我无法做到这一点。这是我的代码,

$(document).ready(function () {
    heroDrop = setInterval(function () {
        var currentTop = parseInt($('#boy').css('top'));
        $('#boy').css('top', currentTop + 5);
    }, 50);

    barMove = setInterval(function () {
        var currentTop = parseInt($('.platbars').css('top'));
        $('.platbars').css('top', currentTop - 2);
    }, 100);

    setInterval(function () {
        action_process()
    }, 10);
});

function action_process() {
    $('#boy').each(function () {
        var fallstop = $(this).collision(".platbars");
        if (fallstop.length != 0) {
            clearInterval(heroDrop);
            heroDrop = null;
        } else {
            if (heroDrop !== null) return;
            heroDrop = setInterval(function () {
                var currentTop = parseInt($('#boy').css('top'));
                $('#boy').css('top', currentTop + 5);
            }, 50);
        }
    });
}

JSFiddle 演示在这里

我怎样才能使物体随平台上升?非常需要这个帮助!谢谢。

4

1 回答 1

2

我对你的代码做了一些调整:

var heroDrop;
var heroMove;
var barMove;
var coll;

$(document).ready(function () {
    heroDrop = setInterval(function () {
        var currentTop = parseInt($('#boy').css('top'));
        $('#boy').css('top', currentTop + 5);
    }, 50);

    barMove = setInterval(function () {
        var currentTop = parseInt($('.platbars').css('top'));
        $('.platbars').css('top', currentTop - 2);
    }, 100);

    coll = setInterval(function () {
        action_process()
    }, 10);
});

function action_process() {
    $('#boy').each(function () {
        var fallstop = $(this).collision(".platbars");
        if (fallstop.length != 0) {
            clearInterval(heroDrop);
            clearInterval(coll);
            heroDrop = setInterval(function () {
                var currentTop = parseInt($('#boy').css('top'));
                $('#boy').css('top', currentTop - 2);
            }, 100);
        }
    });
}
于 2015-06-07T16:03:23.463 回答