Word Search Game: How to Select Multiple Items in a GridView By Dragging? (Android) -
here program details: creating word search game, there bunch of letters arranged in square, , have find , select words either go vertically, horizontally, or diagonally. using array of strings board, , using arrayadapter store string array in gridview, base element of layout of main activity.
here's question: how make users can drag finger on selection, selecting of letters in word without having lift finger off screen multiple times? , want highlight on selected word either stay on screen player selected word, , want highlight on letters go away when user lifts finger screen if did not correctly select word.
i'm late question coming across similar problem recently, decided create own custom board view library address problem. adds more flexibility can , leads more straightforward implementation.
here snippets of code showing how works. first, easy draw board grid on custom view's ondraw() method:
@override protected void ondraw(canvas canvas) { (int = 0; <= numrows; i++) { //for custom grid sizes, y can't equal board size or line drawn won't show int y = math.min(boardheight - 1, * tilesize); canvas.drawline(0, y, boardwidth, y, boardpaint); } (int = 0; <= numcols; i++) { //for custom grid sizes, x can't equal board size or line drawn won't show int x = math.min(boardwidth - 1, * tilesize); canvas.drawline(x, 0, x, boardheight, boardpaint); } } if have string[][] array of strings, here how set letters, assuming views each tile have been placed on board:
public void setletterboard(string[][] letterboard) { if (letterboard.length != getnumrows() || letterboard[0].length != getnumcols()) { setboardsize(letterboard.length, letterboard[0].length); } int row, col; (int i=0; < getchildcount(); i++) { row = / getnumcols(); col = % getnumcols(); lettertileview child = (lettertileview) getchildat(i); child.setletter(letterboard[row][col]); } } then need more complex work handle touch , dragging on board tiles select words:
@override public boolean ontouchevent(motionevent event) { float x = event.getx(); float y = event.gety(); int row = (int) (y / gettilesize()); int col = (int) (x / gettilesize()); view child = getchildat(row, col); //exit on invalid touches if (event.getactionmasked() != motionevent.action_up && (row >= getnumrows() || col >= getnumcols() || child == null)) { return true; } switch (event.getactionmasked()) { case motionevent.action_down: case motionevent.action_move: tile currenttile = new tile(row, col, child); if (currentselectedword == null) { currentselectedword = new selectedword(currenttile); } else if (!currenttile.equals(currentselectedword.lasttile) && currentselectedword.istilevalid(currenttile)) { if (!currentselectedword.istileallowed(currenttile)) { //clear status of old selection updatetiles(currentselectedword.selectedtiles, false, false); //if current tile valid not allowed current word selection, //start new selection matches tile currentselectedword = new selectedword(currentselectedword.getinitialtile()); } list<tile> tiles = gettilesbetween(currentselectedword.lasttile, currenttile); if (tiles.size() > 0) { currentselectedword.addtiles(tiles); } } updatetiles(currentselectedword.selectedtiles, true, false); break; case motionevent.action_up: if (currentselectedword != null) { boolean isvalidselection = (listener != null && listener.onwordselected(currentselectedword.toboardword())); updatetiles(currentselectedword.selectedtiles, false, isvalidselection); if (isvalidselection) { selectedwords.add(currentselectedword); } currentselectedword = null; } break; default: return false; } return true; } the methods istilevalid() , istileallowed() make sure tile user trying select in allowed direction , has not been selected. finally, gettilesbetween() return valid tiles between first tile user touched , 1 he/she touching.
hope helps!
Comments
Post a Comment