2016-06-25 20 views
0

私が書いているコードでは、Phaser.Spriteを拡張するGameObjectを拡張したEntityを拡張するSkeletonというクラスを作成しました。私は選手クラスも作った。しかし、プレーヤーのクラスコードは正常に動作します。 Uncaught TypeError: this.onTextureUpdate is not a function(エラーはフェイザーファイルにあります)しかし、プレーヤーコードはスケルトンとほぼ同じです(唯一の違いは、プレイヤーがエンティティからエンティティを継承し、敵ではありません)Phaser JS - Uncaught TypeError:this.onTextureUpdateは関数ではありません

ここで

は、プレーヤーのコードです:

var Player = function(game, x, y, stats) { 
    Entity.call(this, game, x, y, 'player', 1, 24, 14, 26, stats, [ 
     [0, 1], 
     [2, 3], 
     [4, 5], 
     [6, 7] 
    ], [ 
     [8, 9, 10, 11], 
     [12, 13, 14, 15], 
     [16, 17, 18, 19], 
     [20, 21, 22, 23] 
    ], 10, 10); 

    this.width = 30; 
    this.height = 40; 
}; 

Player.prototype = Object.create(Entity.prototype); 
Player.prototype.constructor = Player; 

Player.prototype.update = function() { 
    game.physics.arcade.collide(this, objects, null, function(obj1, obj2) { 
     return obj2 instanceof Wall; 
    }); 

    this.moving = false; 

    if (cursors.up.isDown) { 
     this.direction = Entity.prototype.directions.BACKWARD; 
     this.moving = true; 

    } else if (cursors.down.isDown) { 
     this.direction = Entity.prototype.directions.FORWARDS; 
     this.moving = true; 
    } else if (cursors.left.isDown) { 
     this.direction = Entity.prototype.directions.LEFT; 
     this.moving = true; 
    } else if (cursors.right.isDown) { 
     this.direction = Entity.prototype.directions.RIGHT; 
     this.moving = true; 
    } 

    this.requiredFunctions(); 
}; 

スケルトン:

var Skeleton = function(game, x, y, stats) { 
    Enemy.call(this, game, x, y, 'skeleton', 1, 23, 13, 5, stats, [ 
     [0, 1], 
     [2, 3], 
     [4, 5], 
     [6, 7] 
    ], [ 
     [8, 9, 10, 11], 
     [12, 13, 14, 15], 
     [16, 17, 18, 19], 
     [20, 21, 22, 23] 
    ], 5, 5, "LEFT_TO_RIGHT_MOVEMENT"); 
}; 

Skeleton.prototype = Object.create(Enemy.prototype); 
Skeleton.prototype.constructor = Skeleton; 

ゲームオブジェクト:

var GameObject = function(game, x, y, texture, boxX, boxY, boxW, boxH) { 
    Phaser.Sprite.call(this, game, x, y, texture); 

    game.physics.arcade.enable(this); 

    objects.push(this); 

    game.math.snapTo(this.x, 32); 
    game.math.snapTo(this.y, 32); 

    this.body.setSize(boxW, boxH, boxX, boxY); 
}; 

GameObject.prototype = Object.create(Phaser.Sprite.prototype); 
GameObject.prototype.constructor = GameObject; 

GameObject.prototype.update = function() { 
    if(debugMode) { 
     this.game.debug.body(this); 
    } 
} 

エンティティ:

var Entity = function(game, x, y, texture, boxX, boxY, boxW, boxH, stats, idleFrames, movingFrames, idleFrameRate, movingFrameRate) { 
    GameObject.call(this, game, x, y, texture, boxX, boxY, boxW, boxH); 

    this.stats = stats; 

    this.idleFrameRate = idleFrameRate; 
    this.movingFrameRate = movingFrameRate; 

    this.animations.add("idle forward", idleFrames[0], this.idleFrameRate, true); 
    this.animations.add("idle right", idleFrames[1], this.idleFrameRate, true); 
    this.animations.add("idle left", idleFrames[2], this.idleFrameRate, true); 
    this.animations.add("idle backward", idleFrames[3], this.idleFrameRate, true); 

    this.animations.add("moving forward", movingFrames[0], this.movingFrameRate, true); 
    this.animations.add("moving right", movingFrames[1], this.movingFrameRate, true); 
    this.animations.add("moving left", movingFrames[2], this.movingFrameRate, true); 
    this.animations.add("moving backward", movingFrames[3], this.movingFrameRate, true); 

    this.animations.play("idle forward"); 

    this.moving = false; 

    //0 = forward 
    //1 = right 
    //2 = left 
    //3 = back 
    this.direction = 0; 

    this.effects = []; 
}; 

Entity.prototype = Object.create(GameObject.prototype); 
Entity.prototype.constructor = Entity; 

Entity.prototype.move = function() { 
    if(this.moving) { 
     switch(this.direction) { 
      case Entity.prototype.directions.FORWARDS: 
       this.body.velocity.y += this.stats.speed; 
       this.direction = 0; 
       this.moving = true; 
       break; 

      case Entity.prototype.directions.BACKWARDS: 
       this.body.velocity.y -= this.stats.speed; 
       this.direction = 3; 
       this.moving = true; 
       break; 

      case Entity.prototype.directions.RIGHT: 
       this.body.velocity.x += this.stats.speed; 
       this.direction = 1; 
       this.moving = true; 
       break; 

      case Entity.prototype.directions.LEFT: 
       this.body.velocity.x -= this.stats.speed; 
       this.direction = 2; 
       this.moving = true; 
       break; 
     } 
    } 
}; 

Entity.prototype.requiredFunctions = function() { 
    this.physics(); 
    this.statusEffects(); 
    this.move(); 
    this.animate(); 

    if(debugMode) { 
     this.game.debug.spriteBounds(this); 
    } 
}; 

Entity.prototype.statusEffects = function() { 
    this.stats.resetToDef(); 

    for (i in this.effects) { 
     if (this.effects[i].dead) { 
      this.effects.splice(i, 1); 
     } 
    } 
}; 

Entity.prototype.addEffect = function(effect) { 
    var name = effect.name.split(" ")[0]; 
    var p = []; 

    for (i in this.effects) { 
     if (i.name.split(" ")[0] == name) { 
      p.push(i); 
     } 
    } 

    if (p.length === 0) { 
     this.effects.push(effect); 
    } else { 
     var p1 = []; 

     var potency = effect.potency; 

     for (i in p) { 
      if (i.potency > potency) { 
       return; 
      } 

      if (i.potency === potency) { 
       p1.push(i); 
      } 
     } 

     if (p1.length === 0) { 
      this.effects.push(effect); 
     } else { 
      var p2 = []; 

      var time = effect.effectTime; 

      for (i in p1) { 
       if (i.effectTime >= time) { 
        return; 
       } 
      } 

      this.effects.push(effect); 
     } 
    } 
}; 

Entity.prototype.physics = function() { 
    this.body.velocity.y = 0; 
    this.body.velocity.x = 0; 
}; 

Entity.prototype.animate = function() { 
    this.smoothed = false; 

    if (!this.moving) { 
     switch (this.direction) { 
      case 0: 
       this.animations.play('idle forward'); 
       break; 
      case 1: 
       this.animations.play('idle right'); 
       break; 
      case 2: 
       this.animations.play('idle left'); 
       break; 
      case 3: 
       this.animations.play('idle backward'); 
       break; 
     } 
    } else { 
     switch (this.direction) { 
      case 0: 
       this.animations.play('moving forward'); 
       break; 
      case 1: 
       this.animations.play('moving right'); 
       break; 
      case 2: 
       this.animations.play('moving left'); 
       break; 
      case 3: 
       this.animations.play('moving backward'); 
       break; 
     } 
    } 
}; 

Entity.prototype.directions = { 
    FORWARD: 0, 
    FORWARDS: 0, 
    RIGHT: 1, 
    LEFT: 2, 
    BACKWARD: 3, 
    BACKWARDS: 3 
}; 

Entity.prototype.update = function() { 
    this.requiredFunctions(); 
}; 

が敵:あなたはもう、コードの必要がある場合

var Enemy = function(game, x, y, texture, boxX, boxY, boxW, boxH, stats, idleFrames, movingFrames, idleFrameRate, movingFrameRate, movementType) { 
    Entity.call(game, x, y, texture, boxX, boxY, boxW, boxH, stats, idleFrames, movingFrames, idleFrameRate, movingFrameRate); 

    this.movementFunc = undefined; 

    switch(movementType) { 
     case "LEFT_TO_RIGHT_MOVEMENT": 
      this.movementFunc = this.LEFT_TO_RIGHT_MOVEMENT; 
      break; 
     case "UP_TO_DOWN_MOVEMENT": 
      this.movementFunc = this.UP_TO_DOWN_MOVEMENT; 
      break; 
     case "STATIC_MOVEMENT": 
      this.movementFunc = this.STATIC_MOVEMENT; 
      break; 
     default: 
      throw "ERROR: Invalid movement type specified for instance of " + this.constructor; 
    } 
}; 

Enemy.prototype = Object.create(Entity.prototype); 
Enemy.prototype.constructor = Enemy; 

Enemy.prototype.move = function() { 
    Enemy.move.apply(this); 
    movementFunc(); 
}; 

Enemy.prototype.STATIC_MOVEMENT = function() { 

}; 

Enemy.prototype.LEFT_TO_RIGHT_MOVEMENT = function() { 
    if(this.direction === Entity.directions.FORWARDS || this.direction === Entity.directions.BACKWARDS) { 
     this.direction = Entity.directions.RIGHT; 
    } 

    game.phyics.arcade.collide(this, walls, function(obj1, obj2) { 
     this.direction = this.direction === Entity.directions.RIGHT ? Entity.directions.LEFT : Entity.directions.RIGHT; 
    }); 
}; 

Enemy.prototype.UP_TO_DOWN_MOVEMENT = function() { 
    if(this.direction === Entity.directions.RIGHT || this.direction === Entity.directions.LEFT) { 
     this.direction = Entity.directions.FORWARDS; 
    } 

    game.phyics.arcade.collide(this, walls, function(obj1, obj2) { 
     this.direction = this.direction === Entity.directions.FORWARDS ? Entity.directions.DOWNWARDS : Entity.directions.FORWARDS; 
    }); 
}; 

言ってください。

EDIT:私はEnemy.call()の開始時にこれを省いていることに気付きましたが、それを行った後もエラーは発生します。

答えて

0

私はスケルトンのためにした敵のために同じことをする必要があった! (編集を参照してください)申し訳ありません!

関連する問題