Page 1 of 1

[SOLVED] Stock component - addToStock throwing an error

Posted: 27 August 2014, 21:04
by Woodruff
Hi!
I'm a newbie in BGA studio. I try to develop a card game and I am stuck just at the beginning...
I'd like to add a hand for each player using the stock component. Creating it works wells but the app crashes when I try addToStock, throwing this error in Firebug -> network :

Code: Select all

During pageload _799(...) is undefined .cache["dojo/string"]/</_797.substitute/<@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/js/modules/layer/ly_studio.js:15:308670 .cache["dojo/string"]/</_797.substitute@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/js/modules/layer/ly_studio.js:15:308479 .cache["ebg/stock"]/</<.updateDisplay@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/js/modules/layer/ly_studio.js:15:606815 .cache["ebg/stock"]/</<.addToStockWithId@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/js/modules/layer/ly_studio.js:15:604061 .cache["ebg/stock"]/</<.addToStock@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/js/modules/layer/ly_studio.js:15:603572 .addStock@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/games/innovation/999999-9999//innovation.js?1409167468175:199:4 .setup@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/games/innovation/999999-9999//innovation.js?1409167468175:68:5 .cache["ebg/core/gamegui"]/</<.completesetup@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/js/modules/layer/ly_studio.js:15:182357 @http://en.1.studio.boardgamearena.com/innovation?table=17098:802:2594 _c6@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/js/dojoroot/dojo/dojo.js:15:11344 _36@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/js/dojoroot/dojo/dojo.js:15:12971 _7b/<@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/js/dojoroot/dojo/dojo.js:15:13217 _37@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/js/dojoroot/dojo/dojo.js:15:13061 _7b@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/js/dojoroot/dojo/dojo.js:15:13142 _32/_ee@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/js/dojoroot/dojo/dojo.js:15:14774 req.injectUrl/_108@http://1.studio.boardgamearena.com:8081/data/themereleases/140706-1531/js/dojoroot/dojo/dojo.js:15:17584
Here's the code I typed :
In .tpl :

Code: Select all

<div id="test"></div>
In .js

Code: Select all

constructor: function(){
            console.log('innovation constructor');
              
            // Here, you can init the global variables of your user interface
	    this.card_width = 196;
            this.card_height = 140;
            this.colors = new Array('blue','red','green','yellow','purple');
        },
        
        /*
            setup
        */
        
        setup: function( gamedatas )
        {
            console.log( "Starting game setup" );
            
	    this.pile = this.addStock($('test'), "cards.jpg");

            // Setup game notifications to handle (see "setupNotifications" method below)
            this.setupNotifications();

            console.log( "Ending game setup" );
        },

       ...

	addStock: function(container, source_img)
	{
		stock = new ebg.stock();
		stock.create(this, container, this.cardwidth, this.cardheight);
			
		stock.image_items_per_row = 15;
		var id = 0;
		for(var i=1; i<11; i++)
		{
			if(i==1){var max = 15;} else{var max = 10;}
			for(var j=0; j<max; j++)
			{
				id++;
				stock.addItemType(id, id, g_gamethemeurl+'img/'+source_img, id);
			}
		}
		alert(stock.count());
		stock.addToStock(1);
		return stock;
	},
CSS sprite : my image contains 15 cards on the first row, and 10 cards on the others


When I comment "stock.addToStock(1);", the app does not crash.

Any idea what I did wrong ?

Tcheby

Re: Stock component - addToStock throwing an error

Posted: 09 September 2014, 18:18
by sourisdudesert
Hi,

You should use a member variable for your stock item (ex: this.stock instead of stock).

[SOLVED] Stock component - addToStock throwing an error

Posted: 09 September 2014, 18:53
by Woodruff
sourisdudesert wrote:Hi,

You should use a member variable for your stock item (ex: this.stock instead of stock).
Hello sourisdudesert,
Thanks a lot for your answer.
I did not use a member variable because I return the stock as the result of addStock, and use it to affect the member variable this.pile.

Following your advice, I created the stock directly in the setup function, as a member variable, without using any utility function :

Code: Select all

        setup: function( gamedatas )
        {
            console.log( "Starting game setup" );
            
            // Setting up player boards
            // TODO: Set up your game interface here, according to "gamedatas"
            
			
			this.pile = new ebg.stock();
			this.pile.create(this, $('test'), this.cardwidth, this.cardheight);
			
			this.pile.image_items_per_row = 15;
			var id = 0;
			for(var age=1; age<11; age++)
			{
				for(var i=0; i<15; i++)
				{
					id++;
					this.pile.addItemType(id, id, g_gamethemeurl+"img.jpg", id);
				}
			}
			alert(this.pile.count()) // => Displays zero
			this.pile.addToStock(1);  // => Crash
			
			
            // Setup game notifications to handle (see "setupNotifications" method below)
            this.setupNotifications();

            console.log( "Ending game setup" );
        },
The game is just crashing the same during the setup. I doubled checked that "img.jpg" existed in my img folder and that's right.

Re: Stock component - addToStock throwing an error

Posted: 10 September 2014, 14:49
by sourisdudesert
Hi,

You are using this.cardwidth in the Stock creation but you defined this.card_width => this is why the Stock is invalid.

I will add an error message in the log when the width/height is not defined.

Cheers,

[SOLVED] Stock component - addToStock throwing an error

Posted: 11 September 2014, 19:08
by Woodruff
Hi,

Thanks verry much, that was only that indeed. I shall pay more attention on my variables name.
Thanks also for adding this more explicit error throw. I admit that without it I did not get the idea of checking that.

Tcheby