jQuery .animate()冲突检测 [英] jquery .animate() collision detection

查看:107
本文介绍了jQuery .animate()冲突检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用动画在div网格内移动div.作为测试,我将一些网格div设置为障碍.如果动画div与障碍物之一碰撞,我想结束动画.插件很好,也可以编写一小段代码.我有点怀疑我用.animate()嗅到了错误的树,因此任何朝着正确方向或我正在做的事情的改进都值得赞赏:

I'm using animate to move a div within a grid of divs. As a test I am setting some of the grid divs as obstacles. If the animated div collides with one of the obstacles I want to end the animation. A plug in is fine or a snippet of code. I kind of suspect I am sniffing up the wrong tree with .animate() so any shove in the right direction or improvement on what I am doing is appreciated:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html lang="en">
<head>
<meta charset="utf-8">
<title>Game Test</title>
<!-- meta tags -->
<meta name="keywords" content="">
<meta name="description" content="">

<!-- javascript -->
<script src="../js/jquery.js"></script>
<script language="javascript">

//set up a var for the cube to check to see if it is moving
    var move = true;

    //set up gameBoard parameters
    var minX = 100;
    var maxX = 1000;
    var minY = 200;
    var maxY = 500;

$(document).ready(function(){

    $(".mapGrid").click(function() {

        //get the locations of all the obstacles
        var obstacles = new Array();
        var mapGrid = $(".mapGrid").each(function() { 
            if($(this).attr("isObstacle") == "true") {

                obstacles.push($(this));
            }
        });

        //get position of div clicked
        var divPos = $(this).position();

        //get cube location
        var cubePos = $("#gameCube").position();
        var cubeLeft = cubePos.left - 101;
        var cubeTop = cubePos.top - 101;

        var animateX = (divPos.left - cubeLeft);
        var animateY = (divPos.top - cubeTop);

        $("#gameCube").animate({"left": "+=" + animateX + "px"}, "slow");
        $("#gameCube").animate({"top" : "+=" + animateY + "px"}, "slow");

    });

});

</script>

<!-- stylesheets -->

<style>

#gameCube { 
    position: absolute;
    top: 101px;
    left:101px;
    background-color: #ff0044;
    width: 40px;
    height: 40px;
    z-index: 1;
}

#gameBoard {
    position: absolute;
    top: 100px;
    left:100px;
    border: 1px solid black; 
    width: 1000px;
    height: 500px;
    z-index: 0;
}

    #gameBoard div.mapGrid { 
        background-color: #cccccc;
        float: left;
        height: 50px;
        width: 50px;
    }

        #gameBoard div.mapGrid img { 
            float: left;
        }
</style>

<!--conditional comments -->

</head>

<body>

<div id="msgBox"></div>
<div id="gameCube">&nbsp;</div>
<div id="gameBoard">


<?php 

    $rows = 10;
    $cols = 20;
    $x=-100; 
    $y=-100;

    $obstacles = array("0" => array("1", "5"), "1" => array("4", "10"), "2" => array("6", "18"), "3" => array("7", "2"), "4" => array("8", "11")); 

    //loop through the columns
    for($r=0; $r<$rows; $r++) { 

        //loop through the rows
        if ($r == 0) { 

            //loop through the columns
            for($c=0; $c<$cols; $c++) { 
                print "<div id=\"mapGrid_" . $r . "_" . $c . "\" row=\"" . $r . "\" col=\"" . $c . "\" class=\"mapGrid\" >" . $r . "::" . $c . "</div>";
            }
        } else { 

            $x=$x+100;
            $y=$y+100;

            for($c=0; $c<$cols; $c++) { 

                $isObstacle = false;

                //loop through the obstacles
                for($o=0; $o<count($obstacles); $o++) { 

                    $thisObsArray = $obstacles[$o];
                    $thisObsRow = $thisObsArray[0];
                    $thisObsCol = $thisObsArray[1];

                    if ($thisObsRow == $r) { 
                        if ($thisObsCol == $c) { 
                            $isObstacle = true; 
                        }
                    }

                }

                if($isObstacle == true) { 
                    print "<div id=\"mapGrid_" . $r . "_" . $c . "\" row=\"" . $r . "\" col=\"" . $c . "\" class=\"mapGrid\"  isObstacle=\"true\" style=\"background-Color: #44ff66; position: relative; top: " . $y. "px; left: " . $x . "px; \">" . $r . "::" . $c . "</div>";
                } else { 
                    print "<div id=\"mapGrid_" . $r . "_" . $c . "\" row=\"" . $r . "\" col=\"" . $c . "\" class=\"mapGrid\"  isObstacle=\"false\" style=\"position: relative; top: " . $y. "px; left: " . $x . "px; \">" . $r . "::" . $c . "</div>";
                }
            }

            $y=$y-100;
            $x=-100; 
        }
    }
?>
</div>

</body>
</html>

推荐答案

我也遇到了与您同样的问题,但是我决定制定自己的解决方案.我写了一个插件来将函数调用绑定到冲突.

I was faced with the same issue you are having, but I have decided to make my own solution. I wrote a plugin to bind a function call to collisions.

实时演示: http://jsfiddle.net/lilglower/mptp3/5/

这里是函数:

$.fn.collide = function($selector, $callback){
    exists = 'collide' in $;
    if(!exists) $.collide = [];
    $.collide.push({
        s1 : $(this),
        s2 : $($selector),
        callback : $callback
    });
    if(!$.collideStarted){
        $.collideStarted = true;
        setInterval(function(){
            $.each($.collide, function(id, data){
                data.s1.each(function(){
                    $s1 = $(this);
                    data.s2.each(function(){
                        $s2 = $(this);
                        position1 = $s1.position();
                        position2 = $s2.position();
                        size1 = {width : $s1.width(), height : $s1.height()};
                        size2 = {width : $s2.width(), height : $s2.height()};
                        if (((position1.left + size1.width) > position2.left) &&
                        ((position1.top + size1.height) > position2.top) &&
                        ((position2.left + size2.width) > position1.left) &&
                        ((position2.top + size2.height) > position1.top))
                        {
                            data.callback($s1, $s2);
                        }
                    })
                })
            })
        }, 50);
    }
}

以下是用法示例:

$(function(){
    $(".left").animate($(".right").position());
    $(".left").collide(".right", function($left, $right){
         alert('collide!');
    })

})

这篇关于jQuery .animate()冲突检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