DatKenwayGuy

Phaser in combination with arrays

Recommended Posts

Hello everyone,

I've been working on a game inspired by Heroes of Might and Magic and Final Fantasy-esque and I wanted to turn it into a browser game. Everything's been going well until I hit a bump in the road. I thought it would have been smart and easier to put all the sprites of the creatures in an array but doing so only let me use one sprite at a time so if the fight was between 4 Halberdiers and 4 Skeletons then they would on both sides only have 1 sprite. The stats for all the creatures are in a database and so far worked fine.

    var chosensprite =[];
    chosensprite[1]=game.add.sprite(-100 ,-100 , 'skeleton'); 
    chosensprite[2]=game.add.sprite(-100 ,-100 , 'halberdier'); 

This is what I used as an array. I tried to hardcode all the characters but doing so only created more of a clustermess which doesn't seem good to work with.

// The playerunit1 and its settings
    playerunit1 = game.add.sprite(32+32,    game.world.height/numcreaturesplayer*0+64, 'halberdier'); 
    playerunit1.kill(); 
    playerunit2 = game.add.sprite(32+32,    game.world.height/numcreaturesplayer*1+64, 'halberdier');   
    playerunit2.kill(); 
    playerunit3 = game.add.sprite(32+32,    game.world.height/numcreaturesplayer*2+64, 'halberdier');
    playerunit3.kill();    
    playerunit4 = game.add.sprite(32+32,    game.world.height/numcreaturesplayer*3+64, 'halberdier');   
    playerunit4.kill(); 

    enemyunit1 = game.add.sprite(game.world.width - 128-32, game.world.height/numcreaturesenemy*0+64, 'skeleton');
    enemyunit1.kill(); 
    enemyunit2 = game.add.sprite(game.world.width - 128-32, game.world.height/numcreaturesenemy*1+64, 'skeleton');
    enemyunit2.kill(); 
    enemyunit3 = game.add.sprite(game.world.width - 128-32, game.world.height/numcreaturesenemy*2+64, 'skeleton');
    enemyunit3.kill(); 
    enemyunit4 = game.add.sprite(game.world.width - 128-32, game.world.height/numcreaturesenemy*3+64, 'skeleton');
    enemyunit4.kill(); 
    selector = game.add.sprite(0,0, 'mselector');
    /*
    playerunit1bolt = game.add.image(playerunit1.x+800, playerunit1.y+16, 'star');
    enemyunit1bolt = game.add.image(enemyunit1.x+800, enemyunit1.y+86, 'star');
    */

     for (var i=0;creaturestats[i];) 
     {

        playerunit1.unittype= 2;
        
        
        if (playerunit1.unittype == creaturestats[i]['unittype'])
        {
            if (playerunit1.unittype = 2){
                playerunit1 = game.add.sprite(32+32,    game.world.height/numcreaturesplayer*0+64, 'halberdier');
            }

            //playerunit1 = chosensprite[playerunit1.unittype];playerunit1.x=64;playerunit1.y=64;
            playerunit1.damage=creaturestats[i]['damage'];
            playerunit1.healthblock=creaturestats[i]['healthblock'];
            playerunit1.units=creaturestats[i]['unitamount'];
            playerunit1.defense=creaturestats[i]['defense'];
            playerunit1.damagetype=creaturestats[i]['damagetype'];
            playerunit1.attacktype=creaturestats[i]['attacktype'];
        }
        playerunit2.unittype= 1;

        if (playerunit2.unittype == creaturestats[i]['unittype'])
        {
            if (playerunit2.unittype = 1){
                game.add.sprite(32+32,    game.world.height/numcreaturesplayer*1+64, 'skeleton');
            }
            //playerunit2 = chosensprite[playerunit2.unittype];playerunit2.x=128;playerunit2.y=128;
            playerunit2.damage=creaturestats[i]['damage'];
            playerunit2.healthblock=creaturestats[i]['healthblock'];
            playerunit2.units=creaturestats[i]['unitamount'];
            playerunit2.defense=creaturestats[i]['defense'];
            playerunit2.damagetype=creaturestats[i]['damagetype'];
            playerunit2.attacktype=creaturestats[i]['attacktype'];
        }
        playerunit3.unittype= 1;

        if (playerunit3.unittype == creaturestats[i]['unittype'])
        {
            if (playerunit3.unittype = 1){
                game.add.sprite(32+32,    game.world.height/numcreaturesplayer*2+64, 'skeleton');
            }
            //playerunit3 = chosensprite[playerunit3.unittype];playerunit3.x=500;playerunit3.y=200;
            playerunit3.damage=creaturestats[i]['damage'];
            playerunit3.healthblock=creaturestats[i]['healthblock'];
            playerunit3.units=creaturestats[i]['unitamount'];
            playerunit3.defense=creaturestats[i]['defense'];
            playerunit3.damagetype=creaturestats[i]['damagetype'];
            playerunit3.attacktype=creaturestats[i]['attacktype'];
        }
        playerunit4.unittype= 2;

        if (playerunit4.unittype == creaturestats[i]['unittype'])
        {
            if (playerunit4.unittype = 2){
                game.add.sprite(32+32,    game.world.height/numcreaturesplayer*3+64, 'halberdier');
            }
            //playerunit4 = chosensprite[playerunit4.unittype];playerunit4.x=300;playerunit4.y=20;
            playerunit4.damage=creaturestats[i]['damage'];
            playerunit4.healthblock=creaturestats[i]['healthblock'];
            playerunit4.units=creaturestats[i]['unitamount'];
            playerunit4.defense=creaturestats[i]['defense'];
            playerunit4.damagetype=creaturestats[i]['damagetype'];
            playerunit4.attacktype=creaturestats[i]['attacktype'];
        }
        enemyunit1.unittype= 2;

        if (enemyunit1.unittype == creaturestats[i]['unittype'])
        {
             if (enemyunit1.unittype = 2){
                game.add.sprite(game.world.width - 128-32, game.world.height/numcreaturesenemy*0+64, 'halberdier');
            }
            enemyunit1.damage=creaturestats[i]['damage'];
            enemyunit1.healthblock=creaturestats[i]['healthblock'];
            enemyunit1.units=creaturestats[i]['unitamount'];
            enemyunit1.defense=creaturestats[i]['defense'];
            enemyunit1.damagetype=creaturestats[i]['damagetype'];
            enemyunit1.attacktype=creaturestats[i]['attacktype'];
        }
        enemyunit2.unittype= 2;

        if (enemyunit2.unittype == creaturestats[i]['unittype'])
        {
            if (enemyunit2.unittype = 2){
                game.add.sprite(game.world.width - 128-32, game.world.height/numcreaturesenemy*1+64, 'halberdier');
            }
            enemyunit2.damage=creaturestats[i]['damage'];
            enemyunit2.healthblock=creaturestats[i]['healthblock'];
            enemyunit2.units=creaturestats[i]['unitamount'];
            enemyunit2.defense=creaturestats[i]['defense'];
            enemyunit2.damagetype=creaturestats[i]['damagetype'];
            enemyunit2.attacktype=creaturestats[i]['attacktype'];
        }
        enemyunit3.unittype= 2;

        if (enemyunit3.unittype == creaturestats[i]['unittype'])
        {
            if (enemyunit3.unittype = 2){
                game.add.sprite(game.world.width - 128-32, game.world.height/numcreaturesenemy*2+64, 'halberdier');
            }
            enemyunit3.damage=creaturestats[i]['damage'];
            enemyunit3.healthblock=creaturestats[i]['healthblock'];
            enemyunit3.units=creaturestats[i]['unitamount'];
            enemyunit3.defense=creaturestats[i]['defense'];
            enemyunit3.damagetype=creaturestats[i]['damagetype'];
            enemyunit3.attacktype=creaturestats[i]['attacktype'];
        }
        enemyunit4.unittype= 1;
        if (enemyunit4.unittype == creaturestats[i]['unittype'])
        {
            if (enemyunit4.unittype = 2){
                game.add.sprite(game.world.width - 128-32, game.world.height/numcreaturesenemy*3+64, 'skeleton');
            }
            enemyunit4.damage=creaturestats[i]['damage'];
            enemyunit4.healthblock=creaturestats[i]['healthblock'];
            enemyunit4.units=creaturestats[i]['unitamount'];
            enemyunit4.defense=creaturestats[i]['defense'];
            enemyunit4.damagetype=creaturestats[i]['damagetype'];
            enemyunit4.attacktype=creaturestats[i]['attacktype'];
        }

        i++;
    }

So my question is if there is anyone well known with Phaser and could help me work out the solution with an array, another solution or if they're is no other choice but to hardcode it or work with HTML5 instead of Phaser.

Thank you in advance! :)

index.php

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.