Copy link to clipboard
Copied
Hey there, I'm tinkering around with flash trying to build just a fun little RPG for myself. I'm currently trying to make an inventory and I've run into a problem. I was wondering if I am able to convert a boolean into a string (doesn't matter if an if statement is used).
Basically when I click on an item I want it to check what the first open spot is and go there. I'm doing this by having a for loop, each iteration of the loop it creates a string with the value "slot" + currentNumberInForLoop. I have named my inventory slot booleans, slot0, slot1, etc. I want to convert these booleans to strings, check if they equal the other string, and keep going through until it is found true (an open inventory slot).
If you need further clarification on my question let me know (it's 4am and I'm tired )
Thanks,
Eric
Copy link to clipboard
Copied
var slot:Boolean = true;
var _string = String(slot);
trace(string);
Copy link to clipboard
Copied
you can use slot.toString() but your setup/premise is faulty. your slots shouldn't be booleans.
generally, you'd use:
var slots:Array=[];
and then add inventory items to your slots array.
Copy link to clipboard
Copied
I was thinking about going about it that way but I wasn't sure how. Do you have a general idea on how to?
Copy link to clipboard
Copied
like so:
var slots:Array = [];
// to add an item, pass the item string to addInventoryItemF(item);
// to remove an item, pass the item string to removeInventoryItemF(item);
// to check if an item is in inventory, call checkInventoryF(item)
function addInventoryItemF(s:String):void{
// if there's a limit to the inventory size, put it here. ie, check slots.length
slots.push(s);
}
function removeInventoryItemF(s:String){
if(slots.indexOf(s)>-1){
slots.splice(slots.indexOf(s),1);
}
}
function checkInventoryF(s:String):Boolean{
if(slots.indexOf(s)>-1){
return true;
} else {
return false;
}
}
Copy link to clipboard
Copied
Thanks for your help but sorry I'm not 100% understanding this. Say I have an item called ironSword. How would I call each function to see if it's in my inventory, if it is, what slot and to remove it, and if it is not, add it to the next open inventory slot?
Copy link to clipboard
Copied
to check for ironSword:
if(checkInventoryF('ironSword')){
//do whatever you have an ironSword
} else {
// do something else. you have no ironSword
}
to remove ironSword:
removeInventoryItemF('ironSword');
Find more inspiration, events, and resources on the new Adobe Community
Explore Now