First of all, i'm trying to make a puzzle game with the same concept as this game,
LINK: http://www.youtube.com/watch?v=-b2LunJPXC0 (the title of the game is Siren Fantasia)
Where in the user will hold a single piece of item from the gameboard and the objects that is surrounding it will be rotated clockwise until the user releases it.
I want to imitate that, When the user holds the button, the action will continuously loop until the user releases it.
Heres a sample of the code that i have already made:
float delay = 2f;
button.addListener(new ClickListener() {
#Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
if(pointer == 0) {
Timer.schedule(new Task(){
#Override
public void run() {
int counter = 0;
do{
Timer.schedule(new Task() {
#Override
public void run() {
button2.setVisible(false);
}
}, delay);
Timer.schedule(new Task() {
#Override
public void run() {
button2.setVisible(true);
}
}, delay);
}while( counter >= 1 );
}
}, buttonDelay);
}
return false;
}
});
And the output ain't the one that i wanted it to be. When i hold button, the button2 doesn't set to visible(false) and visible(true). But when i kept holding it over and over again, it would sometimes work.
I really need help.
Thank you.
Related
I am trying to make a keyboard and everything works fine. I just want to break the if statement after each click to stop repetitiveness of the char. I don't want to use a Boolean because user can enter same character more than once. This is the image listener
a = new Image(new Texture("Sprites/Keyboard/a.png"));
a.addListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
apressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
apressed= false;
}
});
And this is what i have in my update inside the activity or state.
#Override
public void handleInput() {
if (keyboard.apressed) {
builder.append('A');
currentWord = builder.toString();
//break; here doesn't work. says its outside the loop.
}
}
So in theory this is what i want but can't use because im guessing its to much work for every char?
#Override
public void handleInput() {
if (keyboard.apressed) {
builder.append('A');
currentWord = builder.toString();
try {
TimeUnit.MILLISECONDS.sleep(400);
} catch (Exception e) {
System.out.println("error");
}
}
any ideas? thanks in advance.
I made my own keyboard as well using LibGDX TextButtons. Since you are using images and not text, you could use the ImageButton instead. I then used the ChangeListener with the changed() method because it only triggers after pressing and releasing the button, which avoids repetitive characters while the button is being pressed.
keyButton.addCaptureListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor){
// key logic here
Im really new to Java and Libgdx
I have created an interface entitled LevelQuizDialog in which the buttons will fire an event, and I want to have a 3 second delay before firing that event.
here's the code:
//fire event on button click
WrongBtn.addListener(new ClickListener(){
#Override
public void clicked(InputEvent event, float x, float y) {
CheckBtn.clearListeners();
if(checker != answer )
{
removeActor(title);
addActor(title2);
}
if(checker == answer)
{
removeActor(title);
addActor(title3);
}
// this is the delay I had a problem with...
float delay = 3; // seconds
Timer.schedule(new Task(){
#Override
public void run() {
fire(new MessageEvent(ON_CLOSE));
}
}, delay);
}
});
}
I had followed a lot of tutorials in com.badlogic.gdx.utils.Timer
but I had a lot of errors..
I got an error in the Timer.schedule(new Task()
saying Timer is not applicable for arguments
thanks for anyone who can help.
A portable LibGDX solution could be to use gdx-ai library which comes with a Message api that allow you to post delayed messages using a specialized event bus :
https://github.com/libgdx/gdx-ai/wiki/Message-Handling#dispatching-a-message
messageDispatcher.dispatchMessage(
delay, // Immediate message if <= 0; delayed otherwise
sender, // It can be null
recipient, // It can be null, see the "Multiple Recipients" section below
messageType, // Any user-defined int code
extraInfo, // Optional data accompanying the message; it can be null
needsReturnReceipt); // Whether the sender needs the return receipt or not
may be this can help
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
fire(new MessageEvent(ON_CLOSE));
}
}, delayMillis);
I'm writing a "space invaders" style app and I needed to do the move stop repeat animation that is classic to the game. I got the processes to go one way, using the code below, however when I want to go back, there does not seem to be an easy way to do this. I think the easiest way to do it is to have something happen on the end animation event, however I cannot get the event to trigger, even when calling super.end();. I've looked around for a way to trigger the event manually but to no avail. The way I am doing this may be a little sketchy but it works well enough right now to try and make it work all the way.
public void start()
{
if(begin < end) //recursive end condition
{
int distance = interval + begin; //change the distance to be traveled
//create new animation to do part of the whole animation
ObjectAnimator anim = ObjectAnimator.ofFloat(toAnimate, property, begin,distance);
TimeInterpolator inter = new TimeInterpolator() //makes the animation move with only one frame
{
public float getInterpolation(float prog)
{
return Math.round(prog * 10) / 10;
}
};
anim.setInterpolator(inter);
anim.setDuration((long)(500));
anim.addListener(new AnimatorListener()
{
#Override
public void onAnimationStart(Animator animation){}
#Override
public void onAnimationEnd(Animator animation)
{
start(); //start the next part of the movement
}
#Override
public void onAnimationCancel(Animator animation){}
#Override
public void onAnimationRepeat(Animator animation){}
});
begin = begin + interval; //update end recursion value
anim.start(); //begin the animation
}
super.end(); //this doesn't work... rip
}
public void start()
{
if(begin < end) //recursive end condition
{
int distance = interval + begin; //change the distance to be traveled
//create new animation to do part of the whole animation
ObjectAnimator anim = ObjectAnimator.ofFloat(toAnimate, property, begin,distance);
TimeInterpolator inter = new TimeInterpolator() //makes the animation move with only one frame
{
public float getInterpolation(float prog)
{
return Math.round(prog * 10) / 10;
}
};
anim.setInterpolator(inter);
anim.setDuration((long)(500));
anim.addListener(new AnimatorListener()
{
#Override
public void onAnimationStart(Animator animation){}
#Override
public void onAnimationEnd(Animator animation)
{
begin += interval;
if(begin < end){
start(); //start the next part of the movement
}
else{
// Do something else
}
}
#Override
public void onAnimationCancel(Animator animation){}
#Override
public void onAnimationRepeat(Animator animation){}
});
anim.start(); //begin the animation
}
}
I'm developing a game where LittleMan can move from block to block, and certain blocks are moving. I'm trying to detect when he moves to a new moving block, if that block is beneath him or if it has moved. I'm using Andengine's TimerHandler to check every 0.1 seconds but it is not working. Here's the code:
private void OnMovingBlock(final int CurrentPosRow, final int CurrentPosColumn) {
this.getEngine().registerUpdateHandler(new TimerHandler(0.1f, true, new ITimerCallback() {
#Override
public void onTimePassed(final TimerHandler pTimerHandler) {
if (LittleManPos[0] == CurrentPosRow && LittleManPos[1] == CurrentPosColumn) {
if (!LittleMan.collidesWith(MapRectangles[CurrentPosRow][CurrentPosColumn])) {
RestartScene();
}
}
}
}));
}
It seems he can move to the block and sit there with it moving in and out beneath him but it doesn't call RestartScene() UNTIL I move him again. Any idea where I am going wrong? Or is there another way to do this?
Why don't create a Timer?
TimerTask task = new TimerTask(){
#Override
public void run(){
// your code goes here
}
};
Timer timer = new Timer();
timer.sechedule(task, 0, 100); // 100 --> 0.1 second
To cancel the Timer, call timer.cancel();.
how do call fire method in every five seconds with libGdx ?
this is my Enemy class :
I wrote the following code, but it is wrong.
public class Enemy {
private List<Bullet> bullets;
private boolean isFire;
public Enemy(){
bullets=new ArrayList<Bullet>();
}
public void update(float delta) {
Gdx.app.log("State", "State Fire");
if(!isFire){
Gdx.app.log("State", "Fire");
fire();
}else{
Gdx.app.log("State", "No Fire");
}
}
private void fire() {
isFire=true;
bullets.add(new Bullet(5, 32));
Timer.schedule(new Task(){
#Override
public void run() {
reload();
}
}, getDelay());
}
private void reload(){
isFire=false;
}
private int getDelay() {
return 5;
}
public List<Bullet> getBullets(){
return bullets;
}
}
Is there a way to solve the problem? I have no idea
I can think of two solutions for that:
1) the render method gets executed in every X milliseconds. You can use that one.
__UPDATE__
Below code is from here
void render(float delta)
Called when the screen should render itself.
Parameters:
delta - The time in seconds since the last render.
in that method, you can add delta to figure out if it has been X milliseconds since the last time you did whatever you want to do. If so, you can do it again. If not just increment your counter. Here is what I mean.
double counter = 0;
void render(float delta){
if (counter >= 5.0){
counter = 0.0;
fireUpYourTimelyProcessHere();
} else {
counter = counter + delta;
}
...
// other rendering stuff
}
However Do not do things that take a long time to finish here, like making an HTTP request or something.
}
2) Or you can create start a regular thread and start it. But keep in mind to avoid any UI rendering on that thread.