How to detect touching/releasing two objects with two pointers - android

I'm trying to touch two rectangle at the same time using two fingers. How can i implements that ?

Never mind I find a solution to this problem :
public void detectTwoRectangleTouching() {
if(Gdx.input.isTouched(0)){
int x = Gdx.input.getX(0);
int y = Gdx.input.getY(0);
if(rectangle1.contain(x,y))
isRectangle1Touched = true;
else
isRectangle1Touched = false;
if(rectangle2.contain(x,y))
isRectangle2Touched = true;
else
isRectangle2Touched = false;
}else {
isRectangle1Touched = false;
isRectangle2Touched = false;
}
if(Gdx.input.isTouched(1)){
int x = Gdx.input.getX(1);
int y = Gdx.input.getY(1);
if(rectangle1.contain(x,y))
isRectangle1Touched = true;
if(rectangle2.contain(x,y))
isRectangle2Touched = true;
}
if(isRectangle1Touched && isRectangle2Touched) {
// the two rectangle are pressed !!!
}
if(!isRectangle1Touched && !isRectangle2Touched) {
// the two rectangle are released !!!
}
}

Related

How to use onscreen buttons to control a random spawned object in Unity?

I am making a Tetris clone for android with Unity 2017 using C#. I followed a Youtube tutorial and have the project basically finished. I would however like to move the spawned Tetrominos with onscreen buttons instead of using touch controls. Is there a way I can use the buttons to access the script on the currentActiveTetrimino to control its movement?
This code is attached to all the tetrominos, they are are randomly spawned and I am not sure how to get the buttons to be able to access this so I can move them.
void CheckUserInput () {
if (Input.GetKeyUp (KeyCode.RightArrow) || Input.GetKeyUp (KeyCode.LeftArrow)) {
movedImmediateHorizontal = false;
horizontalTimer = 0;
buttonDownWaitTimerHorizontal = 0;
}
if (Input.GetKeyUp (KeyCode.DownArrow)) {
movedImmediateVertical = false;
verticalTimer = 0;
buttonDownWaitTimerVertical = 0;
}
if (Input.GetKey (KeyCode.RightArrow)) {
MoveRight ();
}
if (Input.GetKey (KeyCode.LeftArrow)) {
MoveLeft ();
}
if (Input.GetKeyDown (KeyCode.UpArrow)) {
Rotate ();
}
if (Input.GetKey (KeyCode.DownArrow) || Time.time - fall >= fallSpeed) {
MoveDown ();
}
if (Input.GetKeyUp (KeyCode.Space)) {
SlamDown ();
}
}
public void MoveLeft () {
if (movedImmediateHorizontal) {
if (buttonDownWaitTimerHorizontal < buttonDownWaitMax) {
buttonDownWaitTimerHorizontal += Time.deltaTime;
return;
}
if (horizontalTimer < continuousHorizontalSpeed) {
horizontalTimer += Time.deltaTime;
return;
}
}
if (!movedImmediateHorizontal) {
movedImmediateHorizontal = true;
}
horizontalTimer = 0;
transform.position += new Vector3 (-1, 0, 0);
if (CheckIsValidPosition ()) {
FindObjectOfType<Game> ().UpdateGrid (this);
} else {
transform.position += new Vector3 (1, 0, 0);
}
}

Android Password Strength checker with seekbar

