移动应用开发实验二_第1页
移动应用开发实验二_第2页
移动应用开发实验二_第3页
移动应用开发实验二_第4页
移动应用开发实验二_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

实验二绘制城堡画面2、理解和使用callback模JSONJSON片绘制在城堡背景画面上。这些是打在应用的APK包中的,这样将会减少网络流量,类图中的HolyWarActivity类为城堡画面的Activity类,它继承了AppActivity类AppActivity封装了网络通信和线程代码,还有HolyWarScene类是城堡场景类,它继承了通用Scene类。HolyWarActivity类骨架代码。资源/drawable-mdpi/archery_camppng资源/drawable-mdpi/army_arsenalpng资源/drawable-mdpi/blank01png资源/drawable-mdpi/chamber_secretspng资源/drawable-mdpi/e_assassinpng资源/drawable-mdpi/mcpng资源/drawable-mdpi/meeting_roompng资源/drawable-mdpi/militiapng资源/drawable-mdpi/spearmanpng资源/drawable-mdpi/templepng实现 接口:地址/castle.php从服务器端请求回数据后,就知道了在这20块空地上要绘制的建筑了。绘制这些建是通过在SurfaceView20地的坐标和建筑物。地块编号、建筑名称、建筑名称拼音、坐标和信息见表所其中blank01、blank02和blank03是3个透明的,把它绘制在已有的背景上,能够响应触摸,例如,粮田已经在背景中了,为了让粮田能够响应触摸事AppUtil.java部分参考代码建筑物的名字获得在res中建筑物的*@param建筑物的名字数@return建筑物的id数publicstaticint[]getBuidingImgId(Stringname[]){int[]building1=newint[20];for(inti=0;i<20;i++){Stringaname=name[i];if(aname.equals("space")){building1[i]=R.drawable.blank03;}if(aname.equals("temple")){building1[i]=R.drawable.temple;}if(aname.equals("residence")){building1[i]=R.drawable.residence;}if(aname.equals("storehouse")){building1[i]=R.drawable.storehouse;}if(aname.equals("chamber_secrets")){building1[i]=R.drawable.chamber_secrets;}if(aname.equals("huzhongsi"))building1[i]=R.drawable.meeting_room;}if(aname.equals("grand_council")){building1[i]=R.drawable.grand_council;}if(aname.equals("parade_ground")){building1[i]=R.drawable.parade_ground;}if(aname.equals("demons_pavilion")){building1[i]=R.drawable.demons_pavilion;}if(aname.equals("gongjingying")){building1[i]=R.drawable.archery_camp;}if(aname.equals("voodoo")){building1[i]=R.drawable.voodoo;}if(aname.equals("martial_club")){building1[i]=R.drawable.martial_club;}if(aname.equals("cavalry_camp")){building1[i]=R.drawable.cavalry_camp;}if(aname.equals("army_arsenal")){building1[i]=R.drawable.army_arsenal;}if(aname.equals("sawmill")){building1[i]=R.drawable.blank03;}if(aname.equals("grain_field")){building1[i]=R.drawable.blank03;}if(aname.equals("land_tower_air_defense")){building1[i]=R.drawable.land_tower_air_defense;}if(aname.equals("wall"))building1[i]=R.drawable.blank02;}if(aname.equals("land_tower_ground_defense")){building1[i]=R.drawable.land_tower_ground_defense;}if(aname.equals("space_land_tower")){building1[i]=R.drawable.blank01;}}return}建筑物的名字获得在res中建筑物的*@parampublicstaticStringgetHanZname(Stringaname){if(aname.equals("space")){return空地}ifaname.equals("templereturn"宗正殿";}ifaname.equals("residencereturn"民居";}ifaname.equals("storehousereturn"仓库";}ifaname.equals("chamber_secretsreturn"密库";}ifaname.equals("meeting_roomreturn"合众司";}ifaname.equals("grand_council"return"军机处";}ifaname.equals("parade_groundreturn"练兵场";}ifaname.equals("demons_pavilionreturn"万魔楼";}ifaname.equals("archery_campreturn"神弓营";}ifaname.equals("voodooreturn"教";}ifaname.equals("martial_clubreturn"武士馆";}ifaname.equals("cavalry_campreturn"骠骑营";}ifaname.equals("army_arsenal"return"机巧坊";}ifaname.equals("sawmill"return"木材厂";}ifaname.equals("grain_fieldreturn"粮田";}ifaname.equals("land_tower_air_defensereturn"防空箭塔";}ifaname.equals("wall"return"城墙";}ifaname.equals("land_tower_ground_defensereturn"防地箭塔";}ifaname.equals("space_land_towerreturn"空箭塔";}

return空地通过选择地块id和地块名称获得弹出框中需要图标良田、城墙、木材厂、空箭塔四种情况绘制的于框图标不同@param@parampublicstaticintgetDialogIconId(intimageid,Stringname){inticonid=imageid;if(iconid==R.drawable.blank01||iconid==||iconidR.drawable.blank03ifname.equals("grain_field//良returnelseifname.equals("wall"//城墙returnR.drawable.wall;elseifname.equals("sawmill"//木材厂returnR.drawable.sawmill;}elseifname.equals("space_land_tower//空箭塔returnR.drawable.land_tower;}}

return通过建筑名字和名字返回招募一个招兵所需要材料信息。这些信息是放置一个数组的包括:@param@parampublicstaticString[]recruitSoldierStuffInfo(StringStringsoldiername){String[]arySoldierStuffInfo=newString[5];StringsoldierHZname="";Stringwood="";Stringfood="";StringironOre="";ifsoldiername.equals("transport_soldiersoldierHZname="兵";wood=food=ironOre=}ifsoldiername.equals("scout_soldiersoldierHZname="侦察兵";wood=food=ironOre=}ifsoldiername.equals("militiasoldierHZname="民兵";wood="500";food=ironOre=}ifsoldiername.equals("spearmansoldierHZname="枪兵";wood="700";food=ironOre=}ifsoldiername.equals("assassinsoldierHZname="刺客";wood=food=ironOre=}}ifbuildingName.equals("marksmansoldierHZname="神射手";wood=food=ironOre=}ifbuildingName.equals("voodoo_mansoldierHZname="术士";wood=food=ironOre=}ifbuildingName.equals("warriorsoldierHZname="武士";wood=food=ironOre=}ifbuildingName.equals("military_general"soldierHZname="骠骑";wood=food=ironOre=}ifsoldiername.equals("battering_ramsoldierHZname="冲撞车";wood=food=ironOre=}ifsoldiername.equals("catapultsoldierHZname="投石车";wood="17000";food=ironOre=}}arySoldierStuffInfo[0]=soldiername;arySoldierStuffInfo[1]=soldierHZname;arySoldierStuffInfo[2]=wood;arySoldierStuffInfo[3]=food;return}Scene.javapublicclassSceneextendsSurfaceViewimplementsSurfaceHolder.CallbackprivatefinalstaticStringTAG="Scene";privateSurfaceHolderholder;privateDirectorprivateArrayList<Spirit>spirits=newpublicScene(Contextcontext)//创建一个新的holder=holder}publicvoidaddSpirit(Spiritsp)}publicbooleanonTouchEvent(MotionEventevent)return}floatX=floatY=TODO根据x和y精synchronized(holder)for(Spiritsp:spirits)Spirit.Coordinatescoord;coord=//判断是if((X>=coord.getX()&&X<=(coord.getX()+&&(Y>=coord.getY()&&Y<=(coord.getY()+.getImage().getHeight()))){sp.onTouchEvent(this,event);}}return}}publicvoidonDraw(Canvascanvas)for(Spiritgraphic:spirits)Spirit.Coordinatescoord;Spirit.Speedspeed;coord=graphic.getCoordinates();speed=graphic.getSpeed();coord.setX(coord.getX()+speed.getX());}elsecoord.setX(coord.getX()-}coord.setY(coord.getY()+speed.getY());}elsecoord.setY(coord.getY()-}//bordersforif(coord.getX()<0){}elseif(coord.getX()+graphic.getImage().getWidth()>getWidth()){coord.setX(coord.getX()+-(coord.getX()+}//bordersforif(coord.getY()<0){}elseif(coord.getY()+graphic.getImage().getHeight()>getHeight()){coord.setY(coord.getY()+-(coord.getY()+}Bitmapbitmap=graphic.getImage();canvas.drawBitmap(bitmap,coord.getX(),coord.getY(),null);}}publicvoidsurfaceChanged(SurfaceHolderholder,intformat,intintheight)}publicvoidsurfaceCreated(SurfaceHolderholder)//启动线mDirector=new}publicvoidsurfaceDestroyed(SurfaceHolderholder)//停止线if(mDirector!=null){mDirector=null;}}classDirectorextendsThreadprivatebooleandone;Director(){done=false;}publicvoidrun()SurfaceHoldersurfaceHolder=//反复循环执行线while(!done)//锁定surface并返回canvas于绘制2D图Canvascanvas=//Android403景状态canvasif(canvas==null){}synchronized(surfaceHolder)//回调MySurfaceView的onDraw方}//画布渲染当前图像try{}catch(InterruptedExceptione)}}}publicvoidrequestExitAndWait()//完成线程并合并子线程到done=true;try{}catch(InterruptedExceptionex)}}}Spirit.javapublicclassSpiritprivatefinalstaticStringTAG="Spirit";privateBitmap_bitmap;privateCoordinates_coordinates;privateSpeed_speed;privateStringpublicSpirit(Bitmapbitmap,intxCoordinates,intintxSpeed,intySpeed,StringcallBackMethod)_bitmap=_coordinates=new_speed=new_callBackMethod=}publicBitmapgetImage()return}publicSpeedgetSpeed()return}publicCoordinatesgetCoordinates()return}*触publicbooleanonTouchEvent(ViewsuperView,MotionEventevent)//Logv(TAG"触摸精灵if(_callBackMethod==null||"".equals(_callBackMethod))return}tryClass<?extendsView>clazz=Methodm=clazz.getDeclaredMethod(_callBackMethod,Spirit.class,m.invoke(superView,this,}catch(Exceptione){returnfalse;}return}publicclassSpeedpublicstaticfinalintX_DIRECTION_RIGHT=1;publicstaticfinalintX_DIRECTION_LEFT=-1;publicstaticfinalintY_DIRECTION_UP=-1;privateint_x=privateint_y=privateint_xDirection=privateint_yDirection=*@returnthepublicintgetXDirection()return}@paramthe_xDirectiontopublicvoidsetXDirection(intdirection)_xDirection=}publicvoidtoggleXDirection()if(_xDirection==X_DIRECTION_RIGHT)_xDirection=}else_xDirection=}}@returnthepublicintgetYDirection()return}@paramthe_yDirectiontopublicvoidsetYDirection(intdirection)_yDirection=}publicvoidtoggleYDirection()if(_yDirection==Y_DIRECTION_DOWN)_yDirection=}else_yDirection=}}@returnthepublicintgetX()return}@paramthe_xtopublicvoidsetX(intspeed)_x=}@returnthepublicintgetY()return}@paramthe_ytopublicvoidsetY(intspeed)_y=}publicStringtoString()StringxDirection="right";}elsexDirection=}}

return"Speed:x:"+_x+"|y:"+_y+"|xDirection:+*ContainsthecoordinatesofthepublicclassCoordinates{privateint_x=0;privateint_y=0;publicintgetX()return}publicvoidsetX

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论