actionscript 3 - Action Script 3.0 -
/* here code .. word search game ...in word list array ...these questions find in grid in game...my questions how can type other languages in array or how can use other languages in grids */
import flash.display.*; import flash.text.*; import flash.geom.point; import flash.events.*; import flash.net.filefilter; import flash.filters.glowfilter; const puzzlesize:uint = 20; const spacing:number = 24; const outlinesize:number = 20; const offsetoint = new point(162.9,179.7); const offsettoint = new point(300,265); const spacingg:number = 36; const letterformat:textformat = new textformat("bamini",18,0x000000,true,false,false,null,null,textformatalign.center); // words , grid var wordlist:array; var usedwords:array; var grid:array; // game state var dragmodetring; var startpoint,endpointoint; var numfound:int; // sprites var gamespriteprite; var outlinespriteprite; var oldoutlinespriteprite; var letterspritesprite; var wordsspriteprite; startwordsearch(); function startwordsearch() { **// word list** wordlist = ("lion,tiger,cheetah,panther".split(","; // set sprites gamesprite = new sprite(); addchild(gamesprite); oldoutlinesprite = new sprite(); gamesprite.addchild(oldoutlinesprite); outlinesprite = new sprite(); gamesprite.addchild(outlinesprite); lettersprites = new sprite(); gamesprite.addchild(lettersprites); wordssprite = new sprite(); gamesprite.addchild(wordssprite); // array of letters var letters:array = placeletters(); // array of sprites grid = new array(); for(var x:int=0;x<puzzlesize;x++) { grid[x] = new array(); for(var y:int=0;y<puzzlesize;y++) { // create new letter field , sprite var newletter:textfield = new textfield(); newletter.defaulttextformat = letterformat; newletter.x = x*spacing + offset.x; newletter.y = y*spacing + offset.y; newletter.width = spacing; newletter.height = spacing; newletter.text = letters[x][y]; newletter.selectable = false; var newletterspriteprite = new sprite(); newlettersprite.addchild(newletter); lettersprites.addchild(newlettersprite); grid[x][y] = newlettersprite; // add event listeners newlettersprite.addeventlistener(mouseevent.mouse_down, clickletter); newlettersprite.addeventlistener(mouseevent.mouse_over, overletter); } } // stage listener stage.addeventlistener(mouseevent.mouse_up, mouserelease); // create word list fields , sprites for(var i:int=0;i<usedwords.length;i++) { //var myglow:glowfilter=new glowfilter(); //myglow.color = 0x0000ff; //myglow.blurx=5; //myglow.blury=5; //myglow.strength=200; var newword:textfield = new textfield(); newword.defaulttextformat = letterformat; newword.x = 700; newword.y = i*spacingg+offsett.y; newword.width =140; newword.height =spacing; newword.text = usedwords[i]; //newword.filters=[myglow]; newword.selectable = false; wordssprite.addchild(newword); } // set game state dragmode = "none"; numfound = 0; } // place words in grid of letters function placeletters():array { // create empty grid var letters:array = new array(); for(var x:int=0;x<puzzlesize;x++) { letters[x] = new array(); for(var y:int=0;y<puzzlesize;y++) { letters[x][y] = "*"; } } // make copy of word list var wordlistcopy:array = wordlist.concat(); usedwords = new array(); // make 1000 attempts add words var repeattimes:int = 1000; repeatloop:while (wordlistcopy.length > 0) { if (repeattimes-- <= 0) break; // pick random word, location , direction var wordnum:int = math.floor(math.random()*wordlistcopy.length); var wordtring = wordlistcopy[wordnum].touppercase(); x = math.floor(math.random()*puzzlesize); y = math.floor(math.random()*puzzlesize); var dx:int = math.floor(math.random()*3)-1; var dy:int = math.floor(math.random()*3)-1; if ((dx == 0) && (dy == 0)) continue repeatloop; // check each spot in grid see if word fits letterloop:for (var j:int=0;j<word.length;j++) { if ((x+dx*j < 0) || (y+dy*j < 0) || (x+dx*j >= puzzlesize) || (y+dy*j >= puzzlesize)) continue repeatloop; var thislettertring = letters[x+dx*j][y+dy*j]; if ((thisletter != "*" && (thisletter != word.charat(j))) continue repeatloop; } // insert word grid insertloop:for (j=0;j<word.length;j++) { letters[x+dx*j][y+dy*j] = word.charat(j); } // remove word list wordlistcopy.splice(wordnum,1); usedwords.push(word); } // fill rest of grid random letters for(x=0;x<puzzlesize;x++) { for(y=0;y<puzzlesize;y++) { if (letters[x][y] == "*" { letters[x][y] = string.fromcharcode(65+math.floor(math.random()*26)); } } } return letters; } // player clicks down on letter start function clickletter(event:mouseevent) { var lettertring = event.currenttarget.getchildat(0).text; startpoint = findgridpoint(event.currenttarget); dragmode = "drag"; } // player dragging on letters function overletter(event:mouseevent) { if (dragmode == "drag" { endpoint = findgridpoint(event.currenttarget); // if valid range, show outline outlinesprite.graphics.clear(); if (isvalidrange(startpoint,endpoint)) { drawoutline(outlinesprite,startpoint,endpoint,0x66ccff); } } } // mouse released function mouserelease(event:mouseevent) { if (dragmode == "drag" { dragmode = "none"; outlinesprite.graphics.clear(); // word , check if (isvalidrange(startpoint,endpoint)) { var word = getselectedword(); checkword(word); } } } // when letter clicked, find , return x , y location function findgridpoint(letterspritebject)oint { // loop through sprites , find 1 for(var x:int=0;x<puzzlesize;x++) { for(var y:int=0;y<puzzlesize;y++) { if (grid[x][y] == lettersprite) { return new point(x,y); } } } return null; } // determine if range in same row, column, or 45 degree diagonal function isvalidrange(p1,p2oint):boolean { if (p1.x == p2.x) return true; if (p1.y == p2.y) return true; if (math.abs(p2.x-p1.x) == math.abs(p2.y-p1.y)) return true; return false; } // draw thick line 1 location function drawoutline(sprite,p1,p2oint,c:number) { var offoint = new point(offset.x+spacing/2, offset.y+spacing/2); s.graphics.linestyle(outlinesize,c); s.graphics.moveto(p1.x*spacing+off.x ,p1.y*spacing+off.y); s.graphics.lineto(p2.x*spacing+off.x ,p2.y*spacing+off.y); } // find selected letters based on start , end points function getselectedword()tring { // determine dx , dy of selection, , word length var dx = endpoint.x-startpoint.x; var dy = endpoint.y-startpoint.y; var wordlength:number = math.max(math.abs(dx),math.abs(dy))+1; // each character of selection var wordtring = ""; for(var i:int=0;i<wordlength;i++) { var x = startpoint.x; if (dx < 0) x -= i; if (dx > 0) x += i; var y = startpoint.y; if (dy < 0) y -= i; if (dy > 0) y += i; word += grid[x][y].getchildat(0).text; } return word; } // check word against word list function checkword(wordtring) { // loop through words for(var i:int=0;i<usedwords.length;i++) { // compare word if (word == usedwords[i].touppercase()) { foundword(word); } // compare word reversed var reversewordtring = word.split("".reverse().join(""; if (reverseword == usedwords[i].touppercase()) { foundword(reverseword); } } } // word found, remove list, make outline permanent function foundword(wordtring) { // draw outline in permanent sprite drawoutline(oldoutlinesprite,startpoint,endpoint,0xe01212); // find text field , set gray for(var i:int=0;i<wordssprite.numchildren;i++) { if (textfield(wordssprite.getchildat).text.touppercase() == word) { textfield(wordssprite.getchildat).textcolor = 0xff0000; } } // see if have been found numfound++; if (numfound ==usedwords.length) { endgame(); } } function endgame() { gotoandstop("gameover"; cleanup(); timee.stop(); } function cleanup() { removechild(gamesprite); gamesprite = null; grid = null; vv=null; }
Comments
Post a Comment