java - Android buttons background -
i'm trying make java-android-memory game... works fine, except 1 thing... when turn first card, , second card, there should delay (~seconds or so) bevor 2 cards turned or removed.
so added delay (tried 2 versions, made them comment)
but.... picture of card never shown second card (only first card)... cards removed or hidden correct delay when use second version.
pls. can me
here code (relevant part)
public abstract class game { private static final int maxbuttonamount = 30; //maximum amount of buttons (=cards) per game private card[] cards; private hashmap<button, card> buttonstocardmap; //the button of first turned card, null if cards turned down private button firstturnedcardbutton; private view.onclicklistener defaultlistener = new view.onclicklistener() { @override public void onclick(view v) { turncard((button) v); evaluatecardchoice((button) v); } }; private view.onclicklistener audiolistener = new view.onclicklistener() { @override public void onclick(view v) { gameactivity.sound.playcardmusic(gameactivity.getcontext(), buttonstocardmap.get(v).getresid()); } }; //------ gameplay-methods ------ protected abstract void endgame(); protected abstract void finishturn(boolean foundpair); /** turns card. if clickable background set appropriate drawable of card , clickable set false. <br><br> * * if not clickable (aka turned) background set r.drawable.cards_back , made clickable again. * * @param turnedcardbutton button of card turn. */ private void turncard(button turnedcardbutton){ card card = buttonstocardmap.get(turnedcardbutton); if (card.getcardstate().equals(cardstate.hidden)){ card.setcardstate(cardstate.turned); log.d("testlog", "card turned"); if (card.getcardtype().equals(cardtype.picture)){ turnedcardbutton.setbackgroundresource(card.getresid()); turnedcardbutton.setclickable(false); } else{ turnedcardbutton.setbackgroundresource(r.drawable.cards_audio); gameactivity.sound.playcardmusic(gameactivity.getcontext(), card.getresid()); turnedcardbutton.setonclicklistener(audiolistener); } } else{ card.setcardstate(cardstate.hidden); log.d("testlog","card hidden"); turnedcardbutton.setbackgroundresource(r.drawable.cards_back); if (card.getcardtype().equals(cardtype.picture)){ turnedcardbutton.setclickable(true); } else{ turnedcardbutton.setonclicklistener(defaultlistener); } } } private void evaluatecardchoice(button turnedcardbutton){ /* //give user time see card before evaluation try{ log.d("testlog", "waiting..."); thread.sleep(1500); } catch(interruptedexception e){ log.e("testlog", "interruptedexception in game.initialisebuttons"); } */ /* long start = system.currenttimemillis(); long = system.currenttimemillis(); log.d("testlog", "waiting..."); while( (now-start)<3000){ now=system.currenttimemillis(); } start= system.currenttimemillis(); log.d("testlog", "stopped waiting..."); */ //if no card turned yet save button first turned card, otherwise check found pair if (firstturnedcardbutton == null){ firstturnedcardbutton = turnedcardbutton; } else{ if (buttonstocardmap.get(firstturnedcardbutton).matches(buttonstocardmap.get(turnedcardbutton))){ firstturnedcardbutton.setvisibility(view.invisible); buttonstocardmap.get(firstturnedcardbutton).setcardstate(cardstate.collected); turnedcardbutton.setvisibility(view.invisible); buttonstocardmap.get(turnedcardbutton).setcardstate(cardstate.collected); firstturnedcardbutton = null; this.finishturn(true); if (!morecardsavailable()){ endgame(); } } else { turncard(firstturnedcardbutton); turncard(turnedcardbutton); firstturnedcardbutton = null; this.finishturn(false); } } } private boolean morecardsavailable(){ boolean morecards = false; (card c : cards){ if (!c.getcardstate().equals(cardstate.collected)){ morecards = true; break; } } return morecards; } //------ initialization-methods ------ private void initialisecardsarray(string cardsetname, boolean onlypictures){ arraylist<integer> pictureresids = card.getresourceidsfromcardset(cardsetname, "drawable"); arraylist<integer> audioresids = card.getresourceidsfromcardset(cardsetname, "raw"); arraylist<integer> pairids = new arraylist<>(); (int = 0; < pictureresids.size(); i++){ pairids.add(i); } //choose random cards cardset , add them cards (int = 0; < cards.length; += 2){ int pairid = pairids.remove(new random().nextint(pairids.size())); cards[i] = new picturecard(pairid, pictureresids.get(pairid)); if (onlypictures) { cards[i+1] = new picturecard(pairid, pictureresids.get(pairid)); } else { cards[i+1] = new audiocard(pairid, audioresids.get(pairid)); } } } private void initialisebuttons(){ //create list randomly draw indexes cards arraylist<integer> cardindexlist = new arraylist<>(); (int = 0; < cards.length; i++){ cardindexlist.add(i); } //initializes used buttons , deletes other buttons (int = 0; < maxbuttonamount; i++){ if (i < cards.length){ int buttonresid = gameactivity.getcontext().getresources().getidentifier(string.format("btn_card%02d", i), "id", gameactivity.getcontext().getpackagename()); button curbtn = (button) ((activity) gameactivity.getcontext()).findviewbyid(buttonresid); curbtn.setonclicklistener(defaultlistener); curbtn.settext(""); curbtn.setbackgroundresource(r.drawable.cards_back); //choose random index cards , map curbtn buttonstocardmap.put(curbtn, cards[cardindexlist.remove(new random().nextint(cardindexlist.size()))]); } else{ int buttonresid = gameactivity.getcontext().getresources().getidentifier(string.format("btn_card%02d", i), "id", gameactivity.getcontext().getpackagename()); ((activity) gameactivity.getcontext()).findviewbyid(buttonresid).setvisibility(view.gone); } } } private void positionbuttons(){ button[] buttons = buttonstocardmap.keyset().toarray(new button[cards.length]); int colamount = getbuttoncolumnamount(); int rowamount = (int) math.ceil(cards.length/colamount); int btnheight = calculatebuttonheight(rowamount); int btnwidth = (int) math.floor(btnheight*0.7711); int btnxmargin = (gameactivity.getcontext().getresources().getdisplaymetrics().widthpixels - (colamount * btnwidth))/(colamount+1); // todo int btnymargin = (gameactivity.getcontext().getresources().getdisplaymetrics().heightpixels - (rowamount * btnheight))/(rowamount+1); int curbtnindex = 0; (int = 0; < rowamount; i++) { (int j = 0; j < colamount && curbtnindex < buttons.length; j++) { relativelayout.layoutparams params = new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content,relativelayout.layoutparams.wrap_content); params.leftmargin = j*btnwidth + (j+1)*btnxmargin; //x-coord params.topmargin = i*btnheight ; //y-coord todo + (i+1)*btnymargin; params.width = btnwidth; //width params.height = btnheight; //height buttons[curbtnindex].setlayoutparams(params); curbtnindex++; } } }
Comments
Post a Comment