我正在根據以下代碼創建一個小游戲: https ://www.w3schools.com/graphics/tryit.asp?filename=trygame_default_gravity
一切都好,但我想把border-radius賭注放在綠色賭注上,有人有什么想法嗎?創建組件的部分是這樣的:
function component(width, height, color, x, y, type) {
this.type = type;
this.score = 0;
this.width = width;
this.height = height;
this.speedX = 0;
this.speedY = 0;
this.x = x;
this.y = y;
this.gravity = 0;
this.gravitySpeed = 0;
this.update = function() {
ctx = myGameArea.context;
if (this.type == "text") {
ctx.font = this.width " " this.height;
ctx.fillStyle = color;
ctx.fillText(this.text, this.x, this.y);
} else {
ctx.fillStyle = color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
}
this.newPos = function() {
this.gravitySpeed = this.gravity;
this.x = this.speedX;
this.y = this.speedY this.gravitySpeed;
this.hitBottom();
}
this.hitBottom = function() {
var rockbottom = myGameArea.canvas.height - this.height;
if (this.y > rockbottom) {
this.y = rockbottom;
this.gravitySpeed = 0;
}
}
this.crashWith = function(otherobj) {
var myleft = this.x;
var myright = this.x (this.width);
var mytop = this.y;
var mybottom = this.y (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y (otherobj.height);
var crash = true;
if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
crash = false;
}
return crash;
}
}
而他創建綠色木樁的部分是:
if (myGameArea.frameNo == 1 || everyinterval(150)) {
x = myGameArea.canvas.width;
minHeight = 20;
maxHeight = 200;
height = Math.floor(Math.random()*(maxHeight-minHeight 1) minHeight);
minGap = 50;
maxGap = 200;
gap = Math.floor(Math.random()*(maxGap-minGap 1) minGap);
myObstacles.push(new component(10, height, "green", x, 0));
myObstacles.push(new component(10, x - height - gap, "green", x, height gap));
}
uj5u.com熱心網友回復:
您在其上繪制的組件<canvas>不是 html 元素,因此您不能使用 CSS 設定它們的樣式。
你想要的是fillRect()用一個可以繪制圓角矩形的函式替換你的函式。
提供了一個這樣的函式來回答這個 stackoverflow 問題:如何使用 HTML Canvas 繪制圓角矩形?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/507589.html
標籤:javascript 帆布 游戏开发 边界半径
