Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package trade {
- public class Player {
- public var money:int = 1000;
- public var inventory:Array = new Array;
- public function Player() {
- trace ("The Player's inventory is empty");
- trace ("The Player's money: " + money);
- }
- public function buy(theItem:Item):void {
- trace ("The Player wants to buy a " + theItem.getName());
- if (money >= theItem.getPrice()) {
- inventory.push(theItem.getName());
- money -= theItem.getPrice();
- trace ("The Player bought a " + theItem.getName());
- trace ("The Player's inventory now contains: " + inventory);
- trace ("Player's money remaining: " + money);
- } else {
- trace ("The Player doesn't have enough money to buy " + theItem.getName());
- return;
- }
- }
- public function sell(theItem:Item):void {
- var hasItem:Boolean = false;
- trace ("The Player wants to sell a " + theItem.getName());
- for (var i:int = 0; i < inventory.length; i++ ) {
- // trace ("[checking element " + [i] + " for " + theItem.getName() +"]");
- if (inventory[i] == theItem.getName()) {
- // ("[the location of " + theItem.getName() + " is element " + [i]);
- inventory.splice(i,1);
- money += theItem.getPrice()*.8; // sell at 80% original value
- trace ("The Player's inventory now contains: " + inventory);
- hasItem = true;
- break;
- }
- }
- if (!hasItem){
- trace ("The Player's inventory does not contain a " + theItem.getName());
- }
- trace ("The Player's inventory contains: " + inventory);
- trace ("Player's money remaining: " + money);
- }
- }
- }
- package trade {
- public class NPC {
- public var money:int = 1000;
- public var inventory:Array = new Array;
- public function NPC() {
- trace ("The NPC's inventory is empty");
- trace ("The NPC's money: " + money);
- }
- public function buy(theItem:Item):void {
- trace ("The NPC wants to buy a " + theItem.getName());
- if (money >= theItem.getPrice()) {
- for (var i:int = 0; i < inventory.length; i++ ) {
- if (inventory[i] == theItem.getName()) {
- inventory.push(theItem.getName());
- money -= theItem.getPrice();
- // 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
- Player.inventory.splice(theItem.getName());
- Player.money += theItem.getPrice();
- trace ("The NPC bought a " + theItem.getName());
- trace ("The NPC's inventory now contains: " + inventory);
- trace ("NPC's money remaining: " + money);
- } else {
- trace ("The NPC doesn't have enough money to buy " + theItem.getName());
- return;
- }
- }
- }
- }
- public function sell(theItem:Item):void {
- var hasItem:Boolean = false;
- trace ("The NPC wants to sell a " + theItem.getName());
- for (var i:int = 0; i < inventory.length; i++ ) {
- // trace ("[checking element " + [i] + " for " + theItem.getName() +"]");
- if (inventory[i] == theItem.getName()) {
- // ("[the location of " + theItem.getName() + " is element " + [i]);
- inventory.splice(i,1);
- money += theItem.getPrice()*.8; // sell at 80% original value
- trace ("The NPC's inventory now contains: " + inventory);
- hasItem = true;
- break;
- }
- }
- if (!hasItem){
- trace ("The NPC's inventory does not contain a " + theItem.getName());
- }
- trace ("The NPC's inventory contains: " + inventory);
- trace ("NPC's money remaining: " + money);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement