我正在搞乱Javascript"类"我有一个有适当绘制方法的桨,但由于某种原因,我的moveBall功能搞砸了。谁能指出为什么?我得到一个错误,说moveBall()不是一个函数。
编辑:我添加了一些代码,我调用init()来启动它。
class Ball {
constructor(x, y, r, sAngle, rAngle) {
this.x = x;
this.y = y;
this.r = r;
this.sAngle = sAngle;
this.rAngle = rAngle;
this.speed = null;
}
drawBall() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.r, this.sAngle, this.rAngle);
ctx.fillStyle = "#FF0000";
ctx.fill();
}
moveBall() {
this.x += this.speed;
}
}
function init() {
var ball = new Ball(c.height / 2, c.width / 2, 10, 0, 2 * Math.PI);
var paddleLeft = new Paddle(0, 0, 20, 100);
ball.ballPhysics = 1.0;
draw(ball, paddleLeft);
main(ball);
}
window.main = function (ball) {
window.requestAnimationFrame(main);
ball.moveBall();
window.onload = function () {
document.addEventListener('keydown', function (event) {
if (event.keyCode === 65) {
}
}, false);
}
};
答案 0 :(得分:1)
确保在您尝试运行的代码范围内有一个ball实例,并尝试var moveball = function(){this.x += this.speed; };
查看它是否是编译问题,并确保您正在访问它{ {1}}
答案 1 :(得分:1)
您定义$thisDate = "2016-01-01 01:00:00 +01:00";
$seperated = explode('+', $thisDate);
$splitHours = explode(':',$seperated[1]);
$hoursToAdd = isset($splitHours[0]) ? (($splitHours[0]!='0' && $splitHours[0]!='00') ? $splitHours[0].'H' : '') : '';
$minutesToAdd = isset($splitHours[1]) ? (($splitHours[1]!='0' && $splitHours[1]!='00') ? $splitHours[1].'S' : '') : '';
$addtionString = ($splitHours[0]!='' || $splitHours[1]!='') ? 'PT'.$hoursToAdd.$minutesToAdd : '';
$date = new DateTime($seperated[0]);
if($addtionString != '') {
$date->add(new DateInterval($addtionString));
}
echo $date->format('Y-m-d H:i:s') . "\n";
接受参数main
。但是你永远不会打电话给ball
,所以你不能传递论据。
虽然main
会调用该函数,但它不会传递任何内容。
您似乎想要引用window.requestAnimationFrame(main);
中定义的ball
变量。在这种情况下,从函数定义中删除参数init
,这样它就不会影响该变量:
ball
您也不想在window.main = function() { ... };
内分配window.onload
,因为这不会一次又一次地发生。
答案 2 :(得分:0)
如果你像Ball.moveBall()一样使用它而不是它不正确,你必须首先实例化Ball类或使用静态方法,如
rng.Value = cl.Parameter
并调用
class A {
static f() {
}
}
否则请查看下面的代码段
A.f();