2017-01-29 97 views
0

這是我第一次嘗試JavaScript遊戲。 我正在試圖做一個黃色塊的JavaScript遊戲,你可以使用wasd鍵來控制。藍色方塊應該是一個敵人,如果你擊中了它,就會讓你鬆動。稍後我會添加更多功能。問題:遊戲工作正常,直到你釋放其中一個按鈕。如何在釋放控制器按鈕時防止播放器飛走?Javascript遊戲控件

我也不能讓按鈕工作時,我讓wasd按鍵控制器,但我不認爲,因爲在這一點上的一個重大問題。即便如此,如果有人知道如何解決這個問題,我會很開心。

var myGamePiece; 
 
var myFi; 
 

 
function startGame() { 
 
    myGameArea.start(); 
 
    myGamePiece = new component(30, 30, "rgba(200,200,0,0.5)", 10, 175); 
 
    //blueGamePiece = new component(20, 20, "blue", 300, 110); 
 
    myFi = new component(15, 15, "blue", 300, 120); 
 
} 
 
var myGameArea = { 
 
    canvas: document.createElement("canvas"), 
 
    start: function() { 
 
    this.canvas.width = 680; 
 
    this.canvas.height = 420; 
 
    this.context = this.canvas.getContext("2d"); 
 
    document.body.insertBefore(this.canvas, document.body.childNodes[0]); 
 
    this.interval = setInterval(updateGameArea, 20); 
 
    window.addEventListener('keydown', function(e) { 
 
     myGameArea.keys = (myGameArea.keys || []); 
 
     myGameArea.keys[e.keyCode] = (e.type == "keydown"); 
 
    }) 
 
    window.addEventListener('keyup', function(e) { 
 
     myGameArea.keys[e.keyCode] = (e.type == "keydown"); 
 
    }) 
 
    }, // I don't get why the comma is so important and what it does! 
 

 
    clear: function() { 
 
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height); 
 
    }, 
 

 
    stop: function() { 
 
    clearInterval(this.interval); 
 
    } 
 
} 
 

 
function component(width, height, color, x, y) { 
 
    this.gamearea = myGameArea; 
 
    this.width = width; 
 
    this.height = height; 
 
    this.speedX = 0; 
 
    this.speedY = 0; 
 
    this.x = x; 
 
    this.y = y; 
 
    this.update = function() { 
 
    ctx = myGameArea.context; 
 
    ctx.fillStyle = color; 
 
    ctx.fillRect(this.x, this.y, this.width, this.height); 
 
    } 
 
    this.newPos = function() { 
 
     this.x += this.speedX; 
 
     this.y += this.speedY; 
 
    }, 
 
    this.crashWith = function(enemy) { 
 
     var myleft = this.x; 
 
     var myright = this.x + (this.width); 
 
     var mytop = this.y; 
 
     var mybottom = this.y + (this.height); 
 
     var fileft = enemy.x; 
 
     var firight = enemy.x + (enemy.width); 
 
     var fitop = enemy.y; 
 
     var fibottom = enemy.y + (enemy.height); 
 
     var crash = true; 
 
     if ((mybottom < fitop) || (mytop > fibottom) || (myright < fileft) || (myleft > firight)) { 
 
     crash = false; 
 
     } 
 
     return crash; 
 
    } 
 
} 
 

 
function updateGameArea() { 
 
    if (myGamePiece.crashWith(myFi)) { 
 
    myGameArea.stop(); 
 
    } else { 
 
    myGameArea.clear(); 
 
    myGamePiece.speedX += myGamePiece.speedX; 
 
    myGamePiece.speedY += myGamePiece.speedY; 
 
    if (myGameArea.keys && myGameArea.keys[65]) { 
 
     myGamePiece.speedX = -1; 
 
    } 
 
    if (myGameArea.keys && myGameArea.keys[68]) { 
 
     myGamePiece.speedX = 1; 
 
    } 
 
    if (myGameArea.keys && myGameArea.keys[87]) { 
 
     myGamePiece.speedY = -1; 
 
    } 
 
    if (myGameArea.keys && myGameArea.keys[83]) { 
 
     myGamePiece.speedY = 1; 
 
    } 
 
    myGamePiece.newPos(); 
 
    //blueGamePiece.x -=1; 
 
    //blueGamePiece.y +=2; 
 
    //blueGamePiece.update(); 
 
    myGamePiece.update(); 
 
    myFi.update(); 
 
    } 
 
} 
 

 
function moveup() { 
 
    myGamePiece.speedY = 1; 
 
} 
 

 
function movedown() { 
 
    myGamePiece.speedY = 1; 
 
} 
 

 
function moveleft() { 
 
    myGamePiece.speedX = 1; 
 
} 
 

 
function moveright() { 
 
    myGamePiece.speedX = 1; 
 
} 
 

 
function clearmove() { 
 
    myGamePiece.speedX = 0; 
 
    myGamePiece.speedY = 0; 
 
}
body { 
 
    text-align: center; 
 
} 
 
canvas { 
 
    border: 1px solid #d3d3d3; 
 
    background-color: #f4f4f4; 
 
} 
 
div { 
 
    text-align: center; 
 
    width: 300px; 
 
    height: 100px; 
 
} 
 
