问题描述
我的代码如下,怎样实现单击中间键实现页面跳转呢,确切的说在keyPressed()方法或menuSelect()方法中代码该怎样写呢importjavax.microedition.midlet.*;importjavax.microedition.lcdui.*;importjavax.microedition.lcdui.game.*;importjavax.microedition.m3g.*;importjavax.microedition.m3g.Camera;publicclassMenuCanvasextendsGameCanvasimplementsRunnable{privateStarHScrollerscroller;//scrollstarsonthescreen.privateAlertalert;//displayinfotextonthescreenprivateGraphics3Dg3d;privateWorldworld;privateMeshmesh;privateCameracamera;//Theappearancesisusedforeachmenutexture.privateAppearanceappearance1;privateAppearanceappearance2;privateAppearanceappearance3;privateAppearanceappearance4;privatefinalintWIDTH,HEIGHT;//thecanvassize//themenueoptionsprivateString[]menu=newString[]{"Playgame","Highscores...","About...","Help...","Options...","Quit"};privatefinalintMENU_SIZE=menu.length;privateTexture2D[]texMenu=newTexture2D[MENU_SIZE];privateintindex=0;//menuindexprivateintface=0;//cubeface0-3//thecurrentangleofthemenu.privateintmenu_angle=0;privateintmenu_rot=0;//-1or1privateUIcontrolleruicontroller;privateChess_MIDletmidlet;publicMenuCanvas(UIcontrolleruicontroller){super(false);this.uicontroller=uicontroller;//midlet=m;setFullScreenMode(true);WIDTH=getWidth();HEIGHT=getHeight();g3d=Graphics3D.getInstance();world=newWorld();camera=newCamera();camera.setPerspective(60.0f,(float)WIDTH/(float)HEIGHT,0.1f,50.0f);world.addChild(camera);world.setActiveCamera(camera);mesh=MC.createItem();//createthemenumeshmesh.translate(0.0f,-3.0f,-11.0f);//menupositionworld.addChild(mesh);appearance1=mesh.getAppearance(0);//frontappearance2=mesh.getAppearance(1);//backappearance3=mesh.getAppearance(4);//topappearance4=mesh.getAppearance(5);//bottom//generatethemenutexturestouse.createTextures();scroller=newStarHScroller(WIDTH,HEIGHT);newThread(this).start();}/**Createonetextureforeachoptioninthenenu.*/privatevoidcreateTextures(){Imageimage;try{for(inti=0;i<menu.length;i++){image=createMenuImage(menu[i]);//createanimagefromtexttexMenu[i]=newTexture2D(newImage2D(Image2D.RGB,image));texMenu[i].setFiltering(Texture2D.FILTER_LINEAR,Texture2D.FILTER_LINEAR);texMenu[i].setWrapping(Texture2D.WRAP_CLAMP,Texture2D.WRAP_CLAMP);}}catch(Exceptione){e.printStackTrace();}appearance1.setTexture(0,texMenu[0]);appearance3.setTexture(0,texMenu[1]);appearance4.setTexture(0,texMenu[5]);}/**Createanimagefromthetextsenttothemethod*/privateImagecreateMenuImage(StringimgStr){intbgColor=0xFFFFFF;intfgColor=0x00;Stringmenu;Fontf=Font.getDefaultFont();Imageimage=null;try{ImagebgImage=Image.createImage("/menubg.png");image=Image.createImage(128,32);Graphicsg=image.getGraphics();g.setFont(f);g.setColor(bgColor);g.fillRect(0,0,image.getWidth(),image.getHeight());g.drawImage(bgImage,0,0,0);g.setColor(fgColor);menu=imgStr;g.drawString(menu,image.getWidth()/2-f.stringWidth(menu)/2,7,Graphics.TOP|Graphics.LEFT);}catch(Exceptione){e.printStackTrace();}returnimage;}/**whenthemenuisrotatingthetexturesonthecubeischanging.*/privatevoidchangeTextures(){intfront,top,bottom;front=top=bottom=0;Appearancef,t,b;f=appearance1;t=appearance3;b=appearance4;//keeptrackofwhichappearanceisactuallyfacingthescreen.switch(face){case0:f=appearance1;t=appearance3;b=appearance4;break;case1:f=appearance3;t=appearance2;b=appearance1;break;case2:f=appearance2;t=appearance4;b=appearance3;break;case3:f=appearance4;t=appearance1;b=appearance2;break;}front=index;top=index<MENU_SIZE-1?index+1:0;bottom=index>0?index-1:MENU_SIZE-1;//setthetextureforanappearancef.setTexture(0,texMenu[front]);t.setTexture(0,texMenu[top]);b.setTexture(0,texMenu[bottom]);}/**Therenderingmethod.*/publicvoiddraw3D(Graphicsg){try{g3d.bindTarget(g);g3d.render(world);}catch(Exceptione){e.printStackTrace();}finally{g3d.releaseTarget();}}/**Usedwhenthefirebuttonispressed*/privatevoidmenuSelect(){switch(index){case0://displaythetextonthescreen.alert.setText("StartGame");alert.start();break;case1:alert.setText("Highscores");alert.start();break;case2:alert.setText("About");alert.start();break;case3:alert.setText("Help");alert.start();break;case4:alert.setText("Options");alert.start();break;case5://Exit//midlet.notifyDestroyed();break;}}publicvoidkeyPressed(intkey){System.out.println(key);switch(key){case-1://UPif(menu_rot==0){index=index>0?index-1:MENU_SIZE-1;face=face>0?face-1:3;menu_rot=-3;}break;case-2://DOWNif(menu_rot==0){index=index<MENU_SIZE-1?index+1:0;face=face<3?face+1:0;menu_rot=3;}break;case-5://FIREmenuSelect();//调用按钮选择break;}System.out.println("index:"+index);}publicvoidkeyRepeated(intkey){keyPressed(key);}publicvoidrun(){Graphicsgraphics=this.getGraphics();alert=newAlert();intexitCounter=0;floatexitSpeed=1.0f;while(true){//rotatethemenu.if(menu_rot!=0){menu_angle+=menu_rot;mesh.postRotate(menu_rot,1.0f,0.0f,0.0f);if(menu_angle%90==0){menu_rot=0;changeTextures();//调用changeTextures()}}draw3D(graphics);//drawthealerttextif(alert.isStarted()){alert.draw(graphics);}scroller.draw(graphics);flushGraphics();try{Thread.sleep(10);}catch(Exceptione){}}}}
解决方案
解决方案二:
M3G我还没学过。。关注中…………
解决方案三:
没搞过J2ME的3D但是跳转按键处理应该和3D关系不大吧!1:用int或其他模拟选择状态当是什么状态时执行什么跳转方法就OK了啊2:把描绘的每个3D选择条看成对象,在每个选择条设置boolean在按键事件中迭代每个选择条是true的话就执行跳转方法啊。
解决方案四:
不了解额
解决方案五:
这么多代码都写出来了,页面跳转应该不是问题吧!当检测到你设定的键被按下,就设置其它的页面为可以显示的页面首先你怎么设置显示这个MenuCanvas界面的?我没看到你的MIDLet类,用同样的方法显示你想显示的界面或页面,个人认为这个与3D没有关系,只要确认该键被按下,就设另一个页面为显示页面、、、、
解决方案六:
我写了一个uicontroller类,里面写了跳转页面的方法,我在menuselect()方法中调用,没有实现页面的跳转,不知道该在哪里调用,请高手帮忙