[英]How to use a function to change the property of another function?
I'm quite new to JS and have gone through courses online but, very frustratingly, I just seem to have such a hard time on my own so I'm sorry if this question has an obvious answer. 我是JS的新手,已经在线上过课程,但非常令人沮丧的是,我似乎一个人很难过,所以很抱歉这个问题有明显的答案。 Basically, this program bounces a colored ball around within a box. 基本上,该程序会在盒子内反弹一个彩色的球。 I want that color to change every time it hits a wall. 我希望这种颜色每次碰到墙时都会改变。 I figured out a way to do so by putting all information under one function but the tutorial I'm using is saying (for tidy code purposes) that 2 functions will be better and so I really just want to understand how to do what I want to do when info is available in different functions since I know I will have to do that in the future. 我想出了一种通过将所有信息放在一个函数下的方法,但是我正在使用的教程说(出于整洁的代码目的)两个函数会更好,所以我真的只想了解如何做自己想做的事情当信息可以在不同的功能中使用时应该执行此操作,因为我知道将来我必须这样做。 I will comment important code lines. 我将注释重要的代码行。 Thank you so much to anyone who can help. 非常感谢任何可以提供帮助的人。
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var x = canvas.width/2;
var y = canvas.height-30;
var dx = 4;
var dy = -4;
var ballRadius = 30;
function drawBall() { \\draws the ball
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI*2);
ctx.fillStyle = "#ff0000";
ctx.fill();
ctx.closePath();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
x += dx;
y += dy;
if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) { \\says when to bounce
dx = -dx;
drawBall.ctx.fillStyle = "#ff0000"; \\this line and next line are lines I wrote
drawBall.ctx.fill(); \\that are obviously incorrect (same goes for
} \\ if statement below). What am I doing wrong?
if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) {
dy = -dy;
drawBall.ctx.fillStyle = "#0095DD";
drawBall.ctx.fill();
}
}
setInterval(draw, 10);
what you can do is pass parameters that will alter the behavior of the function. 您可以做的是传递参数,这些参数将改变函数的行为。
in this case you will be passing the color you want. 在这种情况下,您将传递所需的颜色。
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var x = canvas.width/2;
var y = canvas.height-30;
var dx = 4;
var dy = -4;
var ballRadius = 30;
function drawBall(color) { // draws the ball
ctx.beginPath();
ctx.arc(x, y, ballRadius, 0, Math.PI*2);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawBall();
x += dx;
y += dy;
if(x + dx > canvas.width-ballRadius || x + dx < ballRadius) { // says when to bounce
dx = -dx;
drawBall("#ff0000");
}
if(y + dy > canvas.height-ballRadius || y + dy < ballRadius) {
dy = -dy;
drawBall("#0095DD");
}
}
It seems that you mix some concepts of JavaScript. 似乎您混合了一些JavaScript概念。 So for reasons of readability and design, I would create a 'class' for the ball. 因此,出于可读性和设计的原因,我将为球创建一个“类”。 Something like this: 像这样:
function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
}
You can create an instance of your ball with this: 您可以使用以下方法创建球的实例:
var ball = new Ball(x, y, radius, color);
and access the properties in Java-style: 并以Java样式访问属性:
ball.color = "#0095DD";
You can also add some methods to your ball: 您还可以向球添加一些方法:
function Ball(x, y, radius, color) {
this.x = x;
this.y = y;
this.radius = radius;
this.color = color;
this.draw = function(ctx) {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
}
You can extend your code with this class and code. 您可以使用此类和代码扩展代码。 I think, you get it. 我想,您明白了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.