Getting continuous input from a button in unity2d - android

What is the equivalent of GetKey for buttons. In my android game when user press and holds the button , i want player to move right but it only moves for one time when i press the button.But when i get input from a keyboard with GetKey function , it moves until i stop holding that button. I want to use my button like GetKey.I tried to add a event trigger pointer down component into my button but it is not working.What should i do?
public void moveRight(){
rigidbody2D.velocity = new Vector2(moveSpeed,rigidbody2D.velocity.y);
}
void Update () {
if(Input.GetKeyDown (KeyCode.Space)){
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpHigh);
}
if (Input.GetKey (KeyCode.A)) {
rigidbody2D.velocity = new Vector2(-moveSpeed,rigidbody2D.velocity.y);
}
if (Input.GetKey (KeyCode.D)) {
rigidbody2D.velocity = new Vector2(moveSpeed,rigidbody2D.velocity.y);
}
}
they are absolutely same but i don't understand why its not working continously.
Update method is the key, i know this but i don't know how to get information about is a button pressed in Update method.Stuck help.

Well, we have two different parts of Unity3D API here.
Let's split it up into pieces:
Input.GetKey
As stated in API docs, Input.GetKey just checks if certain key is pressed:
Returns true while the user holds down the key identified by name. Think auto fire.
If you check it every frame, (Update() is executed every frame) you re-check if button is pressed and if so, proceed with velocity change.
EventTrigger
EventTriggers are a little different. They actually bind a delegate (UnityAction to be precise) you pass to them and trigger it as callbacks on given EventTriggerType fired. So if you bind to OnPointerDown() with your moveRight(), it will fire when pointer goes down over GameObject. If you keep the button pressed, it won't fire again because actual event of pointer going down has already happened and it already fired appropriate trigger you passed to it. Pointer is actually not going down right now. It's pressed.
It all goes down to the fact that one part checks every frame if button is pressed while the other one fires when button press happens.

Related

How to keep validation over this cart button?

Kindly check this link , in this on click of cart button ,it shows animation that its added to cart , but i want to keep a validation over this button ,then only it should get added to the cart . Please help me with this code
https://github.com/truizlop/FABRevealLayout
You've got two possibilities:
1) Override the clickListener from the library by registering your own at the FloatingActionButton using fabButton.setOnClickListener(myListener). Make sure you're doing that after the fabRevealLayout has been inflated and initialized. In onCreate() after calling through to super.onCreate() should be fine.
Inside your own clickListener, you perform your validation and based on the result manually trigger the revealing of what the library considers the "secondary view" (fabRevealLayout.revealSecondaryView();) or do not.
2) Register a onTouch listener to the fab button. Inside that touch-listener you check whether it is an ACTION_DOWN event and then do your validation. If it succeeds, return true to notify the system, that the event has not been handled by your functionality (that it has "consumed" the event). Downside: No click sound when pressing among others, so "not the nice way", as we're interfering the android touch handling. On the other hand you do not have to manipulate the third party library.
I would recommend going with the first option.

close a slider after an intent is launched

I have an activity, DogActivity, with a slider. When the user slides view PawsView to a certain degree, I start another activity, CatActivity, using startActivity(intent). If the user clicks the back button, normally the user returns to DogActivity. Here is my problem: if in Developer options I set Do not keep activities then when the user clicks the back button and thus returns to DogActivity, the slider is not asserted and so PawsView is back to its original position; however, if I don't have that option selected, upon returning to DogActivity the slider is still asserted (the sliding already occurred).
Since I don't want to depend on the user selecting or deselecting Do not keep activities, I need to do this programmatically. So does anyone know how to do this? I have tried putting the appropriate code inside onResume but that has no effect. It's as if finishing CatActivity has no effect on DogActivity. BTW, the view I am using to display PawsView is a custom view.
I already tried using a handler with postDelayed to pull PawsView back to normal, but the handler always executes before the startActivity is executed. If on the other hand I start a proper Thread to run the call to close the slider, I get an error about the wrong thread trying to change the layout.
Another way of asking the question may be: How do I force onResume to be called even when Do not keep activities is NOT selected on a user's device.
You could try to launch CatActivity using startActivityForResult and then handle the result in onActivityResult and do the necessary setup from there. It's sort of like forcing onResume.

