Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to convert a Boolean to a String?

Guest
Mar 05, 2016 Mar 05, 2016

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

TOPICS
ActionScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 05, 2016 Mar 05, 2016

var slot:Boolean = true;

var _string = String(slot);

trace(string);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2016 Mar 05, 2016

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 05, 2016 Mar 05, 2016

I was thinking about going about it that way but I wasn't sure how. Do you have a general idea on how to?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 05, 2016 Mar 05, 2016

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;

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 05, 2016 Mar 05, 2016

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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 06, 2016 Mar 06, 2016
LATEST

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');

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines