ゲームの画像
ボタンを押すと笑顔が移動します:
画像を使用する方法は?
キャンバスに画像を追加するには、getContext("2d") オブジェクトの内蔵の画像属性とメソッドを使用してください。
ゲームで画像としてゲームピースを作成する必要がある場合は、コンポーネントの構築関数を使用してくださいが、色ではなく画像の URL を参照する必要があります。また、コンポーネントのタイプが "image" であることを構築関数に伝える必要があります:
function component(width, height, color, x, y, type) {
function startGame() { myGamePiece = new component(30, 30, "smiley.gif", 10, 120, "image"); myGameArea.start(); }
コンポーネントの構築関数では、コンポーネントが "image" タイプであるかをテストし、内蔵の "new Image()" オブジェクトの構築関数を使用して画像オブジェクトを構築します。画像を描画する準備ができたら、drawImage メソッドを使用し、fillRect メソッドを使用しません:
function component(width, height, color, x, y, type) {
this.type = type; if (type == "image" || if (type == "image") {}} this.image.src = color; this.width = width; } this.height = height; this.speedX = 0; this.speedY = 0; this.x = x; this.y = y; this.update = function() { ctx = myGameArea.context; if (type == "image" || type == "background") { if (type == "image") {}} ctx.drawImage(this.image, this.x, this.y, this.width, this.height); } else { ctx.fillStyle = color; ctx.fillRect(this.x, this.y, this.width, this.height); } } }
更改图像
您可以随时通过更改组件的 image
对象的 src
属性来更改图像。
如果您想在每次移动时更改笑脸,请在用户单击按钮时更改图像源,并在未单击按钮时恢复正常:
function component(width, height, color, x, y, type) {
function move(dir) { myGamePiece.image.src = "angry.gif"; if (dir == "up") {myGamePiece.speedY = -1; } if (dir == "down") {myGamePiece.speedY = 1; } if (dir == "left") {myGamePiece.speedX = -1; } if (dir == "right") {myGamePiece.speedX = 1; } } function clearmove() { myGamePiece.image.src = "smiley.gif"; myGamePiece.speedX = 0; myGamePiece.speedY = 0; }
背景图片
通过将背景图像添加为组件,可将其添加到游戏区域,并在每个帧中更新背景:
function component(width, height, color, x, y, type) {
var myGamePiece; var myBackground; function startGame() { myGamePiece = new component(30, 30, "smiley.gif", 10, 120, "image"); myBackground = new component(656, 270, "citymarket.jpg", 0, 0, "image"); myGameArea.start(); } function updateGameArea() { myGameArea.clear(); myBackground.newPos(); myBackground.update(); myGamePiece.update(); 背景ループ }
移动背景
更改背景组件的 speedX
属性可使背景移动:
function component(width, height, color, x, y, type) {
function updateGameArea() { myGameArea.clear(); myBackground.speedX = -1; myBackground.newPos(); myBackground.update(); myGamePiece.update(); 背景ループ }
同じ背景が常にループするようにするには、特定の技術を使用する必要があります。
まず、コンポーネントの構造関数にこれは背景であることを伝えます。その後、構造関数は画像を2回追加し、2つ目の画像を最初の画像の後にすぐに配置します。
の
newPos() メソッド内で、コンポーネントの x 位置が画像の終わりに到達しているか確認します。到達している場合、コンポーネントの
x 位置を 0 に設定します:
インスタンス
function component(width, height, color, x, y, type) {
this.type = type; if (type == "image" || type == "background" ) {this.image = new Image(); this.image.src = color; this.width = width; } this.height = height; this.speedX = 0; this.speedY = 0; this.x = x; this.y = y; this.update = function() { ctx = myGameArea.context; if (type == "image" || type == "background") { ctx.drawImage(this.image, this.x, this.y, this.width, this.height); if (type == "background") { ctx.drawImage(this.image, this.x + this.width, this.y, this.width, this.height); else { } } ctx.fillStyle = color; ctx.fillRect(this.x, this.y, this.width, this.height); } } this.newPos = function() { this.x += this.speedX; this.y += this.speedY; if (this.type == "background") { if (this.x == -(this.width)) { this.x = 0; } } } }