Android animation and performClick to create Game AI - android

I am trying to create the illusion of AI through automated clicking of buttons.
I know I can use the animation framework and use performClick on Button views, but can you recommend a way of adding a performClick() call into the animation sequence?
Thanks for any help you can give!

My solution (MVC inspired approach):
We can create a stack of pre-determined actions:
Stack<AIAction> replay = new Stack<AIAction>(); (AIAction is the model)
We can then add in each new action to our stack to be replayed later:
replay.add(new AIAction(AIAction.SELECT,...));
All the logic and decisions are done here. (controller)
After we have the set of actions, we can replay them and display the animations for the user (view):
for (int i=0;i<replay.size();i++,start+=pause()){
AIAction thisAction = replay.get(i);
switch(thisAction.getAction()){
case AIAction.SELECT:
scheduleSelect(start,...);
break;
case AIAction.REENABLE_CLICKING:
scheduleReEnableClicking(start);
break;
case AIAction.TOAST:
if (thisAction.getToast())
scheduleToast(thisAction.getMessage(),start,true);
else
scheduleToast(thisAction.getMessage(),start,false);
break;
}
}
We now need functions like scheduleSelect(), scheduleReEnableClicking(), and scheduleToast():
This gives the impression of a "thinking" AI, and allows the user to follow.
// for example:
private void scheduleSelect(int start,...){
Handler handler = null;
handler = new Handler();
handler.postDelayed(new Runnable(){
// select logic after 'start' milliseconds
...
}, start);
}
private void scheduleReEnableClicking(int start){
Handler handler = null;
handler = new Handler();
handler.postDelayed(new Runnable(){
public void run(){
// set clickable logic after 'start' milliseconds
setClickable(true);
}
}, start);
}
private void scheduleToast(final String message,int start){
Handler handler = null;
handler = new Handler();
handler.postDelayed(new Runnable(){
public void run(){
toast(message,pause());
}
}, start);
}

Related

How to create a pokemon evolution kind animation in Android

I want to create a basic "pokemon evolution" animation in my app , something like this:
I've been playing with this:
private void evolveAnimation(Pokemon selectedToEvolvePokemon) {
Drawable backgrounds[] = new Drawable[2];
Resources res = getResources();
backgrounds[0] = res.getDrawable(selectedPokemon.getImage(getContext()));
backgrounds[1] = res.getDrawable(selectedToEvolvePokemon.getImage(getContext()));
TransitionDrawable crossfader = new TransitionDrawable(backgrounds);
evolveTransition.setImageDrawable(crossfader);
crossfader.setCrossFadeEnabled(true);
crossfader.startTransition(3000); //The animation starts slow
crossfader.reverseTransition(3000);
crossfader.startTransition(2000);//It gets faster slow
crossfader.reverseTransition(2000);
crossfader.startTransition(1000); //And faster
crossfader.reverseTransition(1000);
}
However , this is not working as expected in the GIF posted below , how can I fix this?
EDIT:
I've changed the method to this:
private void doEvolve(TransitionDrawable crossfader, int durationMills) {
crossfader.startTransition(durationMills);
Handler h = new Handler(Looper.getMainLooper());
h.postDelayed(new Runnable() {
#Override
public void run() {
crossfader.reverseTransition(durationMills);
}
}, durationMills);
if (durationMills != 0) {
doEvolve(crossfader, durationMills - 100);
} else {
crossfader.startTransition(0);
}
}
However , works a bit strange.
currently you are starting all transitions at once, they should be queued
for example you may use some Handler and its postDelayed method, e.g.
crossfader.startTransition(3000);
Handler h = new Handler(Looper.getMainLooper());
h.postDelayed(new Runnable() {
#Override
public void run() {
crossfader.reverseTransition(3000);
}
}, 3000); // will start after startTransition duration
pack this code into some method which will take duration param and some callback informing that start/reverse pattern ended, then call this method again with shorter duration

How do I delay imageviews from changing

Hello I'm trying to delay imageviews to give a perception that they are being taken out one by one. I've tried
Thread.Sleep
CountDownTimer
Runnable/Handler
But they either only delay once and both imageviews change at the same time or do not delay at all. For some reference I'm trying to do something like
private void delaycard(final int Card) {
newcard(Card); //Delay this before it is called
}
Willing to try/retry anything at this point
Off the top of my head, try something like this:
Handler h = new Handler();
//for the number of images we have
for (int i = 0; i < numImages; i++) {
//we create a runnable for that action
Runnable r = new Runnable() {
#Override
public void run() {
newCard(card);
}
};
//this is the amount of time to delay it by
delay = delay + 500;
//effectively, we're creating a series of runnables at the same time
//but they activate one after the other in .5s intervals
h.postDelayed(r, delay);
}
put this inside your method
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
newCard(Card);
}
}, 100);

How to detect no touch on a view after some seconds?

I have a small view (call it "user-object") which overlap the background view (call it map). I can drag the "user-object".
I want the "user-object" automatically move to "mid-point" (screen_width/2, screen_height/2) after some seconds (for instance 4s) if three is no touch/drag.
You can send a delay(3000ms) message at the beging and move "user-object" in the Handler's callback,If "user-object" was touched,remove the message
An example using Handler
Handler mHandler;
public void useHandler() {
mHandler = new Handler();
mHandler.postDelayed(mRunnable, 1000);
}
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
// Perform this here
"user-object" automatically move to "mid-point" (screen_width/2, screen_height/2)
mHandler.postDelayed(mRunnable, 1000);
}
After a drag/touch event is triggered, again apply handler.postDelayed().

How do I start the an activity when user doesn't use it anymore

I have two activities in my app, first activity leads to second and the second leads back to the first. In the second one he can record and message and leave his name. In case the user only uses the second activity and just walks away I want that it jumps automatically to the first one after 20 seconds. Do I have to use a Handler for that? Because if I use
Handler handler = new Handler();
Runnable r = new Runnable() {
public void run() {
// my code
}
};
handler.postDelayed(r,20000);
}
});
It will change the activity independently from the user input? Any ideas? Thank you
You need to check if the user left his name in run(). If there is a name, do not execute the code that starts the other activity.
Handler handler = new Handler();
Runnable r = new Runnable() {
public void run() {
if (textViewName.getText().toString().length() == 0) {
// my code
}
}
};
handler.postDelayed(r,20000);
}
});

What code do i implement to have a time limit in my game

I'm createing a quiz that has a time limit and i dont know what to implement to have a timelimit in my level 1 class. what should i implement? can you show me a complete code?
am i correct?
private Runnable task = new Runnable() {
public void run() {
Intent intent = new Intent(getApplicationContext(),MainMenu.class);
startActivity(intent);
}
};
private void onCreate() {
Handler handler = new Handler();
handler.postDelayed(task, 60000);
There are different ways to do it. One way is to use a Runnable and a Handler.
First, define the Runnable:
private Runnable task = new Runnable() {
public void run() {
Log.i(TAG, "Time limit reached!");
// Execute code here
}
};
Then you call it (say at the start of the level, onCreate) with this Handler and postDelayed
Handler handler = new Handler();
handler.postDelayed(task, 60000);
The code within the run() method of the Runnable will execute 60 seconds after you call postDelayed
If you need regular notifications you can also use a CountDownTimer

Categories

Resources