我有點失望,首先看到我的問題的倒票。我可以想象,僅僅試圖讓他們的代碼寫在這裏的新人有很多問題,但這不是我問的問題。
無論如何,「你分擔問題,你分享解決方案」,心態,我在tw_hoff的幫助下修復了代碼,現在所有的代碼都可以工作。它甚至可以將座標保存在本地存儲中,因此,如果刷新頁面,此示例HTML將使您保持在同一位置。我添加了我使用的兩個示例圖像(將左邊的圖像存儲爲farm.png,將正確的圖像存儲爲tile.png,與html頁面相同的目錄)。
的實際代碼:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Game map demo</title>
<style>
body { margin: 0; padding: 0; }
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div id="map" style="position: absolute; overflow: hidden; width: 100%; height: 100%; background-color: darkgreen;">
<div id="content" style="white-space: nowrap;">
</div>
</div>
<script>
for(i = 0; i < 20; i++) {
for(j = 0; j < 20; j++) {
var tile;
if((i == 4 || i == 5) && (j == 2 || j == 3)) {
tile = 'farm';
} else {
tile = 'tile';
}
$("#content").append('<div style="background: url(\'' + tile + '.png\'); width: 128px; height: 128px; position: absolute; margin-left: ' + (i * 128) + 'px; margin-top: ' + (j * 128) + 'px;"></div>');
}
}
$("body").css("-webkit-user-select","none");
$("body").css("-moz-user-select","none");
$("body").css("-ms-user-select","none");
$("body").css("-o-user-select","none");
$("body").css("user-select","none");
var down = false;
var current_left = 0;
var current_top = 0;
if(localStorage.getItem("current_left") && localStorage.getItem("current_top")) {
current_left = Number(localStorage.getItem("current_left"));
current_top = Number(localStorage.getItem("current_top"));
console.log(current_left);
$("#content").css('marginLeft', (current_left) + 'px');
$("#content").css('marginTop', (current_top) + 'px');
}
$(document).mousedown(function() {
down = true;
}).mouseup(function() {
down = false;
});
var cache_pageX;
var cache_pageY;
$("#map").mousemove(function(event) {
if(down == true) {
current_left += (-1 * (cache_pageX - event.pageX));
if(current_left > 0)
current_left = 0;
if(current_left < (-2560 + $("#map").width()))
current_left = (-2560 + $("#map").width());
current_top += (-1 * (cache_pageY - event.pageY));
if(current_top > 0)
current_top = 0;
if(current_top < (-2560 + $("#map").height()))
current_top = (-2560 + $("#map").height());
localStorage.setItem("current_left", current_left);
localStorage.setItem("current_top", current_top);
$("#content").css('marginLeft', (current_left) + 'px');
$("#content").css('marginTop', (current_top) + 'px');
}
cache_pageX = event.pageX;
cache_pageY = event.pageY;
});
</script>
</body>
</html>
它仍然需要一些工作,並在遊戲中實際使用的過程中做了大量的工作,但是這部分的作品,我希望如果別人可能會遇到同樣的問題,並在stackoverflow上搜索它,我的解決方案給了他們一個正確的方向:)。
感謝那些幫助!
沒有示例代碼,我們不會幫助。 – Oen44
我明白,但沒有代碼樣本,因爲沒有代碼。我試圖找到一個可以工作的概念,然後我會寫代碼(我不希望有人寫我的任何代碼)。我不知道從哪裏開始。 –
我認爲這個問題對於Stack Overflow來說太廣泛了。通常你必須自己嘗試一些東西,並在遇到困難時詢問具體問題... – Badacadabra