I have developed a game in android but now I want to make the gameplay or game screen in LibGdx as I am familiar with it. So, can I make game screen in LibGdx and use splash, login, setting screen etc. which are made in android ?
If Yes, Then how can I do so?
Let me answer your question again. You don't have to use core or desktop folders. You can code everything for Android folder with Ads, Play Services, etc. if you wish.
Let me describe how you do it.
Create a class that extends AndroidApplication this is your MainActivity class where everything starts.You add a game view and ad view to this class game view class extends Game class on create() method of AndroidApplication class.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
// create view that contains game
View viewGame = initializeForView(new MyGame(this), config);
// create ads
setUpAds();
layoutMain = new RelativeLayout(this);
// add game view
layoutMain.addView(viewGame, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
setContentView(layoutMain);
}
You set a class that extends ScreenAdapter insde create() method
of your MyGame class. and send instance of MyGame class to screen
classes as parameter to change screen using Game instance and
everything inside classes like Splash, Game, Setting or anything
else is coded in Java(Android) and you can easily change screens
using game.setScreen((new MainMenuScreen(game))); That's all the
logic.
Related
I have a running Android java written application and I would like to improve it.
For know my mainactivity, called DrugListActivty extend AppCompat. In short it's a recyclerView with Room database management.
I would like to move some part of the code in a MainActivity, in charge of checking database or onPause for example.
For now (and it's not working) I made :
public class MainActivity extends AppCompatActivity {
//some stuff
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, DrugListActivity.class);
startActivity(intent);
}
And DrugListActivity became
public class DrugListActivity extends MainActivity {
//etc
I have blink effect, DrugListActivty is launch again and again and again.
Is my idea bad (move to simply code)?
If is a good idea, there must be a right way to do it but I can't find any help.
So I'm asking the experts.
Thanks in advance for reading me, your advises and your time.
Found a strange issue in the app.
Toolbar style changes itself just for one acitvity in whole app and only on devices below 5.0 Android version.
All the screens have the same <include> field for the toolbar.
The activities, where style changes, are all inflated with a static method:
public static void startAsRecent(Context context, TransferTemplate template) {
Intent starter = new Intent(context, TransferAnotherAccountActivity.class);
starter.putExtra(TransferCommonActivity.EXTRA_KEY_TEMPLATE, template);
starter.putExtra(TransferCommonActivity.EXTRA_KEY_IS_RECENT, true);
context.startActivity(starter);
}
Strange, that it works normail in devices with Android versions abowe 5.0.
What could be the reason of this kind of behavior? And where to look to fix this?
Thanks in advance!
Ok, the problem was in inheritance.
onCreate() method of inherited activities, where was the problem, called setContentView() first, and then super.OnCreate(). Still don't understand, why this affected only pre-lollipop devices, but the temporary solution is to make something like this in the superclass.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewId());
//other stuff
}
#LayoutRes
public abstract int getContentViewId();
I'm working on a game for android, using libgdx and IntelliJ. In this game I have two 'screens', these screens use the same texture for their background, this is how I load them:
Texture backgroundTexture;
public static Sprite backgroundSprite;
backgroundTexture = new Texture("textures/background.png");
backgroundSprite = new Sprite(backgroundTexture);
This is done in both screens, so my question is, can I load these texture in another class, then use them in both screens somehow, I feel like that would be the way to do it, am I correct? If I'm on the right track, how should it be implemented?
You can use AssetLoader on your own way to load assets. It's nothing special just a class, which is called when the application starts, because the fact is that you should avoid to load assets, when everything is running. It would be just a simple class with static things.
public class AssetLoader {
public static Texture myBackgroundTexture;
public static void Load() {
myBackgroundTexture = new Texture("mybgs/my_bg_texture.png");
}
Let's call AssetLoader.Load() when the application start, and you can reference everywhere to your things like:
Texture thisScreenBg = AssetLoader.myBackgroundTexture;
I'm implementing GCM for the first time and the sample app on google provides DemoActivity which deals with GCM functionality. (http://developer.android.com/google/gcm/client.html)
I can copy those gcm related codes over to my MainActivity, but I 'd like to keep things separate, ie. create a separate file for gcm and let MainActivity use it.
In python world, mixin would be great fit here.
But I'm not sure if mixin exists for java and if its the right tool here.
How would one implement the GCM functionality in a separate class in java?
I'm thinking something like the following.
Create GcmHelper.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
-->
public void onCreate(Bundle savedInstanceState, Activity activity) {
context = getApplicationContext();
this.mActivity = activity;
MainActivity::onCreate creates GcmHelper() and calls GcmHelper.onCreate(bundle, this)
do make similar changes for onResume() and activity related code to use the handed-over activity.
Create a Seperate Class eg. GcmHelper
Make GcmHelper constructor using Context parameter
Copy Every GCM related functions to GcmHelper
Create object of GcmHelper by passing the context in MainActivity->OnCreate
Finally call the required methods...
I've been thinking for hours about this problem now without finding a solution. Still I think I'm just overlooking some very simple things.
My program is (mainly) supposed to record and playback audio, so I got one Main-Activity which contains all the functions with interactivity to the user and I got one class I called 'AudioHandler' to manage all the audio stuff in the background.
The Main-Activity uses an instance of AudioHandler.
Within the AudioHandler-class I got an OnCompletionListener, to notice when playback of the recorded audio-file has finished.
The problem is: When playback has finished - and the OnCompletionListener gets called, I want to change an ImageView (Turn the pause-button into a play-button) in the Main-Activity.
How can I access the ImageView in the Main-Activity from the AudioHandler-Instance?
...without making the ImageView-Variable public.
Method that calls play from the AudioHandler in the Main-Activity:
public State playRecording() {
//change play-button to stop-button
iPlay.setImageResource(R.drawable.stop_button_active);
//start or resume playback of the recorded Audio
ah.play();
//Return that the program is in playing-mode
return State.PLAYING;
}
play-function in AudioHandler
public void play() {
try {
mPlayer.start();
} catch (IllegalArgumentException e) {
Log.d("MEDIA_PLAYER", e.getMessage());
e.printStackTrace();
} catch (IllegalStateException e) {
Log.d("MEDIA_PLAYER", e.getMessage());
e.printStackTrace();
}
//add listener to notice when the end of the audio record has been reached
mPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//should somehow change the ImageView 'iPlay' in the Main-Activity when reaching this point.
}
});
}
Any help is appreciated :)
Marco
If you need to gain access to a variable, that variable should be either package-accessible or public. Or, even better, you should have an encapsulating method. Something like:
// On the main class:
public ImageView getIPlay(){
return iPlay;
}
private static MainActivity instance;
public MainActivity getInstance(){
return instance;
}
/** Either on the constructor or the 'OnCreate' method, you should add: */
instance = this;
Now you can access the ImageView from any Activity or class with:
MainActivity.getInstance().getIPlay();
It's that simple, really. You can go through a lot of other solutions, from using reflection through AOP or any kind of things. But it's just making the issue more complex. Try creating the setter method.
Your question is about object encapsulation.
When you define an instance variable as private, you are saying that only that object and members of that class can access the variable. This is a good thing because it protects you from accidentally changing data you did not want to. This is also why we have getters and setters.
So in your class you have something I imagine looks like this
public class Main {
... some code here
private ImageView myView;
... more code here
what you need to do is add the following methods to your Main class
public ImageView getView() {
return myView;
}
and
public void setView(ImageView a) {
myView = a;
}
so now when you want to access the ImageView from another class, you just call something like Main.getView() and you'll have it.
Now if this doesn't answer your question, it's because you didn't design your app too thoughtfully (so it seems) but luckily I won't be the kind of responder who tells you to uproot everything you have done and start over.
It sounds like you're working with an Android app and your issue is to communicate between activities.
This is why Android has something called Intents. You can send an Intent from one activity to another to carry data. So in this case you would send an Intent from one activity to the main class that would tell the main class to change the picture in the imageview. I won't lie, I suck at using intents but there are many wonderful tutorials online that will show you how to do this.
Another option would be to create a RemoteView of your layout that has the play/pause button and access it from the other activity, RemoteView has a built in function to change the bitmap of a remote imageview (again, see the Android Developer Documentation for RemoteView for more on this)
I'm sorry I couldn't give you some actual code, I'm not confident enough in my abilities on these topics but I'm 100% confident that one of the three methods I just listed will answer your question.
You could create a public wrapper method within your main Activity that toggles the ImageView between Pause and Play.
Or, you could pass the the ImageView to the AudioHandler (via the constructor?) and just manipulate it there.