So I'm trying to make a little game for Android. It's about turning lights on and off. I have 25 ImageButtons and, when I press one, I need the ones that are adjacent (up, left, down, right) to change too.
The thing is that I can't find a way to get/identify this buttons.
I tried using "view.getX()" and "view.getY()" but don't know how to implement them the right way (if there is one).
Here's the code I use to change ONE button:
public void change(View view)
{
String state = view.getTag().toString();
if (state.equals("on"))
{
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.lightOff));
view.setTag("off");
lightsOn -= 1;
txtNumLights.setText(String.valueOf(lightsOn));
}
if (state.equals("off")) {
view.setBackgroundDrawable(getResources().getDrawable(R.drawable.lightOn));
view.setTag("on");
lightsOn += 1;
txtNumLights.setText(String.valueOf(lightsOn));
}
}
I use the tags to know if it's On or Off and set a txtView with the number of ON lights.
But, to the point, how do I get the ones adjacent to this one?
Thanks in advance.
You have to store the buttons on a bidimensional array.
When you click on one button, you set the tag and the background on the buttons you are interested on.
Pseudocode:
If (button_matrix[row][column] is clicked) then
Set_tag_background (button_matrix[row-1][column])
Set_tag_background (button_matrix[row+1][column])
Set_tag_background (button_matrix[row][column-1])
Set_tag_background (button_matrix[row][column+1])
Set_tag_background (button_matrix[row][column])
...
On the implementation you should take care about not generating ArrayOutOfBoundsException...
Related
On the Dell Inspiron 15 3000, the touchpad doesn't have any physical left/right buttons. Instead, it is one giant touchpad that is pressure sensitive. I'm assuming it detects right/left clicks based off of hand position on the trackpad.
In my LWJGL application, I detect mouse button clicks with Mouse.isButtonDown(0). This works fine on computers with a mouse with physical buttons, but doesn't work on touchpads that lack physical buttons. Mouse.getButtonCount() returns 0.
Has anybody had any success in detecting if a mouse button is pressed, should the user be using a trackpad that doesn't have physical buttons?
I think, instead of using
org.lwjgl.input.Mouse
This class could be what you are searching for:
org.lwjgl.input.Controllers
http://legacy.lwjgl.org/javadoc/org/lwjgl/input/Controllers.html
Im not entirely sure though since I only have a mouse and no way to test this with a touchpad.
For those who find this in the future, I did find a fix:
You cannot, should there be no physical buttons, use the Mouse.isButtonDown() method. Instead, you are going to have to read the event buffer. To do so, I wrote my own helper class:
public class Mouse{
private static boolean button_left = false, button_right = false;
public static boolean isButtonDown(int button){
if(button == 0) return button_left;
if(button == 1) return button_right;
return false;
}
public static void update(){
while(org.lwjgl.input.Mouse.next()){
if(org.lwjgl.input.Mouse.getEventButton() == 0) button_left = org.lwjgl.input.Mouse.getEventButtonState();
if(org.lwjgl.input.Mouse.getEventButton() == 1) button_right = org.lwjgl.input.Mouse.getEventButtonState();
}
}
}
The update() method is called every tick, and as such I can get button states using Mouse.isButtonDown(button).
I'm creating java game (I'm a beginner with this for now) and I'd like to start with some kind of platform game.
I'd like to know how I can make the player jump (I know how to move him up and down), but I don't know how how to make him go back down after going up.
Here is my code:
public void keyPress() {
if (listener.arrowUp) {
Jump();
}
}
private void Jump() {
if(player.get(1).getPosY() > maxJump) {
player.get(1).moveY(-10);
} else if(player.get(1).getPosY() == maxJump) {
player.get(1).moveY(85);
}
}
So.. the player moves -10px upwards as long as i press 'w' and when he hits maxJump (which is 375 and players position at the start is 465) he "teleports" back to 465 instead of sliding back down like he does when going up.. It's really hard to explain this without a video, but i hope somebody understands and can help me with this.
This question gives a basic answer to yours. Now in your jump function, you have to set the vertical_speed only once and only call fall() every frame and add some moveY.
Here are two alternatives:
Option 1. Simulating some very simple physics. You add forces to your player, so pressing the up arrow will add a force that makes the player move upwards. Gravity is constantly applied and thus it will gradually cancel out the force you applied when the up arrow was pressed. Perhaps you only want to use forces in the vertical direction you do something like this:
// Each frame
if(notOnTheGround){
verticalVelocity -= GRAVITATIONAL_CONSTANT;
}
// When user jumps
vertivalVelocity += JUMP_FORCE;
Option 2. An alternative is to kind of animate the jumps using projectile motion.
// Each frame
if(isJumping){
verticalVelocity = initialVelocity*sin(angle) - g*animationTime;
animationTime += TIME_STEP;
}
// When user jumps
isJumping = true;
animationTime = 0;
initialVelocity = ... // calculate initial velocity
I am making an android game which needs to display some different images.
I need to make a method that keeps on generating images from a resource in the drawable directory and then drop the images from the top of the screen to the bottom. I also need that each image should have an id "image-1" "image-2" in which the index number goes higher once more images are created as an integer. I also need them to be generated at different locations at the top of the screen and drop with a specified speed. Once they hit the bottom, I want a method to be executed like:
public void touchedGround() {
//My code
}
I am using this code so far:
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class GameScreenActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gamescreen);
generateParachuters(savedInstanceState);
}
private void generateParachuters(Bundle icicle) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "generating images", 10).show();
}
}
I know one way to do it, but it requires a lot more code then what you're probably looking for. But hey, here goes.
Create a class for your falling object, which I'm guessing is a Parachuter. This class will have an X and Y variable to keep track of where it is on the screen. You can then have a tick method, which will increase the Y variable by however fast you want it to fall. You can also have them keep track of a integer ID if you want.
Create a list that will hold the Parachuter objects. I use an ArrayList. This way you create a Parachuter when needed and add it to the list. Cycle through the list to draw each one.
You need something to draw the Parachuters with. Personally, I use Canvas from SurfaceView.
You'll need a looping animation. Loop through, each time drawing the Parachuters and calling tick on each one.
During the loop, you can check the position of each Parachuter. If its position goes beyond Canvas.getHeight(), remove the image from the list.
Well, that's quite a bit there. Ask me if you need further clarification.
UPDATE: Here's a fully-working example.
UPDATE 2:
//Check right edge of screen
for (int i = 0; i < parachuters.size(); i++)
{
if (parachuters.get(i).getX() > canvas.getWidth())
//Do whatever it is you want
}
I have been steaming over this one a few days now. I try to have a selector for a big range of numbers... within int-limits. You ought to hold down a button and the value should change slow and the longer you press the more rapid it changes. You all know that kind of button, don't you?! :D
It has to be precise, yet fast to enter the number. I use a OnTouchListener. The event allows to .getDownTime().
I tried a lot, but always only got it to skip numbers (not really but too fast to use) or not repeatingly increase the value while holding down the button. If run without changes it is pretty fast, but it would be nice, if it starts to make 10 100 or 1000-jumps if it is held down long enough.
I prepared a little SSCCE looking like this:
public class ValueChangerSSCCE extends Activity implements OnTouchListener {
Button plusButton;
TextView valueView;
Button minusButton;
int value;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
plusButton = new Button(this);
plusButton.setText("+");
plusButton.setTextSize(120f);
plusButton.setOnTouchListener(this);
layout.addView(plusButton);
valueView = new TextView(this);
valueView.setGravity(Gravity.CENTER_HORIZONTAL);
valueView.setTextColor(Color.WHITE);
valueView.setText(value + "");
valueView.setTextSize(120f);
layout.addView(valueView);
minusButton = new Button(this);
minusButton.setText("-");
minusButton.setTextSize(120f);
minusButton.setOnTouchListener(this);
layout.addView(minusButton);
setContentView(layout);
}
/*
* Called when a button is pressed
*/
#Override
public boolean onTouch(View v, MotionEvent event) {
if (v == plusButton) {
value++;
} else if (v == minusButton) {
value--;
}
valueView.setText(value + "");
return false;
}
}
For devices with low resolution one might have to decrease the text-sizes.
Thanks in advance!
Have a look at the NumberPicker source code. This is a widget from the SDK that does exactly what you are describing, except without the acceleration. Using post calls to a Handler as is done here is the best way to handle the timing events. The only addition you would need to make to this class is to vary the mSpeed value in the post callbacks (and perhaps the increment) based on the down time.
HTH.
The way I might try and tackle this is as follows:
Pressing + or - will start a Thread. The thread updates the number at a set interval by an offset value, if the number has changed more than 20 times then decrease the interval so the number updates faster, if the number is still changing a lot then the offset value would be made larger to skip numbers. Remember all this is happening in a thread.
When the user lifts their finger the thread is killed and the offset, interval is reset.
Use the value of getDownTime() to compute how big the counter increment should be.
First, design the response curve you would like. (That is, determine a function that defines how the value should change as a function of touch time and, perhaps, starting value. It does not — should not, from the way you describe it — be a straight line function.)
Then just make your increment follow the curve. It will probably help to store the button value and down time at the last event.
I'm pretty new at java and I've been trying to write a method that draws a gradient from one configurable color to another. However, it appears that if statements inside of the for loop are being ignored.
How can I fix this? or is there something else I'm missing?
and usage of the method is:
Gradient.dVertical(Graphics,Top left corner X,Top left corner Y,Size X,Size Y,tarting Red Value,Starting Green Value,Starting Blue Value,Ending Red Value,Ending Green Value,Ending Blue Value);
EDIT: I figured out what the real problem was and I fixed it. When it should have been incremental down it was going up. So I added a couple more if statements and that cleared it up. Using random integers when calling the method did reveal another problem though. With certain values it will not finish drawing and it will just cut off in the middle. FIXED
Here's the fixed part of the code if anyone is interested
if (rrepeat == true)
{
//prevents division by zero
if(rrate!=0)
{
//for a rate that must repeat checks
//whether or not it is time to increment
check = k%rrate;
if (check==0)
{
if(ered<sred)
{
rr--;
}
if(sred<ered)
{
rr++;
}
}
else
{
rr = rr;
}
}
}
You need to be overriding the paint method.
Here's an example: Introduction to applets