Button stays disabled until second click?

Good Morning,
I have a list of items with a prev. & next button. When the user is at the start of the list the prev button is disabled. Clicking next takes them to the next record and my click handler sets the prev button enabled true. However in the emulator it doesn't show the button enabled. Clicking next moves me to the third record and again the handler sets the prev button enabled but this time it does become enabled in the emulator. I'm grasping at straw here but do I need to invalidate and redraw or something?? I don't understand why such an elementary task is not working.
In XML:
<Button
android:id="#+id/btn_PrevLift"
...
android:enabled="false"
android:onClick="btn_PrevLiftClick" />
In the handler code:
private void UpdateNavButtonStatus(int z)
{
...
btn_Next.setEnabled(true);
btn_Prev.setEnabled(true);
....
}
No just to show you how little I know about what I'm doing how come when I look at the variable values in Eclipse debug I can't see the enabled property in any state???
More Info
Very odd to me at least. If I move from using XML defined event handlers to programatically defined as below it works great!!!???
btn_Nxt.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//Call helper methods etc...
}
});
I think I have it but not sure exactly why
So when I was trying to get a handle on my Button objects I was using the View.findViewById(etc). When I changed from XML to the programatically declared event handler I used ViewGroup.findViewById. Reverting back to xml if I use the ViewGroup I get a "different" handle that seems to work...????
Ok,so what you can do is declare a variable count and on the click f button next increment the value of count..and on the click of Back button decrement the value of count and give the condition that while count<0 the task of the back button is done else nothing
I have identified the problem and of course the problem is me. I was enabling the button and then the ListView was updating to the next record. Of course my enabled button was one behind so it appeared that when I clicked a second time the button suddenly enable but that is not the case. When I clicked a second time we moved to record three and showed button for record two which was enabled.
See my buttons were part of my ListView and new buttons were being drawn for each record. OOHHHH it makes so much sense now how everything was behaving.
Anyway I moved the buttons off the ListView layout so they remain constant as the user navigates through the records.

back button return the application to inconsistent state

Today I was testing my app (wich is a very simple note application) but some strange behaviour occur .
The app consists of a big ListView which displays all the notes and a button to make a new note (activity A).
When I press the button to "Add new note",it takes me to the activity B
(with all the fields like title and text).
When I press "Add" ,it take me back to the activity A and displays ok
the new Note .
The thing is that I started pressing BACK and it began to take me "back in time" where some notes were still not created,until it reached the time they were 0 notes .
Since I am new to Android devel ,is this normal or is it something wrong with my app ?
When you go from one activity to another then you should finish your activity and then start new activity.
Try the below code:
finish();
startActivity(new Intent(CurrentActivity.this,NewActivity.class));
You probably need to implement some sort of refresh method to update your ListView. Depending on how you are persisting the data, you could create a method that retrieves the latest data and then updates the adapter for the ListView. Once you have that implemented, you should call the method in onResume().

Using an Android Long-Button press to increment/decrement counter

I want to be able to press a button on my program and hold it down (without releasing) to increment a variable. Problem I am having right now is when I conduct the long button press it only runs once, until I release and press again.
First I want to find out if there is a way to do this without having to use the OnTouchListener, and just using the OnLongClick. Is there a way to check the value of the button? For example.. buttondown=true; Conduct a whileloop to increment until the button is released.
Second, I don't want the updates to be delayed, because the incremented value is being drawn as the user holds down the button.
Basically I am doing something like this:
btn_resume.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
..code..
return true;
}
});
OnLongClick will only be called once per press. It isn't going to work for your purpose.
If I understood your question correct this can be achieved using a OnLongClickListener.
Check http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)
OnTouchListener provides a more granular handling of touch events, e.g. KeyDown, KeyUp
I think you can use OnLongClickListener for increment/decrement. But once the long press is done for the button, the longpress has to be canceled or reset for the next long press of the same button.

Categories

Resources