How to Create Password Strength checker with seekbar in android ?
You can use https://github.com/VenomVendor/Password-Strength-Checker for your requirement
or use TextWatcher for checking EditText length .Like this way
public void afterTextChanged(Editable s)
{
if(s.length()==0)
textViewPasswordStrengthIndiactor.setText("Not Entered");
else if(s.length()<6)
textViewPasswordStrengthIndiactor.setText("EASY");
else if(s.length()<10)
textViewPasswordStrengthIndiactor.setText("MEDIUM");
else if(s.length()<15)
textViewPasswordStrengthIndiactor.setText("STRONG");
else
textViewPasswordStrengthIndiactor.setText("STRONGEST");
if(s.length()==20)
textViewPasswordStrengthIndiactor.setText("Password Max Length Reached");
}
};
Demo Help .
afterTextChanged (Editable s) - This method is called when the text
has been changed. Because any changes you make will cause this method
to be called again recursively, you have to be watchful about
performing operations here, otherwise it might lead to infinite loop.
create a rule engine for password strength, may be a simple function which returns strength when you pass a string to it.
use a TextWatcher on your password edit text and pass any string entered through your rules.
Use returned strength value from your rule engine to set progress value and progress color of your progress bar.
https://github.com/yesterselga/password-strength-checker-android
is a really good example. I changed the code not to use string values. Instead of it I am using values from 0-4. here is the code
public enum PasswordStrength
{
WEAK(0, Color.RED), MEDIUM(1, Color.argb(255, 220, 185, 0)), STRONG(2, Color.GREEN), VERY_STRONG(3, Color.BLUE);
//--------REQUIREMENTS--------
static int REQUIRED_LENGTH = 6;
static int MAXIMUM_LENGTH = 6;
static boolean REQUIRE_SPECIAL_CHARACTERS = true;
static boolean REQUIRE_DIGITS = true;
static boolean REQUIRE_LOWER_CASE = true;
static boolean REQUIRE_UPPER_CASE = true;
int resId;
int color;
PasswordStrength(int resId, int color)
{
this.resId = resId;
this.color = color;
}
public int getValue()
{
return resId;
}
public int getColor()
{
return color;
}
public static PasswordStrength calculateStrength(String password)
{
int currentScore = 0;
boolean sawUpper = false;
boolean sawLower = false;
boolean sawDigit = false;
boolean sawSpecial = false;
for (int i = 0; i < password.length(); i++)
{
char c = password.charAt(i);
if (!sawSpecial && !Character.isLetterOrDigit(c))
{
currentScore += 1;
sawSpecial = true;
}
else
{
if (!sawDigit && Character.isDigit(c))
{
currentScore += 1;
sawDigit = true;
}
else
{
if (!sawUpper || !sawLower)
{
if (Character.isUpperCase(c))
sawUpper = true;
else
sawLower = true;
if (sawUpper && sawLower)
currentScore += 1;
}
}
}
}
if (password.length() > REQUIRED_LENGTH)
{
if ((REQUIRE_SPECIAL_CHARACTERS && !sawSpecial) || (REQUIRE_UPPER_CASE && !sawUpper) || (REQUIRE_LOWER_CASE && !sawLower) || (REQUIRE_DIGITS && !sawDigit))
{
currentScore = 1;
}
else
{
currentScore = 2;
if (password.length() > MAXIMUM_LENGTH)
{
currentScore = 3;
}
}
}
else
{
currentScore = 0;
}
switch (currentScore)
{
case 0:
return WEAK;
case 1:
return MEDIUM;
case 2:
return STRONG;
case 3:
return VERY_STRONG;
default:
}
return VERY_STRONG;
}
}
and how to use it:
if(PasswordStrength.calculateStrength(mViewData.mRegisterData.password). getValue() < PasswordStrength.STRONG.getValue())
{
message = "Password should contain min of 6 characters and at least 1 lowercase, 1 uppercase and 1 numeric value";
return null;
}
you may use PasswordStrength.VERY_STRONG.getValue() as alternative. or MEDIUM

Problems with Else If statements in Android

