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!
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.
Related
I am trying to implement a series of notifications based on radio button options the user selects. The notifications date and time will set depending on both radio button options and the user selected date.
for example the user selects Option 1, 2 and 3 along with Jan 1st 2017 and 12 notifications are set every couple of days/weeks depending on said options.
Before I get too far into this, am I just looking at a complex if then statement to set these notifications or am i missing another solution?
When your if-else statements get too long you might be needing a switch statement instead. You said there are twelve notification options so a switch could do this just fine.
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
Remember to separate each option in a modular fashion using different classes, or methods so that you just have to call each block of code.
A lot of switcing or if/else can also be a symptom of you trying to express intrinsically polymorphic code. You are essentially replicating the vtable the compiler would do for you. Following separating business logic from view (rendering) logic -- ie some form of MVC -- you could associate a subclass with each radio button to which the button delegates the logic when it is pressed (btw you might not necessarily need separate subclasses). The controller of each radio button may have a reference to some other object which stores the final frequency of days/hours/minutes, etc for each to call the alarm. So as each radio button is pressed, it delegates to its controller which in turn interprets the value of the radio button and in turn calls into the alarm controller, incrementing / decrementing some value using some appropriate API.
I know it might sound like over-engineering but it is quite simple to set up and it is likely that you will minimally need that alarm controller for other details (which also makes it a lot easier to test; you definitely do not want to shove business logic in your views (activities) as it makes testing a nightmare). So if you wish to simplify this, you could forego each button's individual controller and instead have each button directly invoke the alarm controller API if you prefer to update it.
I've decided to create an Android touch screen game. I am a complete and utter beginner and am learning as I go.
My game has a little elephant that moves up when you press and hold on the screen and falls when there is no contact with the screen. The aim is to collect as many peanuts that fly past as possible to gain the highest score. Pretty simple, you'd think so.
So far, I've managed to get to the point where the elephant can collide with a peanut and the peanut disappears.
My issue right now is, I can't create more than one peanut, with the same instance name of "peanut" because only the one will work and the others will not be recognized. I've done a good ole google search and nothing has really given me the right way to go. Could someone give me a clear answer of what to do or where to go from here?
If you need any more info, the code or a picture of what i've got so far to help you understand just let me know :)
Samantha
Instance name must be unique, and you cannot use instance name to find a set of movie clips. You should instead use an array, and at creating a peanut add it there too using say push(), and at collecting a peanut, splice it out.
In fact, whenever you get a multi-instance class with similar functionality (aka "collect"), use an Array to store references to all of these, so you will always know that ALL of your instances are accessible through that array.
How to work with arrays
A sample code:
var peanuts:Array=new Array();
function addPeanut(x:Number,y:Number):void {
var peanut:Peanut=new Peanut(); // make a peanut, you do this somewhere already
peanut.x=x;
peanut.y=y;
peanuts.push(peanut); // this is where the array plays its role
game.addChild(peanut); // let it be displayed. The "game" is whatever container
// you already have to contain all the peanuts.
}
function removePeanut(i:int):void {
// give it index in array, it's better than giving the peanut
var peanut:Peanut=peanuts[i]; // get array reference of that peanut
peanuts.splice(i,1); // remove the reference from the array by given index
game.removeChild(peanut); // and remove the actual peanut from display
}
function checkForPeanuts():void {
// call this every so often, at least once after all peanuts and player move
for (var i:int=peanuts.length-1; i>=0; i--) {
// going through all the peanuts in the array
var peanut:Peanut=peanuts[i];
if (player.hitTestObject(peanut)) {
// of course, get the proper reference of "player"!
// YAY got one of the peanuts!
// get some scoring done
// get special effects, if any
removePeanut(i); // remove the peanut
}
}
}
I am creating a game similar to Pokemon and right now I'm trying to figure out the best way to handle my battle.
I have created a monster class which when instanced contains a particular monster's name, health, stats, etc. I will have two monsters fighting each other with the player able to select a move (will likely be a class as well with damage, info, and type stats) to attack the opponent with.
Would I be best served to make the Moves class an activity so that I can use methods to attack and calculate damage? Or should I create a battle activity which I send the monsters pertinent stats and have it deal with changing the health and printing out the text explaining what happened? This way, my monster and moves class only holds the information and doesn't use any methods.
From a design perspective, your second thought is more on cue.
The basic idea that you have is that each object (Monster, in this case) will have public functions available to get what moves are available, current stats, etc.
Thus the Battle Activity will expect to have one or more Monster objects per team, rules defined as to what makes the battle win, handle which monster goes first, etc.
Thus if a monster takes damage and we need to save that, you can make a function in the Monster class such as:
public void takeDamage(double damage) {
this.hp -= damage;
//Handle status updates if hp <= 0
//Automatically save the monster's state, to prevent cheating by quitting before battle is over!
this.saveMonsterState();
}
And this would be called from the Battle activity whenever one monster hits another. Hope that helps!
I'm creating a spades app with 1 human player and 3 computer players.
The problem that I am having is, the play must happen sequentially (clockwise) and I need my program to wait on the player input. I can't use wait() and notify(). I have tried while loops to check whether the user has selected a card but those stop the program from running. I've tried recursive methods that won't return anything until the player has chosen a card. That too does not work. So what do I do? I'm stuck.
My method goes like this (leaving out the non-pertinent code)
private void game(){
while(there are more tricks to be played)
while(each player has not played){
if(human turn)
get input from player
else
computer plays
}
}
Maybe you should change a little bit your game controller. Instead of waiting for anything, have your program continuously paint the screen. If user inputs nothing, same screen is paint all the time.
Once he presses a key (or clicks a card or whatever events you have), simply modify the paint method's argument (the screen to be painted). Thus you will separate painting the screen and input handling. It's a common technique called MVC model.
Maybe this will help (it's my game creating blog, and the links inside it are also helpful):
http://m3ph1st0s.blogspot.ro/2012/12/create-games-with-libgdx-library-in.html
You don't need to adapt all your game to the game described there, only the technique of separating input from painting. As a general technique, you should NOT have input determine painting.
Hope it helps. Don't hesitate to request further help.
You can try to add Event Handlers. It will try triger a event every time the user selects a card.
Try this.
Create one thread and in that threat call sleep(1000);
I am new to Android and have learned a lot with this project. I am almost there but, but one of my missing parts is... the game does something like the Simon Says game, and right now I am able to click on the Images and it also loops through them for the user but I can touch them while the loop goes through them. I want now to be able to set like a boolean flag that says userTurn if 0 then it's user turn and if 1 then its the system. Something of that sort.
What would be the best way to approach this?
If the various states in which your application can be in is very large and/or complex, I would recommend the state pattern. But if you just need two states, either system or user, I would use something like this:
private boolean isSystemTurn = false;
and then check it like that:
if (isSystemTurn) {
// the user has to wait
} else {
// it is user turn
}