我知道这不是最好的问题,但我真的被困住了。我试图在这个链接中创建一个类似on的游戏:http://richard.parnaby-king.co.uk/examples/games/balloons/,只使用Jquery/Javascript/CSS/HTML。(所以我不能用闪光灯)。
我知道这场比赛看上去不太好,也不太流畅,但我现在所拥有的甚至都不起作用。
首先,我创建了一个div,当然是‘游戏场’。然后我想每隔200毫秒在页面底部加一个气球,它会上升。它适用于一个气球,但当我添加更多的气球时,当.animate函数启动时,气球就会跳转到屏幕的顶部。
var divheight = $("#playfield").height();
var divwidth = $("#playfield").width();
var y = 0;
while (y<=20) {
var position = Math.round(divwidth*Math.random());
setTimeout(function(){
var testballoon = '<img src="Afbeeldingen/testballon.png" class="blueballoon"'
testballoon+= 'style="position:absolute;bottom:0px;' + 'left:' + position + 'px;"/>';
$("#playfield").append(testballoon);
}, 1000); y++}但我甚至不能把图像放在一个好的地方,而动画也不像我想要的那样工作。我不是在要求现成的代码,但是如果有人有类似的经验,并且知道一种更好的方法来随机添加元素/在页面上移动元素,我会很感激的。
发布于 2014-04-28 19:20:05
一种创建新对象的方法的下面是一个JSFiddle示例,对它们进行动画化,然后删除它们。下面是此示例的代码。要成为一场比赛,当然还有很长的路要走,除非你喜欢看气球飘浮起来!
HTML
<div class="gamefield">
Click this area to add balloons!
<br />
Click fast or click slow!
</div>CSS
div.gamefield {
width: 100%;
height: 500px;
border: 1px red solid;
}
div.balloon {
position: absolute;
bottom: 0px;
left: 0px;
width: 100px;
height: 150px;
background-size: 100% 100%;
background-repeat: no-repeat;
}Javascript
var burl = "http://etc-mysitemyway.s3.amazonaws.com/icons/legacy-previews/icons/green-jelly-icons-culture/028931-green-jelly-icon-culture-balloon.png";
function createBalloon() {
var gamefield = $("div.gamefield");
var d = $(document.createElement("div"));
d.addClass("balloon");
d.css("background-image", "url('" + burl + "')");
var maxwidth = gamefield.width() - 100;
var left = Math.round(maxwidth * Math.random());
d.css("left", left+"px");
d.css("bottom", "0px");
gamefield.append(d);
d.animate({
bottom: gamefield.height()
}, 3000, function() {
animationComplete($(this));
});
}
function animationComplete(jobj) {
jobj.remove();
}
$(function() {
$("div.gamefield").click(function() {
createBalloon();
});
});发布于 2014-04-28 20:18:00
检查这个小提琴
jQuery
var div ="<div class='balloon'></div>";
var $div = $(div);
var totalHits = 0;
var totalSeconds = 30;
var time = 0;
var colors = ["red","green","blue","yellow","brown","orange","black","gray"];
var game = setInterval(function(){
time = time + 3;
for(i=0;i<5;i++){
var numBottom = Math.floor(Math.random()*100);
var numLeft = Math.floor(Math.random()*300);
var colorRand = colors[Math.floor(Math.random()*colors.length)];
var cloneDiv = $div.clone(true);
cloneDiv.css({
"background-color":colorRand,
"bottom":-numBottom,
"left":numLeft
}).appendTo("#panel");
cloneDiv.animate({
bottom:"700px"
},20000,function(){
cloneDiv.remove();
});
cloneDiv.on("click",function(){
$(this).hide();
totalHits = totalHits + 1;
});
if(time > totalSeconds){
clearInterval(game);
$("#panel").html("<h2>Game Over</h2><br/><h3>your Score: " + totalHits + "</h3>");
}
}
},3000);https://stackoverflow.com/questions/23348862
复制相似问题