I'm working on this program that uses a few lines of code that worked that last time I used them but the only difference this time is that I need to use else if's instead of a basic if else statement. Still trying to learn how to really use this but any help would be appreciated. My error is stemming from the if statement and I have a feeling it is to do with the float result; line.
private float caravan = 35;
private float wigwag = 25;
private float tent = 15;
private float caravan(float value){
return value * caravan;
}
private float wigwag(float value){
return value * wigwag;
}
private float tent(float value){
return value * tent;
}
Button bookButton;
public void onClick(View view) {
EditText bookAmount = (EditText)findViewById(R.id.txtNights);
EditText bookFinal = (EditText)findViewById(R.id.txtBooked);
float bookResult = Float.parseFloat(bookAmount.getText().toString());
float result;
RadioButton cbSelected = (RadioButton)findViewById(R.id.radCaravan);
if(cbSelected.isChecked()){
result = caravan(bookResult);
} else if (result == wigwag(bookResult)){
} else if (result == tent(bookResult)){
}
bookFinal.setText(String.valueOf(result));
}
Your problem is that you put a semicolon at the end of your statements, and you don't use the == operator:
if(cbSelected.isChecked()){
result = caravan(bookResult);
} else if (result = wigwag(bookResult);{
} else if (result = tent(bookResult);{
}
Fix by removing the semicolons and using the equality operator:
if(cbSelected.isChecked()){
result = caravan(bookResult);
} else if (result == wigwag(bookResult){
} else if (result == tent(bookResult){
}
used == instead of = operator
if (result == wigwag(bookResult))

Check textures overlapping

Hey i am writing a game when there are bunch of textures that changes locations, but i want to check that they are not drawing over other textures, i tried to write a code for checking it but its not working well.
Here is the code i tried:
circles.setPosition(new Vector2(r.nextInt(width-height/8*2)+height/8,
r.nextInt(height-height/8*2)+height/8),
i);
circl[i].set((float) (circles.getPosition(i).x+height/16),
(float) (circles.getPosition(i).y+height/16),
height/16);
while(isLaping = true){
System.out.println("in");
for(int y = 0; y < circlesArray.length-1; y++){
if(Intersector.overlaps(circl[y], circl[y+1])){
circles.setPosition(new Vector2(r.nextInt(width-height/8*2)+height/8,
r.nextInt(height-height/8*2)+height/8),
i);
circl[i].set((float) (circles.getPosition(i).x+height/16),
(float) (circles.getPosition(i).y+height/16),
height/16);
}else{
isLaping = false;
}
}
}
How to fix it?
this is confusing,
while(isLaping = true){
you mean it
while(isLaping == true){
or
while(isLaping){
while(isLaping=true); isLaping never is false at the moment evaluate while because asign true every time
I tried again to do so with your tip but the circle are getting crazy and changes places every second
this is the code i have been trying.
for(int i = 0;i<circlesArray.length;i++)
{
if(circlesArray[i]>200)
{
changePosition(i);
}
circlesArray[i]++;
}
private void changePosition(int i)
{
int s = 0;
circles.setPosition(new Vector2(r.nextInt(width-height/12*2)+height/8,r.nextInt(height-height/12*2)+height/12),i);
circl[i].set((float) (circles.getPosition(i).x+height/24),(float) (circles.getPosition(i).y+height/24),height/24);
while (lap == true)
{
circles.setPosition(new Vector2(r.nextInt(width-height/12*2)+height/8,r.nextInt(height-height/12*2)+height/12),i);
circl[i].set((float) (circles.getPosition(i).x+height/24),(float) (circles.getPosition(i).y+height/24),height/24);
for(int y= i+1;y<circlesArray.length;y++)
{
if(circl[i].overlaps(circl[y]))
{
circles.setPosition(new Vector2(r.nextInt(width-height/12*2)+height/8,r.nextInt(height-height/12*2)+height/12),i);
circl[i].set((float) (circles.getPosition(i).x+height/24),(float) (circles.getPosition(i).y+height/24),height/24);
s=0;
}
else
{
s++;
}
}
if(s == circlesArray.length)
lap = false;
}
circlesArray[i] = 0;
x[i] = r.nextInt(circleTexture.length);
}

Get Index Of The Shuffled Item After collections.shuffle

I am trying to do a jigsaw puzzle app in android. In this, I have split a Bitmap into many small chunks. These chunks are then displayed in a GridViewNow I need to shuffle them. Then, I need to know each image chunk's actualPosition(where the piece was supposed to be, its actual location in the image) and its currentPosition(where the piece is currently located). actualPosition and currentPosition are 2 integer arrays. So is there a way that I can get each image chunk's currentPosition and actualPosition after the shuffling so that after every move that the user make I can check wether every image chunk's actualPosition equals its currentPosition. If so the user wins the game. Can anyone please help me out.
Below is the number puzzle game in pure Java that works. Can be run from command line.
It re-prints the whole matrix after every move (not pretty). It demos the basic game.
I hope most of the code is self explanatory. This shows the basic 2-dim mapping of the game, position tracking, validating based on numbers. Have fun.
package madhav.turangi.basic.game;
import java.util.Random;
import java.util.Scanner;
public class NumberPuzzle {
int size;
int[][] arr;
int spaceRow;
int spaceCol;
int turnsTook;
public NumberPuzzle(int size) {
this.size = size;
arr = new int[size][size];
}
void init()
{
for(int r=0; r<size; r++)
{
for(int c=0; c<arr[r].length; c++)
{
arr[r][c] = r*size + c + 1; // row-column of cell to its value equation
}
}
spaceRow = spaceCol = size - 1; // bottom-right cell index
}
int readUserInput()
{
int value = -1;
boolean valid = false;
do {
System.out.printf("To move space [0 - Up, 1 - Down, 2 - Left, 3 - Right] : ? ");
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
try
{
value = Integer.parseInt(line);
valid = (value>=0 && value<=3);
}
catch(NumberFormatException ne)
{
}
if(! valid) System.out.println("== Invalid ==");
} while (! valid);
return value;
}
void swap(int aRow, int aCol, int withRow, int withCol)
{
int temp = arr[aRow][aCol];
arr[aRow][aCol] = arr[withRow][withCol];
arr[withRow][withCol] = temp;
}
boolean moveUp()
{
if(spaceRow != 0)
{
int newSpaceRow = spaceRow - 1;
swap(spaceRow, spaceCol, newSpaceRow, spaceCol);
spaceRow--;
return true;
}
else
{
return false;
}
}
boolean moveDown()
{
if(spaceRow != size-1)
{
int newSpaceRow = spaceRow + 1;
swap(spaceRow, spaceCol, newSpaceRow, spaceCol);
spaceRow++;
return true;
}
else
{
return false;
}
}
boolean moveRight()
{
if(spaceCol != size-1)
{
int newSpaceCol = spaceCol + 1;
swap(spaceRow, spaceCol, spaceRow, newSpaceCol);
spaceCol++;
return true;
}
else
{
return false;
}
}
boolean moveLeft()
{
if(spaceCol != 0)
{
int newSpaceCol = spaceCol - 1;
swap(spaceRow, spaceCol, spaceRow, newSpaceCol);
spaceCol--;
return true;
}
else
{
return false;
}
}
void shuffle()
{
Random rnd = new Random(System.currentTimeMillis());
boolean moved = false;
int attemptCount = 1;
int maxMoves = 20;
for(int moveCount=0; moveCount<maxMoves; moveCount++, attemptCount++)
{
int randomMoveDir = rnd.nextInt(4);
moved = move(randomMoveDir);
if(! moved) moveCount--; //ensure maxMoves number of moves
}
System.out.printf("Shuffle attempts %d\n",attemptCount);
}
boolean move(int dir)
{
boolean moved = false;
switch(dir)
{
case 0 : // up
moved = moveUp();
break;
case 1 : // down
moved = moveDown();
break;
case 2 : // left
moved = moveLeft();
break;
case 3 : // right
moved = moveRight();
break;
}
return moved;
}
void prnArray()
{
System.out.println("-- -- -- -- --");
for(int[] row : arr)
{
for(int cellValue : row)
{
String v = (cellValue == 16 ? "" : String.valueOf(cellValue));
System.out.printf("%4s", v);
}
System.out.println();
}
System.out.println("-- -- -- -- --");
}
boolean validate()
{
for(int r=0; r<size; r++)
{
for(int c=0; c<arr[r].length; c++)
{
if(arr[r][c] != (r*size + c + 1))
{
return false;
}
}
}
return true;
}
boolean oneTurn()
{
int dir = readUserInput();
boolean moved = move(dir);
boolean won = false;
if(moved)
{
turnsTook++;
prnArray();
won = validate();
}
else
{
System.out.println("= Invalid =");
}
return won;
}
void play()
{
init();
System.out.println("Before shuffle");
prnArray();
shuffle();
prnArray();
boolean won = false;
while(! won)
{
won = oneTurn();
}
System.out.printf("Won in %d\n", turnsTook);
}
public static void main(String[] args)
{
NumberPuzzle puzzle = new NumberPuzzle(4);
puzzle.play();
}
}

Categories

Resources