gpt4 book ai didi

jquery .animate() 碰撞检测

转载 作者:行者123 更新时间:2023-12-01 04:25:20 25 4
gpt4 key购买 nike

我正在使用 animate 在 div 网格内移动 div。作为测试,我将一些网格 div 设置为障碍。如果动画 div 与障碍物之一发生碰撞,我想结束动画。一个插件或者一段代码就可以了。我有点怀疑我用 .animate() 嗅到了错误的树,所以任何朝正确方向的插入或对我正在做的事情的改进都是值得赞赏的:

<!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>

最佳答案

下面是 jQuery 动画和碰撞的简单实现:http://jsfiddle.net/itechnology/XKJKD/

这里发布了类似的问题:jQuery collision detection during animation其中引用了一个看起来很有前途的 jQuery 碰撞插件:http://sourceforge.net/projects/jquerycollision/

关于jquery .animate() 碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6837167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com