Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package trade {
  2.    
  3.     public class Player {
  4.         public var money:int = 1000;
  5.         public var inventory:Array = new Array;
  6.        
  7.         public function Player() {
  8.             trace ("The Player's inventory is empty");
  9.             trace ("The Player's money: " + money);
  10.         }
  11.        
  12.         public function buy(theItem:Item):void {
  13.             trace ("The Player wants to buy a " + theItem.getName());
  14.             if (money >= theItem.getPrice()) {
  15.             inventory.push(theItem.getName());
  16.             money -= theItem.getPrice();
  17.             trace ("The Player bought a " + theItem.getName());
  18.             trace ("The Player's inventory now contains: " + inventory);
  19.             trace ("Player's money remaining: " + money);
  20.             } else {
  21.                 trace ("The Player doesn't have enough money to buy " + theItem.getName());
  22.                 return;
  23.             }
  24.         }
  25.        
  26.         public function sell(theItem:Item):void {
  27.             var hasItem:Boolean = false;
  28.             trace ("The Player wants to sell a " + theItem.getName());
  29.             for (var i:int = 0; i < inventory.length; i++ ) {
  30.             //  trace ("[checking element " + [i] + " for " + theItem.getName() +"]");
  31.                 if (inventory[i] == theItem.getName()) {
  32.                 //  ("[the location of " + theItem.getName() + " is element " + [i]);
  33.                     inventory.splice(i,1);
  34.                     money += theItem.getPrice()*.8; // sell at 80% original value
  35.                     trace ("The Player's inventory now contains: " + inventory);
  36.                     hasItem = true;
  37.                     break;
  38.                 }
  39.             }
  40.             if (!hasItem){
  41.             trace ("The Player's inventory does not contain a " + theItem.getName());
  42.             }
  43.            
  44.             trace ("The Player's inventory contains: " + inventory);
  45.             trace ("Player's money remaining: " + money);
  46.         }
  47.        
  48.     }
  49.    
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59. package trade {
  60.    
  61.     public class NPC {
  62.         public var money:int = 1000;
  63.         public var inventory:Array = new Array;
  64.        
  65.         public function NPC() {
  66.             trace ("The NPC's inventory is empty");
  67.             trace ("The NPC's money: " + money);
  68.         }
  69.        
  70.         public function buy(theItem:Item):void {
  71.             trace ("The NPC wants to buy a " + theItem.getName());
  72.             if (money >= theItem.getPrice()) {
  73.             for (var i:int = 0; i < inventory.length; i++ ) {
  74.                 if (inventory[i] == theItem.getName()) {
  75.                 inventory.push(theItem.getName());
  76.                 money -= theItem.getPrice();
  77.                 // right here I'm trying to remove this item from Player's inventory but I can't access the variable if they're not static even though they're public
  78.                 Player.inventory.splice(theItem.getName());
  79.                 Player.money += theItem.getPrice();
  80.  
  81.                 trace ("The NPC bought a " + theItem.getName());
  82.                 trace ("The NPC's inventory now contains: " + inventory);
  83.                 trace ("NPC's money remaining: " + money);
  84.                 } else {
  85.                     trace ("The NPC doesn't have enough money to buy " + theItem.getName());
  86.                     return;
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.        
  92.         public function sell(theItem:Item):void {
  93.             var hasItem:Boolean = false;
  94.             trace ("The NPC wants to sell a " + theItem.getName());
  95.             for (var i:int = 0; i < inventory.length; i++ ) {
  96.             //      trace ("[checking element " + [i] + " for " + theItem.getName() +"]");
  97.                 if (inventory[i] == theItem.getName()) {
  98.                 //  ("[the location of " + theItem.getName() + " is element " + [i]);
  99.                     inventory.splice(i,1);
  100.                     money += theItem.getPrice()*.8; // sell at 80% original value
  101.                     trace ("The NPC's inventory now contains: " + inventory);
  102.                     hasItem = true;
  103.                     break;
  104.                 }
  105.             }
  106.             if (!hasItem){
  107.             trace ("The NPC's inventory does not contain a " + theItem.getName());
  108.             }
  109.            
  110.             trace ("The NPC's inventory contains: " + inventory);
  111.             trace ("NPC's money remaining: " + money);
  112.         }
  113.        
  114.     }
  115.    
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement