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);
}
Related
I'm going to use Luxand api for detect persons.
Scenario:
1- At the first on register activity persons sit in front of the camera and take pictures and templates save on sqlite database. also personal information will be save;
2- After that On detect activity one of them sit in front of the camera and take Auto detection and find same temp and show person information.(similarity is 96%)
my problem:Unfortunately too times detection is wrong and two different people’s templates will be recognized as the same person.
My Code:
1- Register activity for register form
for (int i = 0; i < MAX_FACES; ++i) {
if (rects[i] != null && rects[i].x1 <= x && x <= rects[i].x2 && rects[i].y1 <= y && y <= rects[i].y2 + 30) {
mTouchedID = IDs[i];
mTouchedIndex = i;
temp = (byte[]) ImageFrameFaceIDTemps.get(mTouchedID);
if (mPreview != null) {
onTouchMode = true;
try {
requesting name on tapping the face
if (faceSelected != null) {
faceSelected.Selected(temp, data);
}
} finally {
onTouchMode = false;
}
}
break;
}
2- Detection activity for match form
public int RetrievePersonDetection(byte[] template, float[] maxSimilarity_UnderAccepted, float[] maxSimilarityArray) {
if (IDTemps == null || IDTemps.size() == 0) {
return 0;
}
int person = 0;
FSDK_FaceTemplate currentFaceTemp = new FSDK_FaceTemplate();
currentFaceTemp.template = template;
float[] similarityArray = new float[1];
float maxSimilarity = 0;
float MatchingThreshold[] = new float[1];
FSDK.GetMatchingThresholdAtFRR(LocalSetting.MIN_ACCEPTED_SIMILAITY, MatchingThreshold);
for (int i = 0; i < IDTemps.size(); i++) {
long key = IDTemps.keyAt(i);
FSDK_FaceTemplate faceTemp = (FSDK_FaceTemplate) IDTemps.get(key);
int result = FSDK.MatchFaces(currentFaceTemp, faceTemp, similarityArray);
if (result == 0) {
if (maxSimilarity < similarityArray[0] && similarityArray[0] >= MatchingThreshold[0]) {
maxSimilarity = similarityArray[0];
person = (int) key;
maxSimilarityArray[0] = similarityArray[0];
FSDK_FaceTemplate currentFaceTemp2 = new FSDK_FaceTemplate();
}
else if (maxSimilarity_UnderAccepted[0] < similarityArray[0]) {
maxSimilarity_UnderAccepted[0] = similarityArray[0];
}
}
}
return person;
}
I don't no what's my problem? why return wrong person and 2 person similarity is 96%
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);
}
}
I have been working on this yahtzee project and have run into a problem. The dice_number array doesn't seem to be getting the randomly generated values. The oneScore TextView always displays "--". I'm posting my code. Thanks in advance for any help given. Also if you need to see anymore of the code let me know.
switch (v.getId()) {
case R.id.rollBtn:
for(i = 0; i < 5; i++){
randnum = random.nextInt(5);
if(i == 0){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 1){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 2){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 3){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 4){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
else if(i == 5){
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
}
break;
case R.id.onesBtn:
for (i = 0; i < 5; i++)
{
if (dice_number[i] == 1) {
dice_count[0] += 1;
oneScore.setText(Integer.toString(dice_count[0]));
}
else
oneScore.setText("--");
}
That second for loop re-sets the text for each number. So if you have an array
dice_number = {3, 2, 1, 1, 4}
The TextView will be set to --, then --, then 1 (if dice_count[0] was 0), then 2, then --, so all you will see is the result from dice_number[4], which is the last --.
You need to either construct a string or use multiple TextViews to see the whole array, not just the last element.
Also, the if statements in the first for loop aren't doing anything as far as I can tell.
Not an answer, just a suggested refactoring:
case R.id.rollBtn:
for(i = 0; i < 5; i++){
randnum = random.nextInt(5);
images[i].setImageResource(image_array[randnum]);
dice_number[i] = randnum;
}
break;
in fact that piece of code that says if(i == 5) {..} will never be reached because the value of i is always 0,1,2,3, or 4.
I am new to programming and I'm making a very simple blackjack game with only basic functions. When I run the program on the emulator it runs maybe for one hand, two, sometimes 5 or more but it always stops responding at some stage when i click on one of the three butons. There is a splash screen that runs for three seconds and the there is a thread comming from that activity that starts this menu activity. Could anyone maybe tell why this is happening? It usually happens when I clcik on one of the buttons even though there is no much comput
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btDeal = (Button) findViewById(R.id.deal);
playerCards1 = (TextView) findViewById(R.id.playerCards);
playerPoints = (TextView) findViewById(R.id.playerPoints);
dealerCards1 = (TextView) findViewById(R.id.dealerCard);
mpBusted= MediaPlayer.create(this, R.raw.busted);
mpWin = MediaPlayer.create(this, R.raw.win);
mpShuffling = MediaPlayer.create(this, R.raw.shuffling);
mpEven = MediaPlayer.create(this, R.raw.even);
mpHit= MediaPlayer.create(this, R.raw.hit);
btDeal.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
deal ();
}
}); //getTotalDealerCards()
//getTotalPlayerCards()
btHit = (Button) findViewById(R.id.hit);
btHit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Boolean busted = isBusted();
if(!busted){
hitPlayer();
playerCards1.setText(getPlayerCardsToString());
if (isBusted()){
mpBusted.start();
}else{
playerCards1.setText(getPlayerCardsToString());
playerPoints.setText(Integer.toString(getTotalPlayerPoints()));
}
}
}
});
btStand = (Button) findViewById(R.id.stand);
btStand.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
checkWinner();
}// testValue(getTotalPlayerCards())
});
}
/*********** Function declarations starts here **********/
//Sum and return the total points for the dealer cards
public int getTotalDealerPoints(){
int points = 0;
int aceFlag = 0; //flag to deal with Aces
int counter;
for (counter = 0; counter <= getTotalDealerCards(); counter++){
if (dealerCards [counter].getCard() + 1 == 1){
points += 11;
aceFlag++;
}
else if (dealerCards [counter].getCard() + 1 > 10)
points += 10;
else
points += dealerCards [counter].getCard() + 1;
}
do {
if (points > 21 && aceFlag > 0){
points -= 10;
aceFlag--;
}
} while (aceFlag>0);
return points;
}
//Get the total player points deal
public int getTotalPlayerPoints(){
int points = 0;
int aceFlag = 0; //flag to deal with Aces
int counter;
for (counter = 0; counter <= getTotalPlayerCards(); counter++){
if (playerCards [counter].getCard() + 1 == 1){
points += 11;
aceFlag++;
}
else if (playerCards [counter].getCard() + 1 > 10)
points += 10;
else
points += playerCards [counter].getCard() + 1;
}
do {
if (points > 21 && aceFlag > 0){
points -= 10;
aceFlag--;
}
} while (aceFlag>0);
return points;
}
//Deal function to start hand
public void deal (){
// If deal is pressed reset all and start over.
mpShuffling.start();
totalDealerPoints = 0;
totalPlayerPoints = 0;
totalCreatedCards = 0;
for (int i = 0; i < TOTAL_CARDS; i++){
dealerCards [i] = null;
playerCards [i] = null;
createdCards [i] = null;
}
// create dealer & player cards and save them to dealer, player and total arrays.
for (int dealcounter = 0; dealcounter <=1 ; dealcounter++){
dealerCards[dealcounter]= createCard();
addCardToCreatedCards(dealerCards[dealcounter]);
playerCards[dealcounter] = createCard();
addCardToCreatedCards(playerCards[dealcounter]);
}
String theCards = getPlayerCardsToString();
String dealerCard = dealerCards[0].toString();
String playerpoints= Integer.toString(getTotalPlayerPoints());
playerCards1.setText(theCards);
dealerCards1.setText(dealerCard);
playerPoints.setText(playerpoints);//getTotalPlayerPoints()
while (getTotalDealerPoints() < 16){
hitDealer();
}
}
// Create card and validate against existing before returning object.
public Card createCard(){
int counter2 = 0;
int flag = 0;
int value;
int suit;
do {
flag = 0;
suit = randomer.nextInt(4);
value = randomer.nextInt(13);
// validate against permitted values before creating cards
while (counter2 <= getTotalPlayerCards()) {
if (createdCards[counter2].getSuit() == suit && createdCards[counter2].getCard() == value || suit > 3 || suit < 0 || value > 12 || value < 0){
flag = -1;
}
counter2++;
}
} while (flag != 0);
Card theCard = new Card (suit, value);
return theCard;
}
// Add card to the records of created cards
public void addCardToCreatedCards(Card aCard){
createdCards [totalCreatedCards] = aCard;
totalCreatedCards++;
}
// Add a card to dealers cards
public void hitPlayer(){
//If the hand was started add card, else deal to start hand.
if (getTotalPlayerCards()+1 != 0){
mpHit.start();
playerCards [getTotalPlayerCards()+1] = createCard();
addCardToCreatedCards(playerCards [getTotalPlayerCards()]);
}
else
deal();
}
// Create a new card for the dealer
public void hitDealer(){
dealerCards [getTotalDealerCards()+1] = createCard();
addCardToCreatedCards(dealerCards [getTotalDealerCards()]);
}
public String getPlayerCardsToString(){
String cards = "";
int total = getTotalPlayerCards();
if (getTotalPlayerPoints() <=21){
int counter = 0;
while (counter <= total){
cards += playerCards[counter].toString() + " ";
counter++;
}
return cards;
}else {
int counter=0;
while (counter <= total){
cards += playerCards[counter].toString() + " ";
counter++;
}
return cards;
}
}
public int getTotalPlayerCards(){
int initialCount = 0;
while (playerCards[initialCount] != null){
initialCount++;
}
return initialCount-1;
}
public int getTotalDealerCards(){
int initialCount = 0;
while (dealerCards[initialCount] != null){
initialCount++;
}
return initialCount-1;
}
public int getTotalCreatedCards(){
int initialCount = 0;
while (createdCards[initialCount] != null){
initialCount++;
}
return initialCount-1;
}
public Boolean isBusted(){
Boolean busted = false;
if (getTotalPlayerPoints()>21){
busted=true;
totalDealerPoints = 0;
totalPlayerPoints = 0;
mpBusted.start();
playerPoints.setText("You were busted!!");
for (int i = 0; i < TOTAL_CARDS; i++){
dealerCards [i] = null;
playerCards [i] = null;
createdCards [i] = null;
}
}
return busted;
}
//Check for winner
public void checkWinner(){
if (getTotalDealerPoints() <= 21 || getTotalPlayerPoints() <= 21 && !isBusted()){
if (getTotalDealerPoints() > 21 || getTotalDealerPoints() < getTotalPlayerPoints()){
playerPoints.setText("You won!!");
mpWin.start();
}
else if(getTotalDealerPoints() > getTotalPlayerPoints()){
mpBusted.start();
playerPoints.setText("You were busted!!");
for (int i = 0; i < TOTAL_CARDS; i++){
dealerCards [i] = null;
playerCards [i] = null;
createdCards [i] = null;
}
}
else{
mpEven.start();
playerCards1.setText("We have same points!");
}
}
else {
deal ();
}
}
}
Use the debugger in eclipse to find out where it gets frozen.
Also the android emulator is very slow even with a fast PC.
Try using the low resolution simulators.
open DDMS from the android-sdk\tools and check which method or thread is taking more time to execute.
Use AsyncTask or Handler when there is a functional(Computational) things running.
I made a statement and if it is true it continues, I want to stop this "continue" and make another statement for example touchdown and touchup.
here is my code
private void updateRunning(float deltaTime) {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
int len = touchEvents.size();
for (int i = 0; i < len; i++) {
TouchEvent event = touchEvents.get(i);
if (event.type != TouchEvent.TOUCH_UP)
continue;
world.doodle.DOODLE_Y = 3;
touchPoint.set(event.x, event.y);
guiCam.touchToWorld(touchPoint);
if (OverlapTester.pointInRectangle(pauseBounds, touchPoint)) {
Assets.clicks();
state = GAME_PAUSED;
return;
}
}
world.update(deltaTime, game.getInput().getAccelX());
if (world.score != lastScore) {
lastScore = world.score;
scoreString = "" + lastScore;
}
if (world.state == World.WORLD_STATE_NEXT_LEVEL) {
state = GAME_LEVEL_END;
}
if (world.state == World.WORLD_STATE_GAME_OVER) {
state = GAME_OVER;
if (lastScore >= Settings.highscores[4])
scoreString = "new highscore: " + lastScore;
else
scoreString = "score: " + lastScore;
Settings.addScore(lastScore);
Settings.save(game.getFileIO());
}
}
Little confused by what you are asking, but perhaps an else if?
if (event.type == TouchEvent.TOUCH_UP) {
/* do something for TOUCH_UP event */
} else if (event.type == TouchEvent.TOUCH_DOWN) {
/* do something for TOUCH_DOWN event */
} else {
/* do something else */
}
You can't stop a continue after you execute it.
Try adding break; where you want it to stop.