java - Why won't my avatar move. -
this code. box not move if click directional key. so, want know what's going on. can me? in advance!
import java.awt.*; import javax.swing.*; import java.applet.*; import java.awt.event.*; import java.awt.robot.*; import java.awt.color.*; public class project extends applet implements mouselistener, mousemotionlistener, keylistener { robot robos; color colour; string s = ""; int mousex = 0, mousey = 0; int width, height; int x, y; int bsx, bsy; int blsx, blsy; int lto, ltt, rto, rtt; boolean canpass; public void init(){ canpass = true; colour = new color(0, 0, 0); addmouselistener(this); addmousemotionlistener(this); addkeylistener(this); width = getsize().width; height = getsize().height; bsx = width / 2 - 3; bsy = height / 2 - 3; blsx = width / 2 - 2; blsy = height / 2 - 2; repaint(); lto = height; ltt = (width / 2)/ 2; rto = height; rtt = (int)((double)((width / 2) * 1.5)); } public void mouseclicked(mouseevent me){ x = me.getx(); y = me.gety(); //this method has no relevance problem. s = ""; repaint(); me.consume(); } public void mouseentered(mouseevent me){ } public void mouseexited(mouseevent me){ } public void mousepressed(mouseevent me){ } public void mousereleased(mouseevent me){ } public void mousedragged(mouseevent me){ } public void keypressed( keyevent me ) { //this method want at(and paint too) if((me.getkeycode() == keyevent.vk_down || me.getkeycode() == keyevent.vk_kp_down) && (bsy + 10 <= height)){ for(int = bsx; < bsx + 5; a++){ colour = robos.getpixelcolor(a, bsy + 6); if(colour.equals(color.black)){ canpass = false; } } if(canpass){ bsy++; blsy++; } } if(me.getkeycode() == keyevent.vk_up || me.getkeycode() == keyevent.vk_kp_up){ for(int = bsx; < bsx + 5; a++){ colour = robos.getpixelcolor(a, bsy - 1); if(colour.equals(color.black)){ canpass = false; } } if(canpass){ bsy--; blsy--; } } if(me.getkeycode() == keyevent.vk_left || me.getkeycode() == keyevent.vk_kp_left){ for(int = bsy + 6; > bsy; a--){ colour = robos.getpixelcolor(bsx - 1, a); if(colour.equals(color.black)){ canpass = false; } } if(canpass){ bsx--; blsx--; } } if(me.getkeycode() == keyevent.vk_right || me.getkeycode() == keyevent.vk_kp_right){ for(int = bsy + 6; < bsy + 11; a++){ colour = robos.getpixelcolor(bsx + 6, a); if(colour.equals(color.black)){ canpass = false; } } if(canpass){ bsx++; blsx++; } } repaint(); me.consume(); canpass = true; } public void keyreleased( keyevent me ) { } public void keytyped( keyevent me ) { } public void mousemoved(mouseevent me){ showstatus("moving mouse @ " + me.getx() + ", " + me.gety()); } public void paint(graphics g){ g.drawline(ltt, lto, ltt, 0); g.drawline(rtt, rto, rtt, 0); g.setcolor(color.cyan); g.drawrect(bsx, bsy, 5, 5); g.setcolor(color.blue); g.fillrect(blsx, blsy, 4, 4); g.setcolor(color.black); g.drawline(0, 400, 500, 400); } }
if possible, can have explanation of what's going wrong know avoid in future when code? once again in advance!
Comments
Post a Comment