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
}
Related
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.
I'm programming an app using android studio. I want to know in which way I can do a tutorial that users will see only the first time that use the app. Tutorial like image or screenshoots
Can someone help me? Thanks
I encountered this thread while looking for a solution for running a tutorial only at the first time (as rbaleksandar suggested), so in case it will be helpful for someone someday, here's a template of a solution that works for me (using the SharedPreferences API):
#Override
protected void onResume() {
super.onResume();
String tutorialKey = "SOME_KEY";
Boolean firstTime = getPreferences(MODE_PRIVATE).getBoolean(tutorialKey, true);
if (firstTime) {
runTutorial(); // here you do what you want to do - an activity tutorial in my case
getPreferences(MODE_PRIVATE).edit().putBoolean(tutorialKey, false).apply();
}
}
EDIT - BONUS - If you're into app tutorial - I'm messing now with the ShowcaseView library (which is amazing - try it out). Apparently they have some shortcut for that issue using a method called singleShot(long) - its input is a key for the SharedPreferences, and it does the exact same thing - runs only in the first activation. Example of usage (taken from here):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_single_shot);
Target viewTarget = new ViewTarget(R.id.button, this);
new ShowcaseView.Builder(this)
.setTarget(viewTarget)
.setContentTitle(R.string.title_single_shot)
.setContentText(R.string.R_string_desc_single_shot)
.singleShot(42)
.build();
}
You could always code your own solution, but, let us not reinvent the wheel.
Check this Android Library:
Tour Guide Repository
It allows you to add pointers in your screen, so the user knows where is he supposed to touch next.
It's pretty easy to use, you only need to point to the element you want the user to touch.
From the doc:
Let's say you have a button like this where you want user to click on:
Button button = (Button)findViewById(R.id.button);
You can add the tutorial pointer on top of it by:
TourGuide mTourGuideHandler = TourGuide.init(this).with(TourGuide.Technique.Click)
.setPointer(new Pointer())
.setToolTip(new ToolTip().setTitle("Welcome!").setDescription("Click on Get Started to begin..."))
.setOverlay(new Overlay())
.playOn(button);
Hope this helps!
Some links to libraries for creating introduction and/or tutorial screens.
Horizontal cards like Google Now:
https://github.com/PaoloRotolo/AppIntro
Tutorial screen:
https://github.com/amlcurran/ShowcaseView
As far as I understand the question is not How do I create a tutorial? (as the people who have already posted an answer have concluded) but instead How to show a tutorial upon first launch only?. So here are my two cents on this topic:
I'm not familiar with how your Android app stores its configuration data but I will assume that it's either in a database (SQLite) or a text file (plaintext, YAML, XML - whatever). Add a configuration entry to wherever the app's settings are being stored - something like tutorial_on : false, tutorial_on : 1 etc. depending on the format the configuration is represented in.
The way configurations work is that whenever an app (or software in general) is launched it has to be loaded in the app itself. So add the following to your app (where and how is up to you and your app design):
Check tutorial_on entry
If tutorial_on is set to true/1 whatever
2.1 Display tutorial
2.2 Change tutorial_on to false/0 whatever
2.3 Store the result in your configuration
Continue using the app
By doing so the first time your app launches the flag responsible for displaying the tutorial will be toggled and afterwards every time you start the app the toggle flag will be read leading to omitting the tutorial.
Personally I would suggest that you an option similar to Don't show tutorial anymore along with a description how to re-enable it (by triggering some action in that app's menu etc.). This has two major benefits:
Improved user experience - users like to have control (especially over trivial matters such as showing or hiding a tutorial). Whenever you take the control away from them, they get pissed off.
Enable your user to re-learn forgotten things - a general rule of thumb is to create apps that should not burden the user with a lot of stuff to remember. That is why things should be self-explanatory. However sometimes you may want to do that nonetheless. By adding the possibility that the user re-launches (by simply resetting the tutorial_on flag and repeating the steps from above) the tutorial allows just that - refreshing a user's memory.
So I am currently designing a game where the main idea is that you choose your attack and then an animation plays out based on the attack that you picked (think pokemon). The game is turn-based as well.
My question is whether scene2d would be easier to use than implementing a custom solution for handling the animation part of the game. From what I've read (and I've found it difficult to find good information on scene2d), it sounds like scene2d would make designing the UI for the buttons/menu extremely easy, but I'm not sure how I can roll that into making the actors move. Is it as simple as handling the touch event on the button and calling the corresponding actor's action method based on the player's choice?
In what I have in mind, the actors never actually move (except during their animation and they don't move across the screen, they merely go through their animations in place). During the animation, there will also be particle effects (the attack) which, if using scene2d, would need to be their own actors. Would the synchronization of the actors and the attack be difficult to produce?
Actors do movee..
actor.addAction(Actions.moveTo(posX, posY, 5)));
by this your actor moves to posX, posY and 5 is the time duration ..
using scene2d would be a good idea in my opinion..
Scene2D would be better since you would have to manually implement the listener for actions such as Click when you use Sprite whereas Scene2D provides the functions to set Listeners.
you may already know this, but let me try to answer your question:
Scene2d has a very handy action system, which basically allows the following:
do any of the provided actions
fine tune them with the many provided interpolations
make new actions with Actions.run()
chain existing actions forming sequences
Like this:
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.Action;
Action a1 = Actions.sequence(Actions.fadeOut(0), Actions.fadeIn(3, Interpolation.bounce));
Action a2 = Actions.moveTo(100, 200, 3, Interpolation.pow2Out);
Runnable r = new Runnable() {
#Override
public void run() {
setColor(1, 0, 0, 1);
System.out.println("now I'm a red actor");
}
};
And then combine then, for example like this:
addAction(Actions.sequence(Actions.parallel(a1, a2), Actions.run(r)));
This allows you to profit from scene2d's built-in sequencer saving you the half of the work, at least. So, answering your question, I think it is very possible indeed to easily implement fixed as well as reactive/randomized animations using this system. It also allows you to easily encapsulate simpler actions into complexer ones, and has the following advantages:
Very readable and maintainable code
Tradeoff CPU/Mem: much more memory-efficient than storing plain sequences or even videos
Reactivity: this way you can program your animations to be slightly different each time
On the other hand, this developing system can become very time consuming if you constantly want "uncovered" things, like the following:
Implement time-based actions yourself that aren't built-in (like camera travelling)
Make your own interpolations if the built-in ones don't fit your goals
Work with many little granular elements (for that sake I would use the ParticleEditor).
Which I don't think is your case. As a last remark, you should take a look at the spine animation engine. I don't use it myself but may be useful for what you have in mind.
My app has some UI elements that are based on some settings from the user and I am not sure how I should go about coding this for best performance and there doesn't seem to be a good way to Google this to try to find if this has been asked previously.
Basically I want to check for the App Version UI set in the settings and I currently do it via a function:
public String appVersion() {
appSettings = currentActivity.getSharedPreferences(APP_SETTINGS, 0);
String prefAppVersion = appSettings.getString("appVersion", "v2");
return prefAppVersion;
}
Then I will display certain UI elements via an if/else statement:
if (appVersion().equals("v2")) {
// do something here
}
else {
// do something else
}
Is this going to cause memory problems if I call the function 5-6 times within my app (getting a SharedPreference over & over again) or am I better somehow declaring a global variable that gets the SharedPreference once and then uses that for the tests? My only concern with that being if the Preference changes and the UI needs to be redrawn if the variable is not reset.
Your thoughts / input is greatly appreciated.
I don't think the appVersion function will cause any memory problem, but maybe make appSettings as a local variable would be a better practice.
I don't understand the second question, do you mean how to know the preference being changed? SharedPreferences.OnSharedPreferenceChangeListener may help you.
I want to know is there any method or any link or tutorial to perform redo undo operation in Android edittext. If any one knows than please let me know.
Quick note on the Antti-Brax/Divers(Kidinov) solution. It works great, except if you try to use it with a TextView post-API 23, you'll run into problems, because guess-what, Google actually added a hidden UndoManager (android.content.UndoManager) and didn't document it or make it obvious it was there. But if you have a hard/bluetooth keyboard in Marshmallow or Nougat and hit ^Z or SHIFT-^Z, you'll get undo/redo.
The problem comes if you're already using Antti-Brax's class with an EditText, and you also hook it to ^Z and shift-^Z, you'll run into problems with anyone using a hard keyboard. Namely the ^Z will trigger BOTH the native and Antti-Brax's undo, leading to two undos simultaneously, which isn't good. And after a few of them, you'll probably get a Spannable out of bounds crash.
A possible solution I found is to subclass the TextView/TextEdit/whatever and intercept the undo/redo calls from the TextView so they don't run as follows:
#Override
public boolean onTextContextMenuItem(int id) {
int ID_UNDO, ID_REDO;
try {
ID_UNDO = android.R.id.undo;
ID_REDO = android.R.id.redo;
} catch (Resources.NotFoundException e) {
ID_UNDO = 16908338; // 0x1020032
ID_REDO = 16908339; // 0x1020033
}
return !((id == ID_UNDO) || (id == ID_REDO)) && super.onTextContextMenuItem(id);
}
Those magic id numbers were found here, and are used only as a backup if the android.R.id.undo values aren't found. (it also might be reasonable to assume that if the values aren't there the feature isn't there, but anyway...)
This is not the best solution because both undo trackers are still there and both are running in the background. But at least you won't trigger both of them simultaneously with ^Z. It's the best I could think to do until this gets officially documented and the getUndoManager() methods of TextView is no longer hidden...
Why they made a feature you can't turn off (or even know if it was there or not) "live" in released Android I can't say.
I just opened an issue on Android's issue tracker if anyone wants to follow this.
There is an implementation of undo/redo for Android EditText in
http://credentiality-android-scripting.googlecode.com/hg/android/ScriptingLayerForAndroid/src/com/googlecode/android_scripting/activity/ScriptEditor.java
The code works but does not handle configuration changes properly. I am working on a fix and will post here when it is complete.
My Google search was :-
android edittext onTextChanged undo
I know this is an old question, but as there is no accepted answer, and this is an issue I've tackled myself from many angles, I'd like to add my solution in case it helps anyone. My answer is probably most relevant to large (1,000words+) volumes of text editing apps that require this feature.
The simplest way to resolve this problem is to make periodic copies of all text on screen, save it to an array and call setText() every time the Undo method is called. This makes for a reliable system, but it isn't ideal for large (i.e. 1,000words+) text editing apps. This is because it:
Is wasteful. It could be that only one word changes in a two thousand word document, so that's one thousand, nine hundred and ninety nine words needlessly committed to memory.
Can lead to performance issues, as some low-tier hardware struggles with rendering large amounts of text. On some of my test devices, this method can lead to freezes of a few seconds whenever Undo is called.
The solution I currently use is comparatively complex, but I've published the results in a library here.
Essentially, this library saves a copy of text as soon as a user begins typing, and then another copy of text once they've stopped typing for a set amount of time (in my case, two seconds). The two text strings are then compared, and the altered section of text returned, the indexes where the alterations occured, and details on whether or not the change was an addition of new text, a deletion, or a replacement of old text with new text.
The net result is that only the necessary text is saved, and when Undo is called, there is only a local delete(), replace() or insert() call, which makes for much faster operations on large text fields.
Here is the undo/redo implementation that was linked to from Gary Phillips' answer extracted into a reusable and universal undo/redo plugin for any widget that descends from a TextView. I added some code for persisting the undo history.
http://code.google.com/p/android/issues/detail?id=6458#c123
Hope this helps.
To preserve EditText Styling with regards to undo:
You can create an ArrayList<EditText> or ArrayList<String> (String containing html text) to store your last 10 (for example) actions. So ArrayList [0] would contain html text from your first action and ArrayList [9] would contain html text from your very last action. Each time the user taps "undo" in your app, you would apply ArrayList [size()-1] to your EditText and then remove ArrayList [size()-1] from your Array.