Delay when changing screen in libGDX - java

I'm new member in libGDX . i want delay screen when changing screen
Here my code
myStage.addAction(Actions.sequence(Actions.delay(1), Actions.run(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
game.setScreen(new GameOverScreen(game,MYSCORE));
}
})));
But the game not change the screen which i want. It stop at current screen . sorry about my bad languge . How can I fix it. thanks all.

Depending on when you want it to be done, you need to call:
myStage.act();
And an example to get the output your looking for would be:
if(player.isDead()) {
myStage.act();
}
But note, this is not an effective way to transition screens.

Related

view.setOnClick() during animation not working

I'm new in android development and have now problem and can not understand why and solve it.
Sorry, here is my idea and hope it helps to understand my question:
I would like to develop an app for the kids. I have two car roads. On both roads drive different cars with a certain number (0-10). The cars drive from left to right with the help of animation. A question with a number is asked. The child has to click on a certain car with the number x.
Below I've imageview (imgCarUp1) and try to animate from left to right. During animation I want to make click-event on my image and should run some code.
`
imgCarUp1 = findViewById(R.id.imgCarUp_1);
imgCarUp1.setImageResource(vehicleUpStreetList.get(0).getResId());
imgCarUp1.setTag(vehicleUpStreetList.get(0).getResId());
imgNumberUp1.setOnClickListener(view -> {
Log.d(TAG, String.format("setOnClickListener Tag: %s", view.getTag()));
...
});
TranslateAnimation animation = new TranslateAnimation(0, width - 50, 0, 0);
Animation.AnimationListener animL = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
LogUtils.i(TAG, "onAnimationEnd is stopped!");
}
};
animation.setAnimationListener(animL);
animation.setDuration(15000);
animation.setRepeatCount(5);
animation.setRepeatMode(1);
animation.setFillAfter(true);
imgNumberUp1.startAnimation(animation);
`
Aninmation is working well. But if I try to click on imageview (imgNumberUp1) nothing will work. But if I click on area where animation was started, setOnClickListener works. I should be able to make click during animation. What is here my problem? Can someone please help with code?
Thanks a lot for helping.
Tried to search in internet, without any solution yet.
You are using Translate animation. Translate Animation translates the views position to the animated positions and wont actually change the views original position.
So you will need to change X and Y value to change the original position and then handle clicks wherever the animation is currently positioned
you can do .
view.animate().X().y().start()
while x and y will have the params of where you want to animate

How do I make a button appear and disappear?Libgdx

I want my button to appear whenever an object reaches a certain position, that object is a sprite that is generated every second:
public void create() {
if(spritePosition>700) {
buttonObj.createButton();
}
}
public void render() {
if (condition==true) {
stage.draw();
}
}
The problem is that when the games starts no Sprite is generated yet, so the result is an error. I'm also thinking of calling the createButton() method on the render method but it would generate a new button every frame because it's called constantly.
A simple way to let your button "disappear" is to just set its position to some position outside of the visible screen area.
For example something like:
buttonObj.setPosition(-1000, -1000);
To make it visible again you can just set the real coordinates again!
How about:
public void create() {
buttonObj.createButton();
buttonObj.setVisible(false);
}
public void render() {
if (condition==true) {
buttonObj.setVisible(true);
}
}
All actors in Scene2d have the setVisible method. simply try :
yourButton.setVisible(true)
or
yourButton.setVisible(false);

How to make blinking animation in LibGDX

