Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对象原型在画布游戏中不起作用_Javascript_Canvas_Prototype Programming - Fatal编程技术网

Javascript 对象原型在画布游戏中不起作用

Javascript 对象原型在画布游戏中不起作用,javascript,canvas,prototype-programming,Javascript,Canvas,Prototype Programming,所以我在写一个愚蠢的小画布游戏,主要是一个小行星的副本。无论如何,我已经设置了按钮侦听器,以便当用户按下空格键时,调用播放器对象的fire()函数: eGi.prototype.keyDownListener = function(event) { switch(event.keyCode) { case 32: player.fire(); break; 在fire函数中,我的脚本检查进程是否已经运行,如果没有,则创建一

所以我在写一个愚蠢的小画布游戏,主要是一个小行星的副本。无论如何,我已经设置了按钮侦听器,以便当用户按下空格键时,调用播放器对象的
fire()
函数:

eGi.prototype.keyDownListener = function(event) {
    switch(event.keyCode) {
        case 32:
            player.fire();
            break;
在fire函数中,我的脚本检查进程是否已经运行,如果没有,则创建一个新的“bullet”对象,将其存储在临时变量中,并将其添加到绘图堆栈中

fire:(function() {
    if (this.notFiring) {
        var blankObject = new bullet(this.x,this.y,this.rot,"bullet");
        objects.push(blankObject);
        timer2 = setTimeout((function() {
            objects.pop();
        }),1000);
        this.notFiring = false;
    }}),
(顺便说一句,当用户释放空格键时,
this.notFiring
被设置回true。)

这是bullet对象构造函数及其必需的原型方法,
draw(context)

无论如何,当我运行游戏并按下空格键时,Chrome开发者控制台会给我一个错误,上面写着:

Uncaught TypeError: Object function (x,y,rot,name) {
    this.x = x;
    this.y = y;
    this.sx = 0;
    this.sy = 0;
    this.speed = 1;
    this.maxSpeed = 10;
    this.rot = rot;
    this.life = 1;
    this.sprite = b_sprite;
    this.name = name;
} has no method 'draw'
尽管我用原型制作了它。我做错了什么

编辑:

在将
var bullet=function
更改为
function bullet
并将
bullet.prototype.draw
更改为
bullet.draw
后,我仍然得到一个错误。这一次,它更神秘,说

Uncaught TypeError: type error
    bullet.draw
    (anonymous function)
    eGi.drawObjs
    eGi.cycle
完整的代码在我的网站上

另一编辑:

Chrome控制台说,第122行发生了这种类型的错误,这恰好是代码片段:

context.drawImage(this.sprite,this.x,this.y);

但是,我不确定那里怎么会有类型错误,精灵是图像,X和Y值不是未定义的,它们是数字。

在哪里调用绘图函数?我打赌你正在调用bullet.draw()而不是在实际的bullet实例上调用它

有点像

Cat.meow();


您确定在将此.sprite传递给
context.drawImage
时定义了它吗?尝试添加
console.log(this.sprite)
以首先检查它。
b_sprite
定义是否正确?是的,正在传递。我已经用
var b_sprite=new Image()的格式定义了我所有的精灵;b_sprite.src=“bullet.png”这基本上是正确的,我的“引擎”(eGi)执行draw函数的糟糕方式是使用我传递给它的“名称”,在本例中,它恰好是bullet,与类名相同。我已经和Objective-C合作太多了,子弹必须是全球性的,我的“引擎”才能识别它。
Cat.meow();
var mittens = new Cat();
mittens.meow();