问题描述
importjava.io.IOException;importjava.io.InputStream;importjava.util.Random;importjava.io.ByteArrayInputStream;importjava.io.IOException;importjavax.microedition.media.Manager;importjavax.microedition.media.MediaException;各位仁兄,小弟初学,不知道代码哪里的原因,帮帮忙吧importjavax.microedition.media.Player;importjavax.microedition.media.PlayerListener;importjavax.microedition.lcdui.*;importjavax.microedition.media.Manager;importjavax.microedition.media.MediaException;importjavax.microedition.media.Player;publicclassMyCanvasextendsCanvasimplementsRunnable{publicImageoffScreen;publicGraphicsoffg;//定义publicintscreenW,screenH;publicintappState=APP_LOGO;//当前应用的状态publicstaticfinalintAPP_LOGO=1;publicstaticfinalintAPP_CP=2;publicstaticfinalintAPP_MENU=3;publicstaticfinalintAPP_HELP=4;publicstaticfinalintAPP_ABOUT=5;publicstaticfinalintAPP_GAME=6;publicstaticfinalintAPP_GAME2=7;publicstaticfinalintAPP_GAME21=8;publicstaticfinalintAPP_GAME22=9;publicstaticfinalintAPP_GAME23=10;publicstaticfinalintAPP_SY=11;publicstaticfinalintAPP_YUYAN=12;publicstaticfinalintAPP_tiaochu=13;publicstaticfinalintAPP_MAP1=14;publicstaticfinalintAPP_MAP2=15;publicstaticfinalintAPP_MAP3=16;publicstaticfinalintAPP_MAP4=17;publicstaticfinalintAPP_LOADING=18;publicstaticfinalintAPP_LOAD=19;publicstaticfinalintAPP_LOD=20;publicstaticfinalintAPP_SOUND=21;publicstaticfinalintAPP_puanduan=22;publicstaticfinalintAPP_OVER=23;publicstaticfinalintAPP_PUZUO=24;publicstaticfinalintAPP_PUYOU=25;publicstaticfinalintAPP_PUZUO1=26;publicstaticfinalintAPP_PUYOU1=27;publicstaticfinalintAPP_SHU=28;publicstaticfinalintAPP_YING=29;publicstaticfinalintAPP_PING=30;publicfinalintUP=0;publicfinalintRIGHT=1;publicintdir=1;publicfinalintDOWN=2;publicfinalintLEFT=3;//声明图片与各自坐标publicImageimlogo,imlog,immenu,immenupoint,kaishi,xuanxiang,bangzhu,guanyu,tuichu,石头,剪刀,布,meiqiu;publicintcount=0,temp;publicintmenuPy=105;publicImageimg_map1,img_map2,img_map3,img_map4,r0,r1,r2,r3,setout;publicImageimg_play,img0,img1,img2,img3,img4,bg,shu,shu1,ying,ying1,woshu,woying,dianshu,dianying;publicintrx=120,ry=160,rw=16,rh=16;publicintrloop;publicRandomrdm=newRandom();publicintspeed=0;publicImageimg_s1,img_you;publicImageC_k,cy1,cy2,cy3,cs1,cs2,cs3;publicImageimg_bk;publicImageimg_zuo;publicImagetou_k,y1,y2,y3,s1,s2,s3;publicImageimg_shang,img_xia;publicintimg_x1=78,j=5,s1y=-20;publicintimg_x2=144,m=10;publicintloop;//表示当前的电脑帧数publicfinalintclip_h=71;publicintaction[][]={{0,1,0,2},//动作0{0,1,1,2},{0,1,2,2},{1,2,0,1},{1,1,0,2},};//Game需要的图片publicImageqiu,chang;publicImageti1,zhunxing,右扑,左扑,等待;//加载资源publicMyCanvas(){this.setFullScreenMode(true);screenW=this.getWidth();screenH=this.getHeight();//创建一个和屏幕一样大的缓冲区offScreen=Image.createImage(screenW,screenH);//菜单用图offg=offScreen.getGraphics();imlogo=this.loadImage("logo0.png");imlog=this.loadImage("caida.png");immenu=this.loadImage("menuback.png");immenupoint=this.loadImage("31.png");kaishi=this.loadImage("x1.png");xuanxiang=this.loadImage("x2.png");bangzhu=this.loadImage("x3.png");guanyu=this.loadImage("x4.png");tuichu=this.loadImage("x5.png");meiqiu=this.loadImage("meiqiu.png");//电脑图片加载C_k=this.loadImage("/win/82.png");img_s1=this.loadImage("/xing/s1.png");img_you=this.loadImage("/xing/you.png");shu=this.loadImage("/win/shu.png");shu1=this.loadImage("/win/shu1.png");ying=this.loadImage("/win/ying.png");ying1=this.loadImage("/win/ying1.png");//人物图片加载tou_k=this.loadImage("/win/80.png");img_zuo=this.loadImage("/xing/zuo.png");img_s1=this.loadImage("/xing/s1.png");img_shang=this.loadImage("/xing/shang.png");img_xia=this.loadImage("/xing/xia.png");//出拳加载bg=this.loadImage("/du.png");石头=this.loadImage("/xing/2.png");剪刀=this.loadImage("/xing/1.png");布=this.loadImage("/xing/3.png");//人物地图img_map1=this.loadImage("/c1.png");img_map2=this.loadImage("/c2.png");img_map3=this.loadImage("/c3.png");img_map4=this.loadImage("/c4.png");r1=this.loadImage("/r1.png");r2=this.loadImage("/r2.png");r3=this.loadImage("/r3.png");r0=this.loadImage("/r0.png");setout=this.loadImage("/setout.png");img_play=this.loadImage("/7.png");img0=this.loadImage("/0.png");img1=this.loadImage("/1.png");img2=this.loadImage("/2.png");img3=this.loadImage("/3.png");img4=this.loadImage("/4.png");//Game2用图chang=this.loadImage("/ch.png");qiu=this.loadImage("/qiu1.png");ti1=this.loadImage("/ti1.png");zhunxing=this.loadImage("/zhunxing.png");右扑=this.loadImage("/youpu.png");左扑=this.loadImage("/zuopu.png");等待=this.loadImage("/dengdai.png");woshu=this.loadImage("/woshu.png");woying=this.loadImage("/woying.png");dianshu=this.loadImage("/dianshu.png");dianying=this.loadImage("/dianying.png");addPlayer();Threadt=newThread(this);t.start();}//声音播放器publicPlayerp;publicvoidaddPlayer(){InputStreamis=getClass().getResourceAsStream("/md10.mid");try{p=Manager.createPlayer(is,"audio/midi");p.prefetch();p.setLoopCount(-1);}catch(IOExceptione){e.printStackTrace();}catch(MediaExceptione){e.printStackTrace();}}//读取图片原理publicImageloadImage(Stringfname){Imageim=null;try{im=Image.createImage("/"+fname);}catch(Exceptione){System.out.println(fname+"出错了");}returnim;}//绘制离屏画笔publicvoidpaint(Graphicsg){//贴图g.drawImage(offScreen,0,0,Graphics.LEFT|Graphics.TOP);}//第一张logopublicvoiddrawLogo(){offg.drawImage(imlogo,0,0,0);}//自己的界面publicvoiddrawCP(){offg.drawImage(imlog,0,0,0);}//菜单publicvoiddrawMenu(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);temp++;if(temp>=4){temp=0;}offg.drawImage(immenu,0,0,0);if(menuPy==105){offg.drawImage(kaishi,55+temp,105,0);}else{offg.drawImage(kaishi,55,105,0);}if(menuPy==138){offg.drawImage(xuanxiang,55+temp,138,0);}else{offg.drawImage(xuanxiang,55,138,0);}if(menuPy==171){offg.drawImage(bangzhu,55+temp,171,0);}else{offg.drawImage(bangzhu,55,171,0);}if(menuPy==204){offg.drawImage(guanyu,55+temp,204,0);}else{offg.drawImage(guanyu,55,204,0);}if(menuPy==237){offg.drawImage(tuichu,55+temp,237,0);}else{offg.drawImage(tuichu,55,237,0);}offg.drawImage(immenupoint,165,menuPy,0);}publicvoiddrawAbout(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.setColor(0xFFFFFF);offg.drawString("关于",30,30,0);offg.drawString("返回",screenW-50,screenH-40,0);}publicvoiddrawHelp(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.setColor(255,0,0);offg.drawString("你可以在游戏中按方向键或数字键移动",screenW/2,10,17);offg.drawString("在你找到对手之前他是隐藏的",screenW/2,40,17);offg.drawString("找到对手猜拳赢了得一球",screenW/2,70,17);offg.drawString("请在红色线上按5可以跳转场地",screenW/2,100,17);offg.drawString("每个场地只有一球可以拿到",screenW/2,130,17);offg.drawString("攒够5个开始点球大战",screenW/2,160,17);offg.drawString("祝你好运,自动跳转至游戏",screenW/2,190,17);offg.drawString("返回主菜单",6,300,0);offg.drawString("直接开始",190,300,0);}publicvoiddrawSound(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(immenu,0,0,0);offg.setColor(255,0,0);offg.drawString("游戏设置",screenW/2,screenH/3,17);offg.drawString("开启声音?",screenW/2,190,17);offg.drawString("是",6,300,0);offg.drawString("否",216,300,0);}publicvoiddrawStop(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.setColor(255,0,0);offg.drawString("返回主菜单?按5继续",screenW/2,screenH/2,17);offg.drawString("是",6,300,0);offg.drawString("否",216,300,0);}publicvoidQingping(){offg.setColor(0);offg.setClip(0,0,240,320);}//玩家控制的人物publicvoiddrawPlay(){offg.setClip(rx,ry,16,32);offg.drawImage(img_play,rx-rloop*16,ry-dir*32,0);}//第1个人物publicintx;publicinty;publicvoidRenwu0(){offg.setClip(x,y,16,32);offg.drawImage(img4,x-rloop*16,y-dir*32,0);}//第2个人物publicvoidRenwu1(){offg.setClip(x,y,16,32);offg.drawImage(img1,x-rloop*16,y-dir*32,0);}//第3个人物publicvoidRenwu2(){offg.setClip(x,y,16,32);offg.drawImage(img2,x-rloop*16,y-dir*32,0);}//第4个人物publicvoidRenwu3(){offg.setClip(x,y,16,32);offg.drawImage(img3,x-rloop*16,y-dir*32,0);}//循环运行的
解决方案
解决方案二:
publicvoidrun(){while(true){rloop++;if(rloop>3){rloop=0;}//处理游戏逻辑switch(this.appState){caseAPP_LOGO:count++;if(count>15){count=0;this.appState=APP_CP;}break;caseAPP_CP:count++;if(count>15){this.appState=APP_SOUND;}break;caseAPP_SOUND:count++;if(count>40){count=0;this.appState=APP_MENU;this.imlogo=null;this.imlog=null;System.gc();}break;caseAPP_MENU:break;caseAPP_HELP:break;caseAPP_MAP1:Qiu=1;XIANSHI=false;break;caseAPP_MAP2:XIANSHI=false;break;caseAPP_MAP3:XIANSHI=false;break;caseAPP_MAP4:XIANSHI=false;break;caseAPP_puanduan:/*this.appState=APP_MAP;*/break;caseAPP_GAME:break;caseAPP_GAME2:Qy=242;Zx=58;//左的坐标Yx=140;tt=0;PuJiu=false;break;caseAPP_GAME21:switch(Mx){case66:switch(S){case0:if(Qy==116)//确定{this.appState=MyCanvas.APP_PUYOU;}break;case1:if(Qy==116)//确定{Jinqiu1+=1;this.appState=MyCanvas.APP_GAME2;}break;}break;case146:switch(S){case0:if(Qy==116)//确定{Jinqiu1+=1;this.appState=MyCanvas.APP_GAME2;}break;case1:if(Qy==116)//确定{this.appState=MyCanvas.APP_PUZUO;}break;}break;}break;caseAPP_GAME22:Qy=242;Zx=58;//左的坐标Yx=140;tt=0;break;caseAPP_GAME23:switch(K){case0:if(O==2){if(Qy==116)//确定{this.appState=MyCanvas.APP_GAME22;Jinqiu2+=1;}}elseif(O==1){if(Qy==116)//确定{this.appState=MyCanvas.APP_PUZUO1;}}break;case1:if(O==1){if(Qy==116)//确定{this.appState=MyCanvas.APP_GAME22;Jinqiu2+=1;}}elseif(O==2){if(Qy==116)//确定{this.appState=MyCanvas.APP_PUYOU1;}}break;}break;caseAPP_PUZUO:if(tt<-20){this.appState=MyCanvas.APP_GAME2;//switch状态是在踢还是守}break;caseAPP_PUYOU:if(tt>20){this.appState=MyCanvas.APP_GAME2;}break;caseAPP_PUZUO1:if(tt<-20){this.appState=MyCanvas.APP_GAME22;//switch状态是在踢还是守}break;caseAPP_PUYOU1:if(tt>20){this.appState=MyCanvas.APP_GAME22;}break;caseAPP_YUYAN:break;caseAPP_ABOUT:break;caseAPP_LOADING:break;caseAPP_LOAD:break;caseAPP_OVER:break;caseAPP_SHU:break;caseAPP_YING:break;caseAPP_PING:break;caseAPP_LOD:break;caseAPP_SY:count++;if(count>30){if(Jinqiu1>Jinqiu2){this.appState=MyCanvas.APP_YING;}elseif(Jinqiu1<Jinqiu2){this.appState=MyCanvas.APP_SHU;}elseif(Jinqiu1==Jinqiu2){this.appState=MyCanvas.APP_PING;}}break;}//球的碰撞if(isColZ(Qx,Qy,Qw,Qh,Zx,Zy,Zw,Zh)){Visibal=true;}if(isColY(Qx,Qy,Qw,Qh,Yx,Yy,Yw,Yh)){Visibal=true;}//处理屏幕绘制switch(this.appState){caseAPP_LOGO:drawLogo();break;caseAPP_CP:drawCP();break;caseAPP_SOUND:drawSound();break;caseAPP_MENU:drawMenu();break;caseAPP_LOADING:drawStop();break;caseAPP_LOAD:drawStop();break;caseAPP_LOD:drawStop();break;caseAPP_OVER:JifenQiu();break;caseAPP_MAP1:drawMap1();break;caseAPP_MAP2:drawMap2();break;caseAPP_MAP3:drawMap3();break;caseAPP_MAP4:drawMap4();break;caseAPP_puanduan:PanDuan();break;caseAPP_YUYAN:Yuyan1();break;caseAPP_GAME:drawGame();break;caseAPP_GAME2:drawGame2();break;caseAPP_GAME21:DrawGame21();break;caseAPP_GAME22:DrawGame22();break;caseAPP_GAME23:DrawGame23();break;caseAPP_PUZUO:tt-=3;ZuoQiu();break;caseAPP_PUYOU:tt+=3;YouQiu();break;caseAPP_PUZUO1:tt-=3;QiuZuo();break;caseAPP_PUYOU1:tt+=3;QiuYou();break;caseAPP_ABOUT:drawAbout();break;caseAPP_HELP:drawHelp();break;caseAPP_SY:ShuYing();break;caseAPP_SHU:Shu();break;caseAPP_YING:Ying();break;caseAPP_PING:Ping();break;}move();playRC();repaint();try{Thread.sleep(200);}catch(Exceptione){}}}//画背景
解决方案三:
publicvoidTishi(){offg.setColor(255,0,0);offg.drawString("我的位置:"+rx+"_"+ry,screenW/2,0,17);offg.drawString("我的球数:"+Qiu,40,160,17);offg.drawString("他的位置:"+x+"_"+y,screenW/2,300,17);}publicvoidC1(){offg.setClip(0,0,240,320);offg.drawImage(img_map1,0,0,0);//背景图片}publicvoidR1(){offg.setClip(208,286,32,34);offg.drawImage(r1,208,286,0);//背景图片}publicvoidR2(){offg.setClip(0,286,32,34);offg.drawImage(r2,0,286,0);//背景图片}publicvoidR3(){offg.setClip(208,0,32,34);offg.drawImage(r3,208,0,0);//背景图片}publicvoidR0(){offg.setClip(0,0,32,35);offg.drawImage(r0,0,0,0);//背景图片}publicvoiddrawMap1(){C1();Tishi();R1();drawPlay();if(rx>x-26&&rx<x+26&&ry+42>y&&ry<y+42){Renwu1();XIANSHI=true;}}publicvoidC2(){offg.setClip(0,0,240,320);offg.drawImage(img_map2,0,0,0);//背景图片}publicvoiddrawMap2(){C2();Tishi();R2();drawPlay();if(rx>x-26&&rx<x+26&&ry+42>y&&ry<y+42){Renwu2();XIANSHI=true;}}publicvoidC3(){offg.setClip(0,0,240,320);offg.drawImage(img_map3,0,0,0);//背景图片}publicvoiddrawMap3(){C3();Tishi();R3();drawPlay();if(rx>x-26&&rx<x+26&&ry+42>y&&ry<y+42){Renwu3();XIANSHI=true;}}publicvoidC4(){offg.setClip(0,0,240,320);offg.drawImage(img_map4,0,0,0);//背景图片}publicvoiddrawMap4(){C4();Tishi();R0();drawPlay();if(rx>x-26&&rx<x+26&&ry+42>y&&ry<y+42){Renwu0();XIANSHI=true;}}//猜拳部分的绘画publicvoiddrawGame(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(bg,0,0,0);//背景图片offg.setClip(70,220,100,100);offg.drawImage(C_k,70,220,0);//电脑头像offg.setClip(70,0,100,100);offg.drawImage(tou_k,70,0,0);//人物头像offg.setClip(144,138,17,44);offg.drawImage(img_you,img_x2,138,0);offg.drawImage(img_you,img_x2-22,138,0);//向左箭头offg.setClip(78,138,17,44);offg.drawImage(img_zuo,img_x1,138,0);offg.drawImage(img_zuo,img_x1+22,138,0);//向zuo箭头offg.setClip(0,122,77,71);//人物手势offg.drawImage(img_s1,0,s1y,0);offg.setClip(13,100,44,22);//上offg.drawImage(img_shang,13,100,0);offg.setClip(13,197,44,22);//下offg.drawImage(img_xia,13,197,0);//电脑随机chuquan3();}//在地图上处理的publicvoidYuyan1(){offg.setClip(80,140,76,38);offg.drawImage(setout,80,140,0);//背景图片}publicbooleanXIANSHI=false;publicintH2=Math.abs(rdm.nextInt())%4;publicintkaiju=3;//电脑的出拳滚动publicvoidchuquan3(){offg.setClip(163,122,77,71);//电脑动offg.drawImage(img_s1,163,122-71*action[H2][loop],0);loop++;if(loop>3){loop=0;}}//石头publicvoidchuquan1(){offg.setClip(163,122,77,71);//电脑动offg.drawImage(石头,163,122,0);}//剪刀publicvoidchuquan0(){offg.setClip(163,122,77,71);//电脑动offg.drawImage(剪刀,163,122,0);}//布publicvoidchuquan2(){offg.setClip(163,122,77,71);//电脑动offg.drawImage(布,163,122,0);}//对对手的处理publicvoidJifenQiu(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(bg,0,0,0);//背景图片offg.setColor(255,0,0);offg.drawString("身无分文,一个球也没有",screenW/2,screenH/3,17);offg.drawString("返回主菜单?按5继续",screenW/2,190,17);offg.drawString("是",6,300,0);offg.drawString("否",216,300,0);offg.setClip(70,0,100,100);offg.drawImage(meiqiu,70,0,0);//人物头像offg.setClip(70,220,100,100);offg.drawImage(dianying,70,220,0);//电脑头像}publicvoidwoshng1(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(bg,0,0,0);//背景图片offg.setColor(255,0,0);offg.drawString("你赢了,球的数量:"+Qiu,screenW/2,screenH/3,17);offg.setClip(70,220,100,100);offg.drawImage(shu1,70,220,0);//电脑头像offg.setClip(70,0,100,100);offg.drawImage(ying,70,0,0);//人物头像offg.setClip(0,122,77,71);//人物手势offg.drawImage(img_s1,0,s1y,0);switch(kaiju){case0:chuquan0();break;case1:chuquan1();break;case2:chuquan2();break;}}publicvoiddiannaosheng1(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(bg,0,0,0);//背景图片offg.setColor(255,0,0);offg.drawString("赢了我才能得到球,球的数量:"+Qiu,screenW/2,screenH/3,17);offg.setClip(70,220,100,100);offg.drawImage(ying1,70,220,0);//电脑头像offg.setClip(70,0,100,100);offg.drawImage(shu,70,0,0);//人物头像offg.setClip(0,122,77,71);//人物手势offg.drawImage(img_s1,0,s1y,0);switch(kaiju){case0:chuquan0();break;case1:chuquan1();break;case2:chuquan2();break;}}publicvoidpingju(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(bg,0,0,0);//背景图片offg.setColor(255,0,0);offg.drawString("平局,差一点,球的数量:"+Qiu,screenW/2,screenH/3,17);offg.setClip(70,220,100,100);offg.drawImage(C_k,70,220,0);//电脑头像offg.setClip(70,0,100,100);offg.drawImage(tou_k,70,0,0);//人物头像offg.setClip(0,122,77,71);//人物手势offg.drawImage(img_s1,0,s1y,0);switch(kaiju){case0:chuquan0();break;case1:chuquan1();break;case2:chuquan2();break;}}//判断输赢publicintVS=0;publicvoidPanDuan(){if(-20==s1y&&kaiju==1){//System.out.println("选手胜");woshng1();VS=1;}elseif(-20==s1y&&kaiju==0){//System.out.println("电脑胜");diannaosheng1();VS=2;}elseif(-20==s1y&&kaiju==2){//System.out.println("平局");pingju();}elseif(51==s1y&&kaiju==1){//System.out.println("电脑胜");diannaosheng1();VS=2;}elseif(51==s1y&&kaiju==0){//System.out.println("平局");pingju();}elseif(51==s1y&&kaiju==2){//System.out.println("选手胜");VS=1;woshng1();}elseif(s1y==122&&kaiju==1){//System.out.println("平局");pingju();}elseif(s1y==122&&kaiju==0){//System.out.println("选手胜");VS=1;woshng1();}elseif(s1y==122&&kaiju==2){//System.out.println("电脑胜");VS=2;diannaosheng1();}}//按键处理publicintC1=0;publicvoidkeyPressed(intn){switch(this.appState){//这里是关于caseMyCanvas.APP_ABOUT:if(n==-7||n==22||n==35){this.appState=MyCanvas.APP_MENU;}break;//这里是声音caseMyCanvas.APP_SOUND:if(n==-7){try{p.stop();}catch(MediaExceptione){e.printStackTrace();}this.appState=MyCanvas.APP_MENU;}elseif(n==-6||n==-5||n==53){try{p.start();}catch(MediaExceptione){e.printStackTrace();}this.appState=MyCanvas.APP_MENU;}break;//这里是帮助caseMyCanvas.APP_HELP:if(n==-7||n==53||n==-5||n==22||n==35){x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP1;}elseif(n==-6||n==21||n==42){this.appState=MyCanvas.APP_MENU;}break;//这里是菜单caseMyCanvas.APP_MENU:if(n==56||n==-2){if(menuPy<237){menuPy+=33;}else{menuPy=105;}}if(n==50||n==-1){if(menuPy>105){menuPy-=33;}else{menuPy=237;}}if(n==53||n==-5)//确定{switch(menuPy){case105://开始x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP1;break;case138://选项this.appState=MyCanvas.APP_SOUND;break;case171://帮助this.appState=MyCanvas.APP_HELP;break;case204://关于//this.appState=MyCanvas.APP_GAME2;/****************跳转应该是关于*************************************************/this.appState=MyCanvas.APP_ABOUT;break;case237://退出Midlet.midlet.notifyDestroyed();}}break;//这里是暂停caseMyCanvas.APP_LOADING:if(n==-7||n==53||n==-5||n==22||n==35){x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP1;}elseif(n==-6||n==21||n==42){this.appState=MyCanvas.APP_MENU;}break;
解决方案四:
caseMyCanvas.APP_MAP1:C1=1;if(n==-7||n==22||n==35){this.appState=MyCanvas.APP_LOADING;}if(n==-1||n==50){//方向上dir=UP;speed=10;}elseif(n==-2||n==56){dir=DOWN;speed=10;}elseif(n==-3||n==52){dir=LEFT;speed=10;}elseif(n==-4||n==54){dir=RIGHT;speed=10;}if(XIANSHI){if(n==53||n==-5)//确定{this.appState=MyCanvas.APP_YUYAN;}}if(ry<288){if(rx>224){if(n==53||n==-5)//确定{x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP2;}}}elseif(rx<244){if(ry>288){if(n==53||n==-5)//确定{x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP3;}}}break;caseMyCanvas.APP_MAP2:C1=2;if(n==-7||n==22||n==35){this.appState=MyCanvas.APP_LOADING;}if(n==-1||n==50){//方向上dir=UP;speed=10;}elseif(n==-2||n==56){dir=DOWN;speed=10;}elseif(n==-3||n==52){dir=LEFT;speed=10;}elseif(n==-4||n==54){dir=RIGHT;speed=10;}if(XIANSHI){if(n==53||n==-5)//确定{this.appState=MyCanvas.APP_YUYAN;}}if(ry<288){if(rx<0){if(n==53||n==-5)//确定{x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP1;}}}else{if(n==53||n==-5)//确定{x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP4;}}break;caseMyCanvas.APP_MAP3:C1=3;if(n==-7||n==22||n==35){this.appState=MyCanvas.APP_LOADING;}if(n==-1||n==50){//方向上dir=UP;speed=10;}elseif(n==-2||n==56){dir=DOWN;speed=10;}elseif(n==-3||n==52){dir=LEFT;speed=10;}elseif(n==-4||n==54){dir=RIGHT;speed=10;}if(XIANSHI){if(n==53||n==-5)//确定{this.appState=MyCanvas.APP_YUYAN;}}if(rx<224){if(ry<0){if(n==53||n==-5)//确定{x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP1;}}}elseif(ry>0){if(rx>224){if(n==53||n==-5)//确定{x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP4;}}}break;caseMyCanvas.APP_MAP4:C1=4;if(n==-7||n==22||n==35){this.appState=MyCanvas.APP_LOADING;}if(n==-1||n==50){//方向上dir=UP;speed=10;}elseif(n==-2||n==56){dir=DOWN;speed=10;}elseif(n==-3||n==52){dir=LEFT;speed=10;}elseif(n==-4||n==54){dir=RIGHT;speed=10;}if(XIANSHI){if(n==53||n==-5)//确定{this.appState=MyCanvas.APP_YUYAN;}}if(rx>0){if(ry<0){if(n==53||n==-5)//确定{x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP2;}}}else{if(n==53||n==-5)//确定{x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP3;}}break;caseMyCanvas.APP_GAME:if(n==-7||n==22||n==35){this.appState=MyCanvas.APP_LOADING;}if(n==56||n==-2){if(s1y<122){s1y+=71;}else{s1y=-20;}}if(n==50||n==-1){if(s1y>-20){s1y-=71;}else{s1y=122;}}if(n==53||n==-5)//确定{kaiju=Math.abs(rdm.nextInt())%2;this.appState=MyCanvas.APP_puanduan;}break;caseMyCanvas.APP_puanduan:switch(C1){case1:if(n==53||n==-5)//确定{switch(VS){case1:Qiu+=1;if(Qiu>=2){x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP2;}break;case2:Qiu-=1;if(Qiu<1){this.appState=MyCanvas.APP_OVER;}else{x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP1;}break;}}break;case2:if(n==53||n==-5)//确定{switch(VS){case1:Qiu+=1;if(Qiu>=3){x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP3;}break;case2:Qiu-=1;if(Qiu<2){x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP1;}break;}}break;case3:if(n==53||n==-5)//确定{switch(VS){case1:Qiu+=1;if(Qiu>=4){x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP4;}break;case2:Qiu-=1;if(Qiu<3){x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP2;}break;}}break;case4:if(n==53||n==-5)//确定{switch(VS){case1:Qiu+=1;if(Qiu>=5){this.appState=MyCanvas.APP_GAME2;}else{this.appState=MyCanvas.APP_MAP4;}break;case2:Qiu-=1;if(Qiu<4){x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP3;}break;}}break;}break;caseMyCanvas.APP_LOAD:if(n==-7||n==53||n==-5||n==22||n==35){this.appState=MyCanvas.APP_GAME2;}elseif(n==-6||n==21||n==42){this.appState=MyCanvas.APP_MENU;}break;caseMyCanvas.APP_GAME2://对球的处理if(n==-7||n==22||n==35){this.appState=MyCanvas.APP_LOAD;}if(n==53||n==-5)//确定{this.appState=MyCanvas.APP_GAME21;S=Math.abs(rdm.nextInt()%2);Qiu-=1;}if(Qiu<0){this.appState=MyCanvas.APP_GAME22;}break;caseMyCanvas.APP_GAME21://对球的处理if(n==-7||n==22||n==35){this.appState=MyCanvas.APP_LOAD;}break;caseMyCanvas.APP_LOD:if(n==-7||n==53||n==-5||n==22||n==35){this.appState=MyCanvas.APP_GAME22;}elseif(n==-6||n==21||n==42){this.appState=MyCanvas.APP_MENU;}break;caseMyCanvas.APP_GAME22:if(n==-7||n==22||n==35){this.appState=MyCanvas.APP_LOD;}switch(n){case52:K=Math.abs(rdm.nextInt()%2);PU=1;Qiu1-=1;O=1;this.appState=MyCanvas.APP_GAME23;break;case-3:K=Math.abs(rdm.nextInt()%2);PU=1;Qiu1-=1;O=1;this.appState=MyCanvas.APP_GAME23;break;case54:PU=2;Qiu1-=1;O=2;K=Math.abs(rdm.nextInt()%2);this.appState=MyCanvas.APP_GAME23;break;case-4:PU=2;Qiu1-=1;O=2;K=Math.abs(rdm.nextInt()%2);this.appState=MyCanvas.APP_GAME23;break;}if(Qiu1<0){this.appState=MyCanvas.APP_SY;}break;caseMyCanvas.APP_GAME23:if(n==-7||n==22||n==35){this.appState=MyCanvas.APP_LOD;}break;caseMyCanvas.APP_YUYAN:if(XIANSHI){if(n==53||n==-5)//确定{this.appState=MyCanvas.APP_GAME;}}break;caseMyCanvas.APP_OVER:if(n==-7||n==53||n==-5||n==22||n==35){Qiu=1;x=Math.abs(rdm.nextInt()%200);y=Math.abs(rdm.nextInt()%300);this.appState=MyCanvas.APP_MAP1;}elseif(n==-6||n==21||n==42){this.appState=MyCanvas.APP_MENU;}break;caseMyCanvas.APP_SY:break;caseAPP_SHU:if(n==-7||n==22||n==35){this.appState=MyCanvas.APP_MENU;}elseif(n==-6||n==53||n==-5||n==21||n==42){this.appState=MyCanvas.APP_MAP1;}break;caseAPP_YING:if(n==-7||n==22||n==35){this.appState=MyCanvas.APP_MENU;}elseif(n==-6||n==53||n==-5||n==21||n==42){this.appState=MyCanvas.APP_MAP1;}break;caseAPP_PING:if(n==-7||n==22||n==35){this.appState=MyCanvas.APP_MENU;}elseif(n==-6||n==53||n==-5||n==21||n==42){this.appState=MyCanvas.APP_MAP1;}break;}}publicintO;
解决方案五:
还有呢
解决方案六:
publicvoidkeyReleased(intn){speed=0;}publicvoidmove(){switch(dir){caseUP:ry-=speed;break;caseDOWN:ry+=speed;break;caseRIGHT:rx+=speed;break;caseLEFT:rx-=speed;break;}}//碰撞处理//地图的碰撞publicvoidplayRC(){switch(dir){caseUP:if(ry<-16){ry=-16;}break;caseDOWN:if(ry>300){ry=300;}break;caseLEFT:if(rx<-8){rx=-8;}break;caseRIGHT:if(rx>232){rx=232;}break;}}//第二关//定义与球相关的坐标publicintMx=66;//准星的坐标publicintZx=58;//左的坐标publicintZy=100,Zw=42,Zh=16;publicintYx=140;//右的坐标publicintYy=100,Yw=42,Yh=16;publicintQy=242;//球的publicintQx;//球的publicintQw=16,Qh=16;//踢球前publicvoiddrawGame2(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(chang,0,0,0);offg.setColor(255,0,0);offg.drawString("按5射门,5次机会,剩余:"+Qiu,screenW/2,screenH/2,17);Qiu();Miaozhun();//System.out.println("这是第二关");}//踢球后publicintS=2;//显示的动作publicvoidDongzuo(){switch(S){case0:YouPu();break;case1:ZuoPu();break;case2:Deng();break;}//左扑或右扑//出球Qy-=15;if(Qy<116){Qy=116;}switch(Mx){case146:if(!Visibal){offg.setClip(66,Qy,16,15);offg.drawImage(qiu,66-16*lp,Qy,0);lp++;if(lp>3){lp=0;}}break;case66:if(!Visibal){offg.setClip(166,Qy,16,15);offg.drawImage(qiu,166-16*lp,Qy,0);lp++;if(lp>3){lp=0;}}break;}}publicvoidDrawGame21(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(chang,0,0,0);Tiren();Dongzuo();}publicbooleanVisibal;publicintlp=0;publicvoidQiu(){offg.setClip(112,Qy,16,15);offg.drawImage(qiu,112-16*lp,Qy,0);lp++;if(lp>3){lp=0;}Deng();}publicvoidDeng(){offg.setClip(100,100,37,28);offg.drawImage(等待,100,100,0);}publicvoidTiren(){offg.setClip(100,225,37,51);offg.drawImage(ti1,100-37*lp,225,0);lp++;if(lp>3){lp=0;}}publicvoidMiaozhun(){offg.setClip(Mx,92,27,26);offg.drawImage(zhunxing,Mx,92,0);if(Mx<146){Mx+=80;}else{Mx=66;}}publicbooleanPuJiu=false;//碰撞函数publicbooleanisColZ(intQx,intQy,intQw,intQh,intZx,intZy,intZw,intZh){if(Qx+Qw<Zx||Zx+Zw<Qx||Qy+Qh<Zy||Zy+Zh<Qy){returnfalse;}returntrue;}publicbooleanisColY(intQx,intQy,intQw,intQh,intYx,intYy,intYw,intYh){if(Qx+Qw<Yx||Yx+Yw<Qx||Qy+Qh<Yy||Yy+Yh<Qy){returnfalse;}returntrue;}//换防publicvoidZuoPu(){offg.setClip(Zx,100,42,16);offg.drawImage(左扑,Zx,100,0);Zx-=2;if(Zx<45){Zx=45;}}publicvoidYouPu(){offg.setClip(Yx,100,42,16);offg.drawImage(右扑,Yx,100,0);Yx+=2;if(Yx>155){Yx=155;}}publicvoidDrawGame22(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(chang,0,0,0);offg.setColor(255,0,0);offg.drawString("按4或6或左右扑救,5次机会,剩余:"+Qiu1,screenW/2,screenH/2,17);Qiu();Tiren();//左右Deng();offg.setClip(0,90,17,44);offg.drawImage(img_zuo,30,90,0);offg.setClip(223,90,17,44);offg.drawImage(img_you,160,90,0);}publicintPU=0;publicvoidDrawGame23(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(chang,0,0,0);Tiren();switch(PU){case1:ZuoPu();break;case2:YouPu();break;}Fang();}publicintK=2;publicvoidFang(){//左扑或右扑//出球Qy-=15;if(Qy<116){Qy=116;}switch(K){case0:if(!Visibal){offg.setClip(66,Qy,16,15);offg.drawImage(qiu,66-16*lp,Qy,0);lp++;if(lp>3){lp=0;}}break;case1:if(!Visibal){offg.setClip(166,Qy,16,15);offg.drawImage(qiu,166-16*lp,Qy,0);lp++;if(lp>3){lp=0;}}break;}}//踢球扑到后的状态//左出Qx=66-publicinttt=0;publicvoidZuoQiu(){Qingping();offg.setClip(0,0,240,320);offg.drawImage(chang,0,0,0);switch(S){case0:YouPu();break;case1:ZuoPu();break;case2:Deng();break;}//左扑或右扑Tiren();offg.setClip(66+tt,116,16,15);offg.drawImage(qiu,66+tt-16*lp,116,0);lp++;if(lp>3){lp=0;}}//右出Qx=146+publicvoidYouQiu(){Qingping();offg.setClip(0,0,240,320);offg.drawImage(chang,0,0,0);switch(S){case0:YouPu();break;case1:ZuoPu();break;case2:Deng();break;}//左扑或右扑Tiren();offg.setClip(166+tt,116,16,15);offg.drawImage(qiu,166+tt-16*lp,116,0);lp++;if(lp>3){lp=0;}}//防守扑到后publicvoidQiuZuo(){Qingping();offg.setClip(0,0,240,320);offg.drawImage(chang,0,0,0);ZuoPu();Tiren();offg.setClip(66+tt,116,16,15);offg.drawImage(qiu,66+tt-16*lp,116,0);lp++;if(lp>3){lp=0;}}publicvoidQiuYou(){Qingping();offg.setClip(0,0,240,320);offg.drawImage(chang,0,0,0);YouPu();Tiren();offg.setClip(166+tt,116,16,15);offg.drawImage(qiu,166+tt-16*lp,116,0);lp++;if(lp>3){lp=0;}}/**********Qiu应该是1************************************/publicintQiu=1,Qiu1=5;//定义点球数量publicintJifen1=0,Jifen2;//定义结果publicintJinqiu1=0,Jinqiu2=0;publicvoidShuYing(){//左右判定输赢球offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(immenu,0,0,0);offg.setColor(255,0,0);offg.drawString("等待数据",screenW/2,screenH/2,17);}publicvoidShu(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(bg,0,0,0);//背景图片offg.setColor(255,0,0);offg.drawString("结果",screenW/2,screenH/3,17);offg.drawString("比分是_"+Jinqiu1+":"+Jinqiu2,screenW/2,190,17);offg.drawString("从新来过",6,300,0);offg.drawString("返回主菜单",216,300,0);offg.setClip(70,220,100,100);offg.drawImage(dianying,70,220,0);//电脑头像offg.setClip(70,0,100,100);offg.drawImage(woshu,70,0,0);//人物头像}publicvoidYing(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(bg,0,0,0);//背景图片offg.setColor(255,0,0);offg.drawString("结果",screenW/2,screenH/3,17);offg.drawString("比分是_"+Jinqiu1+":"+Jinqiu2,screenW/2,190,17);offg.drawString("从新来过",6,300,0);offg.drawString("返回主菜单",216,300,0);offg.setClip(70,220,100,100);offg.drawImage(dianshu,70,220,0);//电脑头像offg.setClip(70,0,100,100);offg.drawImage(woying,70,0,0);//人物头像}publicvoidPing(){offg.setColor(0);offg.setClip(0,0,screenW,screenH);offg.drawImage(bg,0,0,0);//背景图片offg.setColor(255,0,0);offg.drawString("结果",screenW/2,screenH/3,17);offg.drawString("比分是_"+Jinqiu1+":"+Jinqiu2,screenW/2,190,17);offg.drawString("从新来过",6,300,0);offg.drawString("返回主菜单",216,300,0);offg.setClip(70,220,100,100);offg.drawImage(C_k,70,220,0);//电脑头像offg.setClip(70,0,100,100);offg.drawImage(tou_k,70,0,0);//人物头像}}
解决方案七:
java.lang.OutOfMemoryError是指你的资源占用太大的手机内存,可能是你的声音文件太大了,或者的图片太多,不能通过。。。
解决方案八:
试着优化或是减少图片资源~~
解决方案九:
支持``````·引用5楼qweeqqqw1的回复:
publicvoidkeyReleased(intn){speed=0;}publicvoidmove(){switch(dir){caseUP:ry-=speed;break;caseDOWN:ry+=speed;break;caseRIGHT:rx+=speed;break;caseLEFT:……
解决方案十:
引用6楼huayu_huayu的回复:
java.lang.OutOfMemoryError是指你的资源占用太大的手机内存,可能是你的声音文件太大了,或者的图片太多,不能通过。。。
现在是打开游戏白屏,好像是自动切换的原因,怎么办??
解决方案十一:
内存爆了
解决方案十二:
太长了不想看了我也遇到过这种问题内存溢出我的问题是用了大图片就溢出了LZ根据自己的情况好好查一下
解决方案十三:
你打包完后jar有多大?
解决方案十四:
屁点的一个程序都搞的内存泄露了,估计是很多地方没有释放内存,建议看点有关程序优化方面的资料,再重构你自己的代码
解决方案十五:
溢出,只要重启tomcat,不行的话,重启MyEclipse...
解决方案:
1.image对象太多了!不要去挑战手机的内存,她是很脆弱的。载入图片的前后分别看看freememory得值。2.有可能是自己在程序里面做了一个死循环什么的。做一下跟踪吧!
解决方案:
内存溢出了,通常导致内存溢出的主要原因应该是图片优化一下图片的处理,和美工讨论一下,尽可能的把图片切小
解决方案:
关注一下图片大小,及画面间的切换,资源释放问题