A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [AS 3.0] Game shop question

  1. #1
    Senior Member
    Join Date
    Jan 2008
    Posts
    107

    [AS 3.0] Game shop question

    Hi all,

    I'm trying to build a real simple text rpg (in as 3.0) and would like to know how to program a popular functionality that appears on shops.

    Let's assume the player has 50 coins and visits a shop. The item costs 100 coins.

    How can I make the "Buy Item" button not available (grey out/unable to click) if the player has less coins than the item cost? On the other hand, if the player can afford the item, the "Buy Item" button should be available and "clickable".

    I assume this is done with if/else's, but how can I program it?

    Thanks.

  2. #2
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    as soon as player enters the shop, use a for loop to go through all of your items, assuming they are in an array, do something like:
    Code:
    for(var i:uint=0;i<item_array.length;i++)
    {
       if(item_array[i].price>player.coins)
       {
           item_array[i].buy_Button.disabled = true;
       }
    }
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  3. #3
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Hi,

    Hmm...I don't know how to use arrays.

    I'm just placing the code on the timeline. Each frame has code in it. It's not pefect, but I'm slowly learning.

    Edit: one more thing...

    I have the following code:
    Code:
    btn_healHero.addEventListener(MouseEvent.CLICK,healHero);
    
    function healHero(e:MouseEvent):void
    {
    	if(heroHP < heroMaxHP)
    	{
    		gold -= 50;
    		heroMaxHP == heroHP;
    		txt_gold.text = String("Gold: " + gold);
    		txt_healStatus.text = String("You have been healed!");
    	}
    	else
    	  if(heroHP == heroMaxHP)
    	  {
    		txt_healStatus.text = String("You don't need to be healed!");
    	  }
    	  else
    	  {
    		txt_healStatus.text = String("You're too poor. Go away!");
    	  }
    }
    I'm not sure why this doesn't work.

    If hero's current health (heroHP) equals to max health (heroMaxHP) then there's no need to heal. The player should press the heal button, where the message "You don't need to be healed!" should be displayed and no gold should be spent.

    On the other hand, if heroHP < heroMaxHP, player loses 50 gold and gets fully healed. The last case occurs when the hero can't afford to pay the heal.

    Edit #2: One more thing, regarding the
    Code:
    btn_buyItem1.enabled = false;
    it works but the button only becames unavailable when I don't have money AND click on it.

    How can I make him unavailable the moment the player can't afford the item?

    Thanks.
    Last edited by Hurdarr; 03-28-2010 at 08:33 AM.

  4. #4
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    Code:
    	if(heroHP < heroMaxHP) // this should be if ((heroHP < heroMaxHP) && (gold>=50))
    	{
    		gold -= 50;
    		heroMaxHP == heroHP; //This will be heroHP = heroMaxHP;
    == means "if a equals to b".....it's a classic mistake, and almost everybody makes it from time to time.


    As for shop/inventory, creating without array is pretty inefficient. I suggest you learn to use arrays now rather than working hundred times more later on.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  5. #5
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    hello again,

    Thanks for the quick reply. I made the changes you mentioned, but something isn't right.

    This is the code I have for that paticular frame:
    Actionscript Code:
    btn_exitHealing.addEventListener(MouseEvent.CLICK,exitHealing);

    function exitHealing(e:MouseEvent):void
    {
        gotoAndStop("GameStart");
    }

    txt_heroHP.text = String("HP: " + heroHP);
    txt_heroMaxHP.text = String("/ " + heroMaxHP);
    txt_gold.text = String("Gold: " + gold);


    btn_healHero.addEventListener(MouseEvent.CLICK,healHero);

    function healHero(e:MouseEvent):void
    {
        if ((heroHP < heroMaxHP) && (gold>=50))
        {
            gold -= 50;
            heroMaxHP = heroHP;
            txt_gold.text = String("Gold: " + gold);
            txt_healStatus.text = String("You have been healed!");
        }
        else
          if(heroHP == heroMaxHP)
          {
            txt_healStatus.text = String("You don't need to be healed!");
          }
          else
          {
            txt_healStatus.text = String("You're too poor. Go away!");
          }
    }

    I have the following buttons and dynamic text boxes:

    txt_heroHP, txt_heroMaxHP, txt_healStatus, txt_gold and the button btn_healHero.

    I have an upgrade that costs 200 gold and gives +10 additional MAX HP. I bought it. My HP is 10 out of 20. I'm gonna heal it. When I press the "Heal" button, my life HP turns 10/10 instead of 20/20.

    Also, the dynamic text fields don't update but that's probably because I have to add

    Actionscript Code:
    txt_heroHP.text = String("HP: " + heroHP);
    txt_heroMaxHP.text = String("/ " + heroMaxHP);
    to the if ((heroHP < heroMaxHP) && (gold>=50)) conditional.

    Not sure if there is anything wrong with this code or do I need to create another variable to keep track of the maxHeroHP value after upgrading it?

    Thanks again.

  6. #6
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    gah, you didn't see what i wrote carefully enough....look in my earlier post
    Code:
    heroMaxHP = heroHP; // you are setting the maxhp to the current one
    Correct code:
    Code:
    heroHP = heroMaxHP;
    Also don't split "else if" in two lines, it will cause confusion in nesting later on.
    Code:
    else
          if(heroHP == heroMaxHP)
          {
            txt_healStatus.text = String("You don't need to be healed!");
          }
    should be
    Code:
    else if(heroHP == heroMaxHP)
          {
            txt_healStatus.text = String("You don't need to be healed!");
          }
    Last edited by bluemagica; 03-28-2010 at 01:08 PM.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  7. #7
    Senior Member Alluvian's Avatar
    Join Date
    Jun 2006
    Posts
    967
    Definately take the time to learn arrays, there are lots of tutorials on them out there, they will make your life a LOT easier, especially in something like an rpg. Even a light text rpg is going to be array heavy.

    You are going to want arrays anytime you want groups of things. Like your store, your store has a group of items for sale, your character's inventory is a group of items, etc...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center