Shop

by mutantleg
Prototype for an upgrade shop (i've never done a game with buyable upgrades before)
♥0 | Line 174 | Modified 2012-05-12 01:28:31 | MIT License
play

ActionScript3 source code

/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/bqgy
 */

package {
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
       
        //public var vecBtn:Vector.<Sprite>;
        
        public var curMoney:int = 10000;
        public var moneyField:TextField;
       
        public var mshop:Shop = new Shop();  
        
        public var bu:Buyer;
       
        public function FlashTest() {
            // write as3 code here..
            
            graphics.clear();
            graphics.lineStyle(2,0);
            //graphics.drawCircle(200,200,100);
            graphics.beginFill(0x8080ff,0.75);
            graphics.drawRect(0,0,300,300);
            graphics.endFill();
            
            var i:int;
            var k:int;
            var sx:Number = 8;
            var cx:Number = sx;
            var cy:Number = 8;
            var u:UpgradeBtn;
            
            bu = new Buyer();
            mshop.bu = bu;
            
           mshop.addItem(new shopItem("banana",100,0xFFFF00) );
           mshop.addItem(new shopItem("apple",500,0xFF0000) );
           mshop.addItem(new shopItem("life",1000,0xFF0000) );
           mshop.addItem(new shopItem("ammo",500,0xFF0000) );
            
            
            for (i = 0; i < 16; i++)
            {
                u = new UpgradeBtn();
                
                switch (i)
                {
                 case 0: u.item = "banana"; break;
                 case 1: u.item = "apple"; break; 
                 case 2: u.item = "life"; break;
                 case 3: u.item = "ammo"; break;   
                };//swend
                
                u.x = cx;
                u.y = cy;
                u.pshop = mshop;
                u.update();
                addChild(u);
                
                
                
                cx += 64+4;
                k += 1;
                if (k >= 4) { k = 0; cy += 64+4; cx = sx; }
            }//nexti
            
            moneyField = new TextField();
            moneyField.mouseEnabled = false;
            moneyField.mouseWheelEnabled = false;
            moneyField.text = "$"+bu.curMoney;
            moneyField.y = 350;
            moneyField.x = 20;
            moneyField.scaleX = moneyField.scaleY = 2;
            addChild(moneyField);
            
            stage.addEventListener(MouseEvent.CLICK, onClick);
        }//ctor
        
        public function onClick(e:MouseEvent):void
        {
              moneyField.text = "$"+bu.curMoney; 
        }//onclick
        
    }//classend
}
import flash.utils.Dictionary;
import flash.events.MouseEvent;
import flash.text.TextField;

import flash.display.Sprite;



internal class Buyer
{
   public var curMoney:int = 10000;
    
   public function Buyer()
   {}//ctor
    
   public function  takeItem(si:shopItem):void
   {
       if (si == null) { return; }
       
       if (si.price > curMoney) { return; }
       
       curMoney -= si.price;
       
       switch (si.name)
       {
        case "life": 
          si.price *= 2;
          si.btn.update();
        break;   
           
       }//swend
       
   }//boughtit 
    
}//buyer


internal class Shop
{
    public var dictItem:Dictionary;
    
    public var bu:Buyer;
    
    public function Shop()
    {
        dictItem = new Dictionary();
    }//ctor
   
      public function addItem(i:shopItem):shopItem
    {
        if (i == null) { return null;}
        dictItem[i.name] = i;
        return i;
    }//additem
    
    public function getItem(name:String):shopItem
    {
        return dictItem[name];
    }//getitem
    
    public function getBuyer():Buyer
    { return bu;  }
    
}//shop

internal class shopItem
{
    public var price:int = 100;
    public var defPrice:int = 100;
    public var color:int = 0;
    public var name:String = "none";
    public var dispName:String = "none";
    public var btn:UpgradeBtn;
    
    public function shopItem(name_:String, price_:int=1, col_:int=0, dispname_:String=null)
    {
        defPrice = price_;
        price = defPrice;
        color = col_;
        name = name_;
        if (dispname_ == null) { dispName = name; }
        else { dispName = dispname_; }
    }//upitem
    
}//upitem

internal class UpgradeBtn extends Sprite
{
   public var iname:TextField;
   public var price:TextField;
   //public var btn:Sprite;
   public var curPrice:int = 100;
   public var pshop:Shop;
   public var item:String = "none";
   
   public function UpgradeBtn()
   {
       graphics.clear();
       graphics.lineStyle(2,0);
       graphics.beginFill(Math.random()*0xFFffFFff,0.5);
       graphics.drawRect(0,0, 64, 64);
       graphics.endFill();
       
       curPrice = Math.random() * 1000;
       
       price = new TextField();
       price.text = "$"+int(curPrice);
       addChild(price);
       price.mouseEnabled = false;
       price.mouseWheelEnabled = false;
       
       iname = new TextField();
       iname.text = "none";
       iname.y = 20;
       addChild(iname);
       iname.mouseEnabled = false;
       iname.mouseWheelEnabled = false;
       
       
       this.buttonMode = true;
       this.useHandCursor = true;
       
       addEventListener(MouseEvent.CLICK, onClick);
  }//ctor  
  
  public function update():void
  {
      var si:shopItem;
      si = pshop.getItem(item);
      if (si == null) { disable(); return; }
      curPrice = si.price;
      price.text = "$"+int(curPrice);
      iname.text = si.dispName;
      si.btn = this;
  }//update
  
  public function disable():void
  { visible = false; }//disable
   
  public function onClick(e:MouseEvent):void
  {
      var b:Buyer;
      b = pshop.getBuyer();
      if (b == null) { return; }
      b.takeItem( pshop.getItem(item) );
  }//onlclick
    
}//classend