I'm trying to create a sequence of actions to simulate eye blinking of my character but I have no idea how to properly do this. I need it to stand still for like 5 secs, then blink once and wait for 5 seconds again and loop forever. Hope someone could shed some light here.
This is what I've got so far which doesn't work to what I have expected (After 3f, it will keep blinking, how do I detect blinking animation end and reset back to stand?):
this.addAction( Actions.sequence(
Actions.run( new Runnable() {
#Override
public void run() {
stand();
}
}),
Actions.delay(.3f),
Actions.run( new Runnable() {
#Override
public void run() {
blink();
}
})));
Libgdx has a class RepeatAction, which is what you are looking for.
Basicly you need to call:
this.addAction(Actions.forever(Actions.sequence(
Actions.run(new Runnable {
#Override
public void run() {
stand();
}
}),
Actions.delay(0.3f),
Actions.run(new Runnable {
#Override
public void run() {
blink();
}
});
)));
But instead of using new Runnable you may use one of the methods provided by Libgdx Actions.
So for example, the stand(), isn't it only "do nothing"? This can be achieved with an Actions.delay(5f), which waits for 5 seconds.
And the blink() isn't it only switching from "visible" to "invisible"?
This could be don with Actions.alpha(0, 0.2f), which changes the characters transparency from its current to 0 in 0.2 seconds. Then you could add another delay to let the character "wait" in the invisible state and make it visible again with Actions.alpha(1, 0.2f).
Hope it helps.

What's the right place to dispose a libgdx Screen

Hello I am working out a game and I wonder how to dispose resources as I am experiencing memory problems.
I have something like this:
public SplashScreen implements Screen {
#Override
public void render(float delta) {
}
#Override
public void dispose() {
if (batch != null)
batch.dispose();
batch = null;
}
}
public MapScreen implements Screen {
#Override
public void render(float delta) {
}
#Override
public void show() {
splashScreenInstance.dispose();
}
#Override
public void dispose() {
if (mesh != null)
mesh.dispose();
mesh = null;
}
}
And I am disposing the splash screen as soon as the show method of MapScreen is called. Previously I'd settled the screen to the MapScree. Still the render method of the splashScreenInstance is called and I'd received null pointer exceptions. Why this is so?
I'd expect that once I set another screen, the previous one is no longer rendered. This is not seemingly so. I'd tried disposing right after setting the screen using the game instance, right after the hide method is called on the screen I want to dispose and finally on the show method of the next screen. All of these cases still renders the previous screen a few times before rendering the current one.
I really need to recover memory and also I don't want to test each time (On the render method) for null pointers as this has performance penalties.
Any suggestions?
Thanks
This is how I usually handle this problem:
public SplashScreen implements Screen {
#Override
public void render(float delta) {
// I assume that you have a reference to the game somewhere to switch the screen
game.setScreen(new MapScreen());
dispose();
return;
}
}
I first set the new Screen, then dispose() the current one and then immediately stop any further execution of the current screen via return. This way the current render cycle should be stopped and in the next cycle the render() of your next screen will be called.
Another approach might be to call dispose() in your hide() method of the Screens, because that is going to be the last method being called before the Game will use the next screen. This is especially useful when there could be several different next screens. In that case there will still be only a single place of dispose() and that will be in the hide() method.
Where are you calling setScreen? Since everything should be happening in the rendering thread (even InputListeners) you should be able to call setScreen in your first Screen and then return from the render method. The Game instance will automatically call hide on your first Screen which is where you can call dispose.

Java Timer not working

I have an Image named worldImageToUse and I have a Timer that is supposed to toggle worldImageToUse between two images every 1 second. But it does not seem to work. Help Please?
public void startWorldImageFlash() {
worldImageFlashTimer = new Timer();
TimerTask task = new TimerTask() {
#Override
public void run() {
if(worldImageToUse == worldImage) setWorldImageBW();
if(worldImageToUse == worldImageBW) setWorldImageColor();
}
};
worldImageFlashTimer.scheduleAtFixedRate(task, 0, 1000);
}
public void stopWorldImageFlash() {
worldImageFlashTimer.cancel();
setWorldImageColor();
}
Checked twice, change the second if with "else if", that will solve the problem. Also, you should consider debugging in such cases :)
It looks like your code says if color set to black and white. Then says if black and white set to color. Wouldn't you end up with the same image every time. Your second if needs to be an else if.
Did you repaint() the component afer setting the image?

Categories

Resources