button { 
 
    background-color: black; 
 
    color: yellow; 
 
}
<div> 
 
    <button onmousedown="moveup()" onmouseup="clearmove()">UP</button> 
 
    <br> 
 
    <br> 
 
    <button onmousedown="moveleft()" onmouseup="clearmove()">LEFT</button> 
 
    <button onmousedown="moveright()" onmouseup="clearmove()">RIGHT</button> 
 
    <br> 
 
    <br> 
 
    <button onmousedown="movedown()" onmouseup="clearmove()">DOWN</button> 
 
</div> 
 

 
<p>I have created a game area!</p> 
 
<p>Then I created a component to my game! I is a yellow square!</p> 
 
<p>I continued to add frames and movement</p> 
 
<p>After I had added movement, I could finally add controllers</p> 
 
<p>First I added the buttons that was controlled by the mouse, 
 
    <br>then I added the keystroke controllers for the w,s,a,d keys.</p>

回答

0

TLDR:一個的jsfiddle證明https://jsfiddle.net/tty7y1gr/3/

你問的第一件事是如何讓玩家停止移動。嘗試改變這些行:

myGamePiece.speedY = 1; 

這樣:

myGamePiece.speedY += 1; 

而且,你不設置運動下,左,右正確:沒有負值。試試這個:

function moveup() { 
    myGamePiece.speedY -= 1; 
} 

function movedown() { 
    myGamePiece.speedY += 1; 
} 

function moveleft() { 
    myGamePiece.speedX -= 1; 
} 

function moveright() { 
    myGamePiece.speedX += 1; 
} 

而另一件事是你必須在這裏這段代碼:

myGamePiece.speedX += myGamePiece.speedX; 
myGamePiece.speedY += myGamePiece.speedY; 

這將使得該遊戲人物已經交錯,零星的運動,因爲速度將繼續增加在它自直到遊戲片段的速度幾乎呈指數級增長。除非這是故意的,爲了消除這種運動,擺脫這兩條線。

我在那裏看到的下一個問題是wasd鍵。嘗試是這樣的: 將此放在您的GameArea():

window.addEventListener('keydown', function (e) {    
     myGameArea.keys = (myGameArea.keys || []); 
     myGameArea.keys[e.keyCode] = true; 
}); 

window.addEventListener('keyup', function (e) { 
     myGameArea.keys[e.keyCode] = false; 
}); 

,這要在其中創建您的播放器「控制器」(或運動)

myGamePiece.speedX = 0; 
myGamePiece.speedY = 0; 
if (myGameArea.keys && (myGameArea.keys[37] || myGameArea.keys[65])) { 
    myGamePiece.speedX = -1; 
    } 
    if (myGameArea.keys && (myGameArea.keys[39] || myGameArea.keys[68])) { 
    myGamePiece.speedX = 1; 
    } 
    if (myGameArea.keys && (myGameArea.keys[38] || myGameArea.keys[87])) { 
    myGamePiece.speedY = -1; 
    } 
    if (myGameArea.keys && (myGameArea.keys[40] || myGameArea.keys[83])) { 
    myGamePiece.speedY = 1; 
    } 
} 

(這是方向鍵)

但是這會讓你的按鈕移動停止工作。爲了防止這種情況發生,你必須讓gameupdate()並不總是把你的遊戲片段的速度設置爲0.所以,在那裏放置一個支票,它將設置一個變量來釋放遊戲片段的速度,當你點擊屏幕,然後在停止按下這些按鈕時將其設回。類似這樣的:

在myGameArea之外。更新():

var buttonMove = false; 

裹棋子的速度的設置:

if (!buttonMove) { 
     myGamePiece.speedX = 0; 
     myGamePiece.speedY = 0; 
} 

內myGameArea.update()的:

function moveup() { 
    buttonMove = true; 
    myGamePiece.speedY -= 1; 
} 

function movedown() { 
buttonMove = true; 
    myGamePiece.speedY += 1; 
} 

function moveleft() { 
buttonMove = true; 
    myGamePiece.speedX -= 1; 
} 

function moveright() { 
buttonMove = true; 
    myGamePiece.speedX += 1; 
} 

function clearmove() { 
buttonMove = false; 
    myGamePiece.speedX = 0; 
    myGamePiece.speedY = 0; 
} 

我看見你爲什麼代碼中的註釋你需要一個逗號。您需要它,因爲您正在創建一個JavaScript對象,其屬性用逗號分隔。所以myGameArea看起來是這樣的:

var myGameArea = { 
    canvas: "value", 
    start: "value",  
    clear: "value",  
    stop: "value" 
} 

除非它在的地方看中「價值觀」,有些是功能,有些元素等

而最後一件事,以「鉗」您的播放器下來,他們就不能去畫布之外,你需要與你的後「控制器」是這樣設定的界限,但在更新位置之前:

Number.prototype.clamp = function(min, max) { 
    return Math.min(Math.max(this, min), max); 
    }; 

    myGamePiece.x = myGamePiece.x.clamp(0, myGameArea.canvas.width - myGamePiece.width); 
    myGamePiece.y = myGamePiece.y.clamp(0, myGameArea.canvas.height - myGamePiece.height); 

一個的jsfiddle證明:

https://jsfiddle.net/tty7y1gr/3/