PDA

View Full Version : global varible in data components?


typografuk
01-06-2007, 04:46 PM
I'm working with a data grid component. This is part of the code that adds info to the grid. The first line works, where theitem2 and theprice2 are variables.

I'm wondering why it's not possible to make the variables global variables so that they could be changed outside of this block of AS?

I'm using buttons on the stage to change the variable.
When I trace the value for these global variables it is correct.
But when I add them to the datagrid
_global.test1 and 2 fills in as undefined and NAN

Any thoughts...


var theitem2 = "testitem";
var theprice2 = 10;

var aItem:Array = new Array();
aItem[theitem2] = {instance:bbbcart_mc, price:theprice2, quantity:0};

aItem[_global.test1] = {instance:addtocart_mc, price:_global.test2, quantity:0};

anonymous
01-06-2007, 05:29 PM
Did you cast those variables on the main timeline?

typografuk
01-06-2007, 05:41 PM
I can even trace the value of the global variables from within that block of code and the trace is right.

But in the datagrid it says undefined.

Are there some rules regarding global varables and arrays or components that I'm missing?

thanks

anonymous
01-06-2007, 07:17 PM
This may not be the problem, but I've found that Flash 8 doesn't really like _global variables... You can try setting them on the main timeline, and then referencing to them with _level0.test1 & _level0.test2 rather than with _global...

typografuk
01-06-2007, 10:56 PM
Here's the whole AS - also attached the fla which is really rough looking but I'm just trying to get the code right before I apply it to another project.

Still stumped. Trying to figure out a way to change the items (variables) being shown in the data grid on the fly, without having to hard code all the variables into the AS in the main timeline.

thanks if you can help.


stop();


var theitem = "first item";
var theprice = 4.50;
var theitem2 = "another item";
var theprice2 = 1.50;

//_global.test1 = "xxx";
//_global.test2 = 0.00;



var lvSend:LoadVars = new LoadVars();//do not change
lvSend.business = "spam@email.com"; //your paypal email address here
lvSend.currency_code = "USD"; //the currency you would like to use



//define items, their prices, quantity in basket and there associated movieclips
var aItem:Array = new Array();
aItem[_level0.test1] = {instance:addtocart_mc, price:_level0.test2, quantity:0};
aItem[theitem2] = {instance:bbbcart_mc, price:theprice2, quantity:0};



var totalPrice:Number;

//define drag and drop functionality
for (var i in aItem) {
var currentProduct:String = i;
aItem[i].instance.id = currentProduct;

aItem[i].instance.onRelease = function() {
//it has been added to basket so increase quantity in basket
aItem[this.id].quantity++;

//update datagrid
updateDatagrid();
trace("111 "+_level0.test1);
trace("222 "+_level0.test2);

//also send it back to its original position and fade it in
this._alpha = 50;
reposition(this);
fadeIn(this);

//let user know it has been added to basket

};

aItem[i].instance.onReleaseOutside = aItem[i].instance.onRelease;
}



//--------------------------------------- functions ----------------------------------------------//


function fadeIn(movie:MovieClip):Void{
movie.onEnterFrame = function():Void{
this._alpha +=10 ;
if(this._alpha == 100){
delete this.onEnterFrame;
}
}
}

function updateDatagrid():Void{
totalPrice = 0;
var aBasket:Array = new Array();
for (var i in aItem) {
if(aItem[i].quantity > 0 ){
aBasket.push({Product: aItem[i].instance.id, Quantity: aItem[i].quantity, Price: aItem[i].price * aItem[i].quantity });
totalPrice += aItem[i].quantity * aItem[i].price;
}
}

cart_mc.basketInfo.dataProvider = aBasket;
cart_mc.basketInfo.vScrollPolicy = "auto";
cart_mc.tTotal.text = totalPrice;
}