I'm looking for a way to create a "header" or something like that to specify some variables like:
enum Misc
{
double EFFECT_DAMAGE = Math.pow(2,0);
double EFFECT_ABSORB = Math.pow(2,1);
double EFFECT_HEAL = Math.pow(2,2);
int SPELL_FIREBALL = 51673;
}
And in every .java file I want to be able to write:
double effect = 1;
if (effect == EFFECT)
{
...some code...
}
Is there a nice way to do this?
I'm creating a mini game for now and want to have all the files nice and tidy to manage my project in the future easier once it gets bigger.
Thx in advance.
public enum Misc
{
EFFECT_DAMAGE(0), // 2^0
EFFECT_ABSORB(1), // 2^1
FIREBALL(245151);
private double value;
private Misc(double d){
value = d;
}
public String toString(){
return String.valueOf(value);
}
}
Access like this:
System.out.println("Fireball damage:" + Misc.FIREBALL);
You cannot assign values this way to a enum in java.
Instead you should use a public class with public static final variables to make them constants.
public class Misc {
public static final double EFFECT_DAMAGE = Math.pow(2,0);
public static final double EFFECT_ABSORB = Math.pow(2,1);
public static final double EFFECT_HEAL = Math.pow(2,2);
public static final int SPELL_FIREBALL = 51673;
}
So you can use in your code like
if (effect == Misc.EFFECT_DAMAGE )
{
...some code...
}
If you want use only the field without the class name first you should import the class as static:
import static test.Misc.*;
....
if (effect == EFFECT_DAMAGE ) {
Related
Im trying to build a highscore for a game. A Run is an ojbect made for this game.
The class looks like this:
public class Run {
private int level;
private int moves;
private int time;}
The array of the highscore Looks like this:
Run Highscoresaving[] = new Run[10];
Now i would like to sort this array. The most important value is a high Level, second a low amount of moves and third a short amount of time. Moves are just needed if the run reached the same Level and time is just needed if the run reached the same Level and had the same amout of moves.
You can use lambda expression:
List<Run> myRuns = //init
Collections.sort(myRuns, new RunComparator());
To do it you have to implement Comparable<> in your object like this:
public class Run implements Comparable<Run> {
private int level;
private int moves;
private int time;
}
public class RunComparator implements Comparator<Run>{
public int compare(Run o1, Run o2) {
if(o1.getLevel() == o2.getLevel()){
if(o1.getMoves() == o2.getMoves()){
return o1.getTime() - o2.getTime();
}
return o1.getMoves() - o2.getMoves();
}
return o1.getLevel() - o2.getLevel();
}
hope this helps
Hello all i want to make use of a global integer variable that i shall be incrementing in 7 different activities according to the users right or wrong choise. The problem is that i every time i implement the variable in each different activity , the value is not kept. Instead i get the default value of the variable. What i want is that every increment i make to the variable is saved , when i use it again in the next variable. Any help appreciated.
I have tried and failed :
public class MyApplication extends Application {
private int grade=0;
public int setGrade(int grade) {
this.grade = grade;
}
public int getGrade() {
return grade;
}
}
public class lessonOnePhoto extends Activity {
private int grade = ((MyApplication) this.getApplication()).getGrade();
if (rbtn[0].getText().toString().equals("Boy")) {
grade++;
}
else {
Toast.makeText(getApplicationContext(),"Wrong Choise",Toast.LENGHT_SHORT).show();
}
}
The grade you are incrementing is local and private to your activity. It is also a primitive, rather than an object, so grade = .getGrade() will set the local variable to the same value as the global value, it is not some kind of reference.
Instead, do something like this:
MyApplication myApplication = ((MyApplication) this.getApplication());
myApplication.setGrade(myApplication.getGrade()++);
Or implement increment decrement methods.
public class MyApplication extends Application {
private int grade=0;
public int setGrade(int grade) {
this.grade = grade;
}
public int getGrade() {
return grade;
}
public void incrementGrade() {
grade++;
}
public void decrementGrade() {
grade--;
}
you have to increment the original application value .. not the copy to maintain the variable in between the activities
if (rbtn[0].getText().toString().equals("Boy")) {
grade++;
}
change to
if (rbtn[0].getText().toString().equals("Boy")) {
((MyApplication) this.getApplication()).setGrade(grade++)
}
You can add one method in application class to increment value
public class MyApplication extends Application {
private int grade=0;
public int incrementGrade() {
this.grade = grade + 1;
}
public int setGrade(int grade) {
this.grade = grade;
}
public int getGrade() {
return grade;
}
}
and increment when needed
MyApplication myApplication = ((MyApplication) this.getApplication());
myApplication.incrementGrade();
OR ================
Make that grade static and increment by accessing it in static way
public static int grade = 0;
access it lie this
MyApplication.grade ++;
You can get the result from the activities where the user enters the response and handle it from a MainActivity that manages all the responses.
Another option to avoid storing information in the Application class could be to have a Singleton with a Shared Instance that stores the global variables. However, the use of singletons is considered a bad practice in some cases.
I'm busy trying to translate some iOS code to Android code. The iOS code contains Enums, like the following:
typedef NS_OPTIONS(NSUInteger, Traits) {
TraitNumberOne = 1<<0,
TraitNumberTwo = 1<<1,
);
I have never worked with Enums before in Android, and am having trouble interpreting the documentation and examples that are available. How would I translate the above example to Android code?
use this
public enum NS_OPTIONS {
TraitNumberOne (1<<0),
TraitNumberTwo (1<<1);
private final int Option;
public int getOption()
{
return Option;
}
private NS_OPTIONS(int option) {
this.Option= option;
}
}
Use it like this:
int value = NsOptions.TraitNumberOne.getOption();
Java enums are relatively simple, but can be made more complex to fit whatever needs you want to use them for. If you just want the type-safety of an enum, you can just declare the variable names like this:
public enum Traits{
TraitNumberOne,
TraitNumberTwo
}
If you want more advanced features of an enum, it's treated exactly like a class that is instantiated statically for each item in the enum. So, you can have a constructor and input whatever value you want associated with each individual item, like so:
public enum Traits{
TraitNumberOne(0x01),
TraitNumberTwo(0x02),
// future items go here
; // don't forget the semi-colon, which indicates the list of items is ending
// now, create a private variable to store the data
private final int data;
// and the constructor to set the data
private NsOptions(int data){
this.data = data;
}
// now, you can provide an accessor to provide access to the data
public int getData(){
return this.data;
}
}
You can use the above enum like this:
Traits currentOptions = Traits.TraitNumberOne;
int optionsData = currentOptions.getData();
The idea of NS_OPTIONS is to allow all possible combinations of the enumerated values to be represented by one value (this is why bitwise operators are used). In Java, I guess the equivalent would be:
public enum Permission {
TraitNumberOne (0b01),
TraitNumberTwo (0b10);
...
}
We can implement in android like ,
public enum NS_OPTIONS{
TraitNumberOne(1),TraitNumberTwo(2);
private int type;
NS_OPTIONS(int type){
this.type = type;
}
public int getType(){
return type;
}
}
and if you want to use above enum from your class you can use it like,
int i =NS_OPTIONS.TraitNumberOne;//which will return 1
int j =NS_OPTIONS.TraitNumberTwo;//which will return 2;
In Android, we usually save all of our state variables in the onSaveInstanceState() callback by putting them in the Bundle provided.
How do people deal with saving/loading game state variables with libGDX as the classes can't use/return a Bundle object?
This is quit simple! You can use Preferences for that. You can store values in the Preferences. On android the backend uses the SharedPreferences from Android itself. On desktop its saved as an xml somewhere in the user folder.
I wrote a simple Helper to save options and get the options of my game. Here is some code out of it. (Note, dont forget to flush after saving something)
public class PreferencesHelper {
public final static String PREF_NAME_OPTION = "options";
private final static String VOLUMEN = "volumen";
private final static String VIBRATE = "vibrate";
private final static String EFFECT_VOLUMEN = "effect";
private final static String FIRST_START = "start";
private Preferences optionPref = Gdx.app.getPreferences(PREF_NAME_OPTION);;
public PreferencesHelper() {
optionPref = Gdx.app.getPreferences(PREF_NAME_OPTION);
}
public float getVolumen() {
return optionPref.getFloat(VOLUMEN);
}
public void setVolumen(float vol) {
optionPref.putFloat(VOLUMEN, vol);
optionPref.flush();
}
public boolean getVibrate() {
return optionPref.getBoolean(VIBRATE);
}
public void setVibrate(boolean vibr) {
optionPref.putBoolean(VIBRATE, vibr);
optionPref.flush();
}
public float getEffectVolumen() {
return optionPref.getFloat(EFFECT_VOLUMEN);
}
public void setEffectVolumen(float eff) {
optionPref.putFloat(EFFECT_VOLUMEN, eff);
optionPref.flush();
}
}
This is how i save my options. To save an character you do the same but save all importand stuff you need, to recreate your character when loading the game again. You can also have more than one prefrerence!
I hope this helped.
What i need to do is create an app which will generate a random mathmatical expression for the user to solve based on a difficulty level that they select.
eg. novice: a random operation on 2 terms
easy: random operations on 2 or 3 terms
What I'm struggling with is creating a class to handle the creation of expressions
My Game class is as follows:
package w1279057.CW1;
import android.app.Activity;
import android.os.Bundle;
public class Game extends Activity {
public static final String KEY_DIFFICULTY = "w1279057.CW1.difficulty";
public static final int DIFFICULTY_NOVICE = 0;
public static final int DIFFICULTY_EASY = 1;
public static final int DIFFICULTY_MEDIUM = 2;
public static final int DIFFICULTY_GURU = 3;
private int puzzle[];
#Override
public void onCreate(Bundle savedInstanceState) {
int diff = getIntent().getIntExtra(KEY_DIFFICULTY, DIFFICULTY_NOVICE);
setContentView(R.layout.gameview);
}
}
What i think i need is a class which has methods for generating a specific amount of operations randomly and then generating the random numbers for the expressions as well, once i have a valid expression i need to update a textview to display the expressions.
Am i on the right track?
I think this could be a good approach, as you will decouple the mathematical logic from your Activity. So... my advice is to go in this way :)