使用阶梯将绘制的形状移动到新的Y
Moving drawn shape to new Y with steps
我正在处理我的LD条目,我有点卡住了,所以时间至关重要。
我在香草JS工作,有一个网球/乒乓球风格的设置。有两个按钮,一个在画布的上半部分,另一个在下半部分。
我让它工作,这样当点击时,他们会在Y轴上移动"球"-或+但我想要这样,当点击时,"球"开始移动到另一边,它开始移动到另外一边。可以分别为0和canvas.height,但我不知道该怎么编码。
另一个功能是,我希望只有当"球"碰撞时,才能单击一侧。
CodePen:http://codepen.io/anon/pen/objWKN
var canvas, ctx, mouseX, mouseY, btn1, btn2, ball, ballX, ballY, ballSize, Score;
window.onload = function main() {
canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
canvas.width = width = 320;
canvas.height = height = 560;
mouseX = 0;
mouseY = 0;
btn1 = new Button(0, width, 0, height/2);
btn2 = new Button(0, width, height/2, height);
moveX = width/2;
moveY = height/2;
ballSize = 20;
ballX = moveX;
ballY = moveY;
document.body.appendChild(canvas);
init();
}
function init() {
document.addEventListener('click', mouseClicked, false);
update();
}
function update() {
ctx.clearRect(0, 0, 300, 300);
ctx.beginPath();
ctx.fillStyle="red";
ctx.rect(0, height/2, width, height/2);
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.fillStyle = "black";
ctx.arc( ballX, ballY , ballSize/2, 0, Math.PI * 2, true);
ctx.fill();
ctx.closePath();
}
// button object
// checks if within bounds of 'button'
function Button(xL, xR, yT, yB) {
this.xLeft = xL;
this.xRight = xR;
this.yTop = yT;
this.yBottom = yB;
}
Button.prototype.checkClicked = function() {
if ( this.xLeft <= mouseX && mouseX <= this.xRight && this.yTop <= mouseY && mouseY <= this.yBottom) return true;
};
// event functions
// get position of mouse if its clicked on the canvas
function mouseClicked(e) {
mouseX = e.pageX - canvas.offsetLeft;
mouseY = e.pageY - canvas.offsetTop;
//btn1 clicked = move up
for (i = ballY; i > height; i++); {
if (btn1.checkClicked()) {
ballY = ballY - 40;
}
};
//btn2 clicked = move up
for (i = ballY; ballY > height; i++); {
if (btn2.checkClicked()) {
ballY = ballY - 40; //========= what to put here? =========
}
};
};
setInterval(update, 10);
非常感谢您的帮助。
谢谢你抽出时间。
-Jordan
您应该请求AnimationFrame继续绘制您的画布,而不仅仅是使用球的位置作为变量来增加/减少,您还应该使用它的速度,以及按速度*时间的变化位置。
享受游戏编码的乐趣。
var canvas, ctx, mouseX, mouseY, btn1, btn2, ball, ballX, ballY, ballSize, Score, speedY = 0, end, start;
window.onload = function main() {
canvas = document.createElement("canvas");
ctx = canvas.getContext("2d");
canvas.width = width = 320;
canvas.height = height = 560;
mouseX = 0;
mouseY = 0;
btn1 = new Button(0, width, 0, height/2);
btn2 = new Button(0, width, height/2, height);
moveX = width/2;
moveY = height/2;
ballSize = 20;
ballX = moveX;
ballY = moveY;
document.body.appendChild(canvas);
init();
}
function init() {
document.addEventListener('click', mouseClicked, false);
start = new Date().getTime();
update();
}
function update() {
end = new Date().getTime();
ctx.clearRect(0, 0, 320, 560);
ctx.beginPath();
ctx.fillStyle="red";
ctx.rect(0, height/2, width, height/2);
ctx.fill();
ctx.closePath();
ballY += speedY * (end-start) / 1200
ctx.beginPath();
ctx.fillStyle = "black";
ctx.arc( ballX, ballY , ballSize/2, 0, Math.PI * 2, true);
ctx.fill();
ctx.closePath();
start = end;
requestAnimationFrame(update);
}
// button object
// checks if within bounds of 'button'
function Button(xL, xR, yT, yB) {
this.xLeft = xL;
this.xRight = xR;
this.yTop = yT;
this.yBottom = yB;
}
Button.prototype.checkClicked = function() {
if ( this.xLeft <= mouseX && mouseX <= this.xRight && this.yTop <= mouseY && mouseY <= this.yBottom) return true;
};
// event functions
// get position of mouse if its clicked on the canvas
function mouseClicked(e) {
mouseX = e.pageX - canvas.offsetLeft;
mouseY = e.pageY - canvas.offsetTop;
if (btn1.checkClicked()) {
speedY = 40;
}
if (btn2.checkClicked()) {
speedY = -40; //========= what to put here? =========
}
};
相关文章:
- 在 d3.js 中绘制滚动/移动平均线
- 如何摆脱角色移动时绘制的图像痕迹
- 在HTML5画布上绘制/移动/删除笔划
- 在HTML5画布中绘制鼠标移动的半透明线条
- 在画布上绘制移动
- 在鼠标移动时绘制一个矩形,并使用 kineticjs 在鼠标向上选择该矩形内的所有形状
- 如何重新绘制背景,使其看起来像我的“播放器”在html5-canvas中移动
- 使用拉斐尔用滚动条绘制不移动的东西
- 由谷歌地图绘制的矩形 绘图管理器不会触发鼠标移动事件
- 传单.js绘制控件不能在整个地图窗口区域上移动
- 高库存:移动渲染器元素和导航器/绘制趋势线
- Javascript画布绘制-大量移动时像素化
- 使用阶梯将绘制的形状移动到新的Y
- 可以'不要在移动浏览器上用手指绘制画布
- 在鼠标移动时绘制圆形后,保存并恢复画布矩形
- 如何移动绘制在圆周长上的X、Y坐标的起点
- 在画布上使用JavaScript绘制鼠标移动的透明图像
- 使用sketch.js为移动网页绘制画布会重置到这样
- 重新绘制谷歌地图以查看隐藏的移动标记
- 如何在HTML5画布上移动绘制的图像