我想用paper.js做一個遊戲,但有些東西不能正常工作,所以我簡化了一些東西,而且不再工作。來自純javascript的Paper.js
我試圖用paper.js製作已經制作好的命名空間paperscript
,但它太難調試,所以我試圖使用javascript directly。
即使使用超級簡單的代碼在瀏覽器周圍移動球,它也不起作用。
這裏是我的代碼:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Bouncing ball</title>
<link rel="stylesheet" href="../css/style.css">
<script type="text/javascript" src="../../lib/paper.js"></script>
<script type="text/javascript">
var ball;
function game(_view) {
console.log("game!");
ball = new Ball();
this.view = _view;
console.log(ball);
}
paper.install(window);
window.onload = function() {
var canvas = document.getElementById('canvas');
paper.setup(canvas);
view.onFrame = onFrame;
game(view);
}
var Ball = Base.extend({
initialize: function() {
this.position = new Point(100, 100);
this.velocity = new Point(3, 0);
this.path = new Path.Circle(location, 25);
this.path.strokeColor = 'black';
this.path.fillColor = 'black';
},
iterate: function() {
this.position += this.velocity;
this.path.position = this.position;
return this;
}
});
function onFrame(event) {
ball.iterate();
//console.log(ball);
};
</script>
</head>
<body>
<canvas id="canvas" resize></canvas>
</body>
</html>
我保持上收到此錯誤:
Uncaught TypeError: Cannot call method 'iterate' of undefined
???是的,球是球的一個實例..變量球(小寫)是腳本的第一行.. – nkint 2013-02-19 23:09:56
更新了一點點調查。 – Dennis 2013-02-19 23:39:46
好吧。感謝您審查答案。現在我不明白的是:如果我評論'ball.iterate();'但是取消註釋'console.log(ball);'在你的jfiddle中(和我在本地的chrome中的例子相同) '__proto__'裏面有'iterate'作爲函數。爲什麼?爲什麼它沒有正確調用? – nkint 2013-02-20 00:22:13