1.How 무작위로 파이프 장애물을 산란을 새가 날아갈 수 있도록 틈새 위치 (틈새 위치에 대한 파이프 div의 CSS '오른쪽'속성을 변경하는 기능 사용) 및 화면 맨 아래로 가면 파이프 제거. (그것의 거꾸로 flappy 버드 게임처럼 btw ..)
2.Second 나는 충돌 감지 기능에 대한 도움이 필요하므로 게임이 끝났을 때 알 수있다. (덜 중요한 힘든 원인은 내가 알아낼 수 있다고 생각한다. 병이 해결 첫 번째 문제)
$(document).ready(function(){
//Variables
var screenWidth = $(window).width();//Screen width
var screenHeight = $(window).height();//Screen height
var birdWidth = $("#bird").width();//bird width
var y = 0;//Background y position
var astY = 0;//Pipe position
var bird = {//bird properties
goingLeft: false,
goingRight: false,
lspeed: 0,
rspeed: 0,
maxSpeed: 10
};
setBirdPosition(screenWidth/2 - birdWidth/2, screenHeight/1.3 - birdWidth/2);
startBackgroundMovement();
spawnPipe();
//Start move the screen
function startBackgroundMovement(){
setInterval(function()
{
y+=1;
$('body').css('background-position-y',y + 'px');
}, 10);
}
//bird starting position
function setBirdPosition(posX, posY) {
$("#bird").css("left", posX);
$("#bird").css("top", posY);
bird.position = posX;
}
(function birdLoop() {
if (bird.goingLeft) {
bird.lspeed = Math.min(bird.lspeed *1.1 || 1, bird.maxSpeed);
} else {
bird.lspeed = Math.max(bird.lspeed - 0.5, 0);
}
if (bird.goingRight) {
bird.rspeed = Math.min(bird.rspeed *1.1 || 1, bird.maxSpeed);
} else {
bird.rspeed = Math.max(bird.rspeed - 0.5, 0);
}
bird.position = bird.position + (bird.rspeed - bird.lspeed);
$("#bird").css('left', bird.position);
requestAnimationFrame(birdLoop);
}());
//Move bird
$(document).keydown(function(e){
switch(e.which){
case 37://left
bird.goingLeft= true;
//left edge of screen
if (bird.position < 0) {
bird.position = 0;
}
break;
case 39://right
bird.goingRight= true;
//right edge of screen
if (bird.position > screenWidth - birdWidth) {
bird.position = screenWidth - birdWidth;
}
default: return;
e.preventDefault();//not sure if needed
}
}).keyup(function(e){
switch(e.which){
case 37://left
bird.goingLeft= false;
//left edge of screen
if (bird.position < 0) {
bird.position = 0;
}
break;
case 39://right
bird.goingRight= false;
//right edge of screen
if (bird.position > screenWidth - birdWidth) {
bird.position = screenWidth - birdWidth;
}
default: return;
e.preventDefault();//not sure if needed
}
});
function spawnPipe()//spawn pipes
{
setInterval(function()
{
astY += 1;
$("#fullPipe").css("top", astY);
}, 10);
}
});
확인이 : http://jsfiddle.net/u38ratk9/6/
jQuery로 쉽게 수행 할 수있는 요소의 네 모퉁이의 좌표 위치를 가져 와서 충돌 감지를 수행하면 다른 요소의 구석이 첫 번째 요소의 '상자'내에 있는지 비교할 수 있습니다. –
방금 [exact duplicate] (http://stackoverflow.com/questions/27665666/jquery-spawning-div-randomly-and-removing-it-when-offscreen?noredirect=10)를이 질문에 병합했습니다. 이와 같은 중복 질문을 게시하지 마십시오. –