Regulate speed of game with speed of screen touches - java

I am creating a game where the user must tap the screen faster to allow them to go faster in game, however I have encountered some problems. I have made the game so it calculates the amount of taps per minute from the users last tap and their current tap, however this method seems to be jerky and does not work too well. I also cannot work out another way to do it, so that the when they don't tap at all, it slows down.
How would I go about creating this mechanism for the game where the faster the user taps, the faster the game goes, and if they don't tap at all it slows to a stop.
Thanks
Here is a snippet of what I have so far, calculating the Taps Per Minute (TPM) of the user. (This is in the on touch method). I am currently passing the TPM into an update method which moves the background by TPM/100 each time it updates (it's running at 30FPS)
if (thisTouch == 0 || lastTouch == 0) {
Log.d("info","First Touch");
thisTouch = lastTouch = System.currentTimeMillis();
} else {
Log.d("d","touch");
thisTouch = System.currentTimeMillis();
long difference = thisTouch - lastTouch;
TPM = (int)((1000*60)/difference);
lastTouch = thisTouch;
}
I would like to know how I can change this so that the game speeds up when they click faster, and then slows when they do not touch the screen.

Maybe you could do something like this:
Create a PriorityQueue< Long>
Whenever the user taps the screen, add (currentTime + someTimeOut) to the queue
Whenever you are going to check the current speed (eg size of queue), first remove all elements > currentTime.
Don't forget to add appropriate thread safety measures if you do something like this of course.

Not to be "that guy" but it sounds like your current plan of taps per minute is pretty good but just implemented badly. However, I would strongly recommend a smaller time quantity such as taps per three seconds since games are usually fast paced and having to wait a whole minute to see your character speed up or slow down may produce a sense of delayed lag.
Alternatively, you could could use some form of acceleration that is increased up to a maximum limit every time the screen is tapped and decreases over the time as the screen is not tapped.
Now that I see you updated your question with some code, I'd like to point out that a tap is a touch and release from the screen not necessarily a touch. I honestly don't know from your code whether or not you're compensating for this so this may be the source of some of your problems.
Lastly, for game development algorithms and theory you may want to see this StackExchange site: https://gamedev.stackexchange.com/
It sounds like you're very close to getting your game to feel the way you want it to, good luck!

Your general approach is pretty much the only way to do it, it just needs some fine tuning.
Keep a general TapsPerTime variable. With each new tap you register, do not replace the TapsPerTime, but calculate a new, weighted value:
TapsPerTime = (TapsPerTime * X) + (MeasureTapsPerTime * (1-X))
The factor X you need to find empirically (by trying out what works best for you). The general idea here is that each measurement will only nudge the used value a bit into the right direction.
Also, if you do not detect a tap for a set amount of time, simply update the TapsPerTime with the same formula, but use 0 (zero) for MeasureTapsPerTime. So if the user does not do anything the value goes down.

Related

Initial display of Swing compoenent is slow

The first time I show my Component, it takes a few seconds to show. I am using a card layout. When I switch to a card for the first time is when I experience the slowness. Every time after that, it is almost instant.
I have two questions about this:
For Swing components, is there a way to kick this off early, for example, during application load?
What is happening under the hood that causes the first display to take longer than all the others.

Running a program logic within a program

Note: This thread is not about launching a compiled program from within another program. But rather about running another program logic in parallel to the main program.
In libGDX, a game is handled with screen. A screen has a specific kind of display and input. For example: Menu Screen, Game Screen and option screen.
In most basic game switching game screen is not really a problem. You know that from menu you can go to game or option and you know that when you finish option and game, you get back to menu. So you can actually hard code the destination screen in those class, it's like a "Goto Menu Screen".
One of my screen will be composed of sub-screen which is the same logic as screen except that it stacks multiple sub-screen displayed when that screen is active. Now I am trying to make some sort of hybrid video/board game engine. One of the sub screen could be "Chose a card from your hand", now in that case, there could be multiple situations where a card could be played from your hand. Which mean that when the card is chosen you actually don't know where to go next. For example, let's take that turn order sequence:
Draw a card
Play a card from your hand
Place that card into play
Play a card from your hand
Place that card in the discard pile
Now, you want to reuse the code of "play a card from your hand" to make sure you do not code the same thing twice. But here, "Play a card from your hand" don't know if it should next play the selected card into play or place the selected card into the discard pile. So it cannot use the "GoTo" method.
In fact I will need to have some sort of "Program" to change screen that would map the procedure of the game play. The game would first draw a card, then as the player to chose a card, then use the return value and place that card into play, etc. That works well in a regular program, but that seems harder to do in Libgdx, because the framework is real time. So after each executed command, I need to return the handle of the program to libgdx so that he can do proper animations and rendering. Else the program will simply freeze and wait for user input which will never work. So I need to run a program in parallel of the libgdx program that will tell libgdx which sub-screen to display according to the logic of the game.
So far, I found 2 solutions, but I was wondering if there was something else that I could use.
The first un-elegant solution is an instruction pointer. Like the processor is doing in assembly, remember the ID of the next instruction to run, have a huge switch case that will list all possible instruction. Each time render is called, it will check if the subscreen is finished, if yes, it will execute the next instruction. It could look to something like this:
Note: the code is not perfect, it's just for the illustration.
static int IP = 1; // instruction pointer
if ( subscreen_finished == true )
switch ( IP )
{
case 1:
hand.draw_card();
IP = 2;
break;
case 2:
hand.chose_card();
IP = 3;
break;
case 3:
play_card ( hand.getcard());
IP = 4;
break;
case 4:
hand.chose_card();
IP=5;
break;
case 5:
hand.discard ( hand.getcard() );
IP=6
break;
case 6:
end_of_turn();
break;
}
Each function call would set subscreen_finished to false. It basically wait for the user input, or wait for the animation to be completed before setting it to true.
As you can see, it's really not elegant and it could be very hard to follow the program especially if you want loops like " do 3 actions".
The second method consist in running a parallel thread that could work like this
hand.draw_card();
hand.chose_card();
hand.play_card ( hand.getcard );
hand.chose_card();
hand.discard ( hand.getcard );
end_of_turn();
Each function call would setup the right subscreen and enter a waiting loop until an answer is chosen by the player or the animations are completed. Looks much more elegant, but I must use multi-threading.
On another thread somebody suggested using state machine and messaging.
But the state machine does not quite work for me. Because in my example above, if the game is at the "chose a card from hand" state, in order to know which state to do next, it needs to know all the previous steps that were accomplished. Or know that we are at step 4 and that the next step is 5 because "chose card" could be called at step 2 or 4.
Is there any other solution available? It's really hard to find documentation or other implementation of something similar, because most application are not real time like it's the case of video games.

ItemAnimator how to time animation to start one after the other

I am using a RecyclerView to display data that gets polled from a website (=> completely changes at once).
I already created an ItemAnimator class that has the animation I want but I need to know what is the best way to time the animations to wait for the previous one to finish.
This is what I'm trying to achieve: http://www.google.com/design/spec/animation/meaningful-transitions.html#meaningful-transitions-hierarchical-timing
We've had a lot of success using setStartDelay (or setStartOffset, depending on which animators you are using). Have a delay variable that starts at 0, and as you walk through your children creating their animations, set the start delay to the current value and add some increment, such as 100 ms.
I've been working on the exact same animation. I've put it to the side for the time being, but got a pretty good effect at normal speed. When I slowed it down for debugging, I've got a strange bug that causes certain items to be animated multiple times. Also, scrolling up doesn't quite work if the animation hasn't completed, but that isn't a bug likely to be run into at normal speed.
Here's my code: https://github.com/halfjew22/AnimateRecyclerGrid
Essentially, what I did was I looked at the pattern in the Material Animation. Based off of that pattern, I put together a for loop that spawns a runnable that is given coordinates (based off the value in the for loop) and told to animate the view with those coordinates.
Like I said, I didn't quite finish the project, but it works fairly well as an alpha or proof of concept. I know it's been a while since you asked this question, but let me know if you'd like to work on finishing this up together.
Let me know if that helps you out.
I've created a custom RecyclerView adapter class which does more or less what OP wanted to achieve i.e. animates RecyclerView's items sequentially (in order, one after the other).
The adapter class is shared publicly on github as a gist here.
From the description:
"This adapter aims to create a sequential RecyclerView items' animation.
They're appearing in order, top to bottom. The animation works when the RecyclerView is first created and does NOT work when items are scrolled down."

Changing color of a button too slow

I'm currently making a game of Reversi for my programming class and I wanted to try and implement a computer into the game. I got this working successfully, but the computer makes their move instantly so you can't see how your move worked.
I tried solving this by using the following method:
public void wait (int n) {
long t0, t1;
t0 = System.currentTimeMillis();
do {
t1 = System.currentTimeMillis();
} while ((t1-t0) < n);
}
I then called this method after the player made their move, right before it called my computerAI() method.
However, it didn't work quite right and it seems like it hit the wait before it changes the colors of the board, because the player clicks, the background changes to the default light gray, and then after it waits a second, it performs the color changes of your move and the computers.
Anyone have any suggestions for fixing this?
Use:
Thread.sleep(5000);
This causes the current thread to sleep for 5 seconds.
You may need not only change button's color property, but also call its .repaint() or even .update() method often enough, e.g. every 100 ms. That is, you need to make the button actually redraw itself on the screen when you need it, not when the system determines it has time to render the accumulated changes.

Echo3 Framework - problem setting focus elemnt to text box when screen is built

in echo 3 i have a problem setting focus on a specific text field in a new screen. The probelm occurs when a user holds their mouse on the reference button on the previous screen as opposed to just a simple click.
it looks similar to this:
public void display screen {
build window
if window isnt null{
build screen
if screen.textfield isnt null{
Thread t {
thread sleep 10000
screen.textfield.setFocus
}
}
}
}
in the pseudo above the focus would be set if the user user held the reference button down on the screen before for less than 10 seconds, in which case the focus would not be set until the remaining thread time passed. this isnt good because it take too long; and lower wait delay doesnt insure that the focus will set at all because the user might hold the key for longer.
I have tried launching multiple threads and using timers to hammer the focus in but that didnt work... is there something im missing about how the code is built internally because it seems that the whole thing is built despite the fact that the user hasnt let go of the button.
If thats the case is there a way to do it on release?
Thank You
Found a solution. The problem was with using IE6. I presume the order in which it builds is different to that of IE7+.

Categories

Resources