访问类属性将返回" Undefined"

时间:2014-11-16 12:40:07

标签: javascript arrays class object

我尝试使用paper.js库创建一个简单的游戏,通过利用它的矢量绘图框架在所有显示器上实现清晰的结果。

基本的游戏逻辑是用户必须移动"蠕虫"每次球被击中时,球会在一定时间内朝球传播或消失。或者"收集"然后,通过蠕虫,球消失,并且在屏幕的其他地方产生新的球,从而允许获得更多的点。

当蠕虫撞到它时,我设法在每个球上射击原型方法,但是当它发生时我很难控制球的大小。

请参阅此处的工作示例http://goo.gl/uNcl5y

使用以下方法检查距离,并在此方法中使用" destruct" (或当球缩小到半径为零时)在其中调用:

Ball.prototype = {
    checkCollision: function(linepoint, ball) {

       var dist = this.point.getDistance(linepoint);

       if (dist <= this.radius) {
           this.destruct(ball);
       }
    },
    destruct: function(ball) {
        console.log(ball.bounds.width); //returns undefined
    }
}

这是使用Paper.js onFrame函数调用的(实质上是每隔30ms左右调用一次setInterval用于动画):

//set animations
function onFrame() {
    for (var i = 0; i < balls.length; i++) {
        balls[i].checkCollision(path.firstSegment.point, balls[i]);
    }   
}    

据我所知,传球可以传球[i]&#34;数组对象应该让我访问它的属性,因此允许我改变它们,确定

    destruct: function(ball) {
        console.log(ball);
    }

日志

{
  point: { x: 89, y: 628 },
  path: Path @4,
  radius: 30
}

但是使用(scale()是paper.js中的内置转换函数)

 ball.scale(0);

返回

 "Uncaught TypeError: undefined is not a function"

我无法绕过正在发生的事情,任何指针都会非常感激。

1 个答案:

答案 0 :(得分:0)

scale方法在路径对象上定义。在destruct函数中,您必须在路径对象上调用scale。

 destruct: function(ball) {
        ball.path.scale(0,0);
 }