0

I have a pretty basic knowledge of AS3, so I'm not sure if this is actually possible, or if it is, how to do it. I am in the very starts of coding this project, so I don't actually have much code to show, but would like to figure this issue out before I start writing everything around it.

I am making a game similar to Monopoly, so I will speak in terms of that. Basically, I want 4 Players all with variable integers that reflect how much money they have. In my game, I want to be able to click on a player, click "Buy Property", and then click the property they're buying, which would be a button (btnProperty1).

Let's say Property 1 also has a purchase value, which could be "valueProperty1", and let's say the value is 100. My players will start with 1000, so I want the buying of this property to deduct 100 from the active player money. The player money int could be "moneyPlayer1, moneyPlayer2, etc.". So, I would be able to click btnPlayer1, btnBuyProperty, btnProperty1, and then moneyPlayer1 would go from 1000 to 900. To determine the active player, I had the idea of making an "activePlayer" int, and just setting it to 1, 2, 3 or 4 based on which btnPlayer I click.

In a shotty way of putting it, I could put in the btnProperty1 if (activePlayer == 1){moneyPlayer1 -= valueProperty1} and write four statements, one for each player. HOWEVER, I would like to subvert doing that by writing a function that looks like this:

 moneyPlayer[activePlayer] -= valueProperty1 (if the activePlayer int is equal to 1, then it would fill in 1 to the end of moneyPlayer, therefor targeting moneyPlayer1)

In theory, it would then deduct money from whichever player is active. IF THIS IS POSSIBLE, I have no idea how to write it.

Additionally, I would like to do something similar if a subsequent player lands on an owned property. So in the same vein, I would want something that looks like this:

moneyPlayer[activePlayer] -= rentProperty1 (the rent value is deducted from the active player)

moneyPlayer[paidPlayer] += rentProperty1 (the rent value is added to the paid player)

I can figure out ways to make the active player and paid player functions work, I just need to know if I can put these variables INSIDE of other variables, so I don't have to write 18,748 if statements.

Lastly, I would love to be able to enter a player name into an input text field, then have it appear in a dynamic text field. I tried looking around and couldn't find an answer... So bonus points for this one :)

Any help would be GREATLY appreciated!! Thank you!

2
  • Your interpretation seems fine. Making an Array or Vector of ints where the index corresponds to a different player will work in the syntax you used (moneyPlayer[currentPlayer] -= valueProperty1). I'd recommend making a Player class though instead with a money value and having a static currentPlayer property so you can change the current Player's money by doing Player.currentPlayer.money -= valueProperty1. That way if you want the players to have further functionality beyond just money you'll be able to easily add it to their class. Commented Oct 2, 2020 at 3:22
  • You actually need to plan architecture of your game, and I advise you to use classes, say Player or Property. A property can have value, owner, upgrade level, and a picture, for example, a player can have money, property list, map position, maybe more. So when you want a player to buy or check if he can buy an empty property, you just write one piece of code (a method for Player class that accepts a Property) and just ignore visibility issues. AND, ignore typo issues, to not accidentally deduce money from player 2 instead of player 1 when buying, for example. Commented Oct 2, 2020 at 5:22

1 Answer 1

2

To answer your question, YES you can put variables "inside" another variable, this is called "classes" in AS3, and is a required thing to use whenever you plan something big. Say you want a Player class, then you can do like this:

public class Player {
    private var _money:int;
    ... // whatever else a player has
    public function get money:int { return _money;} // a property
    // such functions are the way to make someone not directly steal the player's money, 
    // but are a way to view the money of a player
    // rest of property functions goes here
    public function Player() // a constructor, required
    {
        _money=1000; // starting money
        ... //etc, place default values everywhere
    }
    public function earnMoney(amount:int) // makes the player able to earn money
    {
        _money += amount; // negative money may be accepted, learn to check parameters!
    }
    // everything else goes here
}

Then you add there more functions that can change a player's state, say a function buy(aProperty):Boolean that would return true if the player did buy that property, and will also change the variables "inside" both the property involved and the player (this, or just call variables as if they are local).

You can do way more with classes, please read about object-oriented programming, there's a lot of basics that can help you draw your game's architecture. Learn about "encapsulation" first, to not accidentally break your objects. An example was shown above.

Sign up to request clarification or add additional context in comments.

3 Comments

Please add the explanation about OOP to your answer, because it is literally what the question is about.
@Organis what exactly do you want me to add here? OOP is a discipline people learn for several years in high school (we had two), it's language independent at the very least, and its basics are illustrated here already.
We don't know if that person knows of OOP. Assuming the nature of the question — probably not. Adding something like "...hey, what you are asking for is OOP en.wikipedia.org/wiki/Object-oriented_programming so how lucky we could be that AS3 is an OOP language..." is just a good thing to do before the actual answer/example you provide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.