Thunderfist

I can't find the bugs in my code

Recommended Posts

Here's my game.js

var RPG = RPG || {};

RPG.GameState = {

    init: function (currentLevel) {
    //keep track of the current level
        this.currentLevel = currentLevel || currentLevel === 'testroom1';

    //constants
        this.PLAYER_SPEED = 90;
    
    //no gravity in a top-down game
        this.game.physics.arcade.gravity.y = 0;

    //keyboard cursors
        this.cursors = this.game.input.keyboard.createCursorKeys();
    },
    create: function () {

        this.game.onscreenControls = this.game.plugins.add(Phaser.Plugin.OnscreenControls);

        this.loadLevel();
    },
    update: function () {

    //player can't walk through walls
        this.game.physics.arcade.collide(this.player, this.collisionLayer);

    //items collection
        this.game.physics.arcade.overlap(this.player, this.items, this.collect, null, this);

    //stop each time
        this.player.body.velocity.x = 0;
        this.player.body.velocity.y = 0;

        if (this.cursors.left.isDown || this.player.btnsPressed.left || this.player.btnsPressed.upleft  || this.player.btnsPressed.downleft) {
            this.player.body.velocity.x = -this.PLAYER_SPEED;
            this.player.scale.setTo(1, 1);
        }
        if (this.cursors.right.isDown || this.player.btnsPressed.right || this.player.btnsPressed.upright  || this.player.btnsPressed.downright) {
            this.player.body.velocity.x = this.PLAYER_SPEED;
            this.player.scale.setTo(-1, 1);
        }
        if (this.cursors.up.isDown || this.player.btnsPressed.up || this.player.btnsPressed.upright  || this.player.btnsPressed.upleft) {
            this.player.body.velocity.y = -this.PLAYER_SPEED;
        }
        if (this.cursors.down.isDown || this.player.btnsPressed.down || this.player.btnsPressed.downright  || this.player.btnsPressed.downleft) {
            this.player.body.velocity.y = this.PLAYER_SPEED;
        }

    //stop all movement if nothing is being pressed
        if (this.game.input.activePointer.isUp) {
            this.game.onscreenControls.stopMovement();
        }

    //play walking animation'
        if (this.player.body.velocity.x !== 0 || this.player.body.velocity.y !== 0) {
            this.player.play('walk');
        }
            else {
                this.player.animations.stop();
                this.player.frame = 0;
            }

    },
    loadLevel: function () {
    //create a tilemap object
        this.map = this.add.tilemap(this.currentLevel);
    
    //join the tile images to the json data
        this.map.addTilesetImage('terrains', 'tilesheet');
    
    //create tile layers
        this.backgroundLayer = this.map.createLayer('backgroundLayer');
        this.collisionLayer = this.map.createLayer('collisionLayer');
    
    //send background to the back
        this.game.world.sendToBack(this.backgroundLayer);
    
    //collision layer should be collisionLayer
        this.map.setCollisionBetween(1, 16, true, 'collisionLayer');
    
    //resize the world to fit the layer
        this.collisionLayer.resizeWorld();

    //create player
        var playerData = {
      //list of items
                items: [],

      //player stats
                health: 30,
                attack: 10,
                defense: 10,
                gold: 0,

      //quest
                quests: [
                    {
                        name: 'find the map in Schnyders room  ',
                        code: 'robschneider',
                        isCompleted: false
                    },
                    {
                        name: 'aquire the Sword of Destiny',
                        code: 'destiny-sword',
                        isCompleted: false
                    },
                    {
                        name: 'aquire the Staff of Gods',
                        code: 'godsstaff',
                        isCompleted: false
                    }
                ]
            };

        this.player = new RPG.Player(this, 100, 100, playerData);

    //add player to the world
        this.add.existing(this.player);

    //group of items
        this.items = this.add.group();
        this.loadItems();

    //enemies
        this.enemies = this.add.group();
    //this.loadEnemies();

    //this.enemy = new RPG.Enemy(this, 200, 60, 'monster', {attack: 10, health: 20, defense: 5});
    //this.enemies.add(this.enemy);

    //follow player with the camera
        this.game.camera.follow(this.player);

        this.initGUI();
    },
    gameOver: function () {
    this.game.state.start('Game', true, false, this.currentLevel);
  },
  initGUI: function() {
    //onscreen controls setup
    this.game.onscreenControls.setup(this.player, {
      left: true,
      right: true,
      up: true,
      down: true,
      upleft: true,
      downleft: true,
      upright: true,
      downright: true,
      action: false
    });

    this.showPlayerIcons();
  },
  collect: function(player, item) {
    this.player.collectItem(item);
  },
  showPlayerIcons: function() {
    //gold icon
        this.goldIcon = this.add.sprite(10, 10, 'coin');
        this.goldIcon.fixedToCamera = true;

        var style = {font: '14px Arial', fill: '#fff'};
        this.goldLabel = this.add.text(30, 10, '0', style);
        this.goldLabel.fixedToCamera = true;

    //attack icon
        this.attackIcon = this.add.sprite(70, 10, 'sword');
        this.attackIcon.fixedToCamera = true;

        var style = {font: '14px Arial', fill: '#fff'};
        this.attackLabel = this.add.text(90, 10, '0', style);
        this.attackLabel.fixedToCamera = true;

    //defense icon
        this.defenseIcon = this.add.sprite(130, 10, 'shield');
        this.defenseIcon.fixedToCamera = true;

        var style = {font: '14px Arial', fill: '#fff'};
        this.defenseLabel = this.add.text(150, 10, '0', style);
        this.defenseLabel.fixedToCamera = true;

        this.refreshStats();
  },
  refreshStats: function() {
        this.goldLabel.text = this.player.data.gold;
        this.attackLabel.text = this.player.data.attack;
        this.defenseLabel.text = this.player.data.defense;
    },
    findObjectsByType: function(targetType, tilemap, layer){
        var result = [];
    
        tilemap.objects[layer].forEach(function (element) {
            if (element.properties.type === targetType) {
                element.y -= tilemap.tileHeight / 2;
                element.x += tilemap.tileHeight / 2;
                result.push(element);
            }
        }, this);
    
        return result;
    },
    loadItems: function () {
        var elementsArr = this.findObjectsByType('item', this.map, 'objectsLayer');
        var elementObj;

        elementsArr.forEach(function (element) {
            elementObj = new RPG.Item(this, element.x, element.y, element.properties.asset, element.properties);
            this.items.add(elementObj);
        }, this);
    }

};

Thing is, I don't know if this is the one filled with bugs.

Share this post


Link to post
Share on other sites

Okay, I still can't find the bug... Maybe it's in my player.js?

 

var RPG = RPG || {};

RPG.Player = function (state, x, y, data) {
    Phaser.Sprite.call(this, state.game, x, y, 'player');

    this.state = state;
    this.game = state.game;
    this.data = data;
    this.anchor.setTo(0.5);

  //walking animation
    this.animations.add('walk', [0, 1, 0], 6, false);


  //enable physics
    this.game.physics.arcade.enable(this);
};

RPG.Player.prototype = Object.create(Phaser.Sprite.prototype);
RPG.Player.prototype.constructor = RPG.Player;

RPG.Player.prototype.collectItem = function (item) {
  //two types of items, quest items and consumables
    if (item.data.isQuest) {
        this.data.items.push(item);

    //check quest completion
        this.checkQuestCompletion(item);
    }
        
        else {
    //consumable items

    //add properties
            this.data.health += item.data.health || item.data.health === 0;
            this.data.attack += item.data.attack || item.data.attack === 0;
            this.data.defense += item.data.defense || item.data.defense === 0;
            this.data.gold += item.data.gold || item.data.gold === 0;

    //refresh stats
            this.state.refreshStats();
        }
    item.kill();
};

RPG.Player.prototype.checkQuestCompletion = function (item) {
    var i = 0;
    var len = this.data.quests.length;

    while (i < len) {
        if (this.data.quests[i].code === item.data.questCode) {
            this.data.quests[i].isCompleted = true;
            console.log(this.data.quests[i].name + ' has been completed');
            break;
        }
    }
};

 

I hope I can find the bugs soon... otherwise I can't make a simple Role-Playing Game.

Share this post


Link to post
Share on other sites

I just checked the console, and it says GainNode.gain.value setting is deprecated and can't be used after last month. Is there some sort of work-around?

 

UPDATE: All of the errors in the console are pointing at my phaser.js. does the Phaser library get updated?

Share this post


Link to post
Share on other sites

I found that my enemyPrefab.js has a bug: According to my console, it says Cannot set property 'prototype' of undefined at enemyPrefab.js line 20.

Is there a workaround for that? It's the only thing that's showing up in the console for bugs.

 

EDIT

 

I took a look at someone's tutorial and found what I did wrong.

The code still isn't working right yet, but I'm getting there.

I found this in the console:

Phaser.StateManager - No state found with the key: game

Share this post


Link to post
Share on other sites

I started re-coding the .js files, re-using certain files. I found this in my console after comparing the code between my new main.js and the old one:

Uncaught TypeError: RPG.getGameLandscapeDimensions is not a function

EDIT: Just realized I put the main.js before my scaler.js file. my bad.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.