キーを入力したら左右に移動したりジャンプしたり
させたいのですがキー入力がされていなかったのですが
原因がわかりません
どうしたらいいでしょうか
ご回答お願い致します
java
1import javax.swing.*; 2import java.awt.*; 3import java.awt.event.*; 4 5class action{ 6 7 public static void main(String args[]){ 8 9 MyFrame mf = new MyFrame("アクションゲーム"); 10 mf.setVisible(true); 11 12 } 13 14} 15 16class MyFrame extends Frame implements ActionListener, MouseMotionListener, MouseListener{ 17 18 int PX = 0; 19 int PY = 0; 20 21 int PlayerX = PX; 22 int PlayerY = PY; 23 24 int x1 = 0; 25 int y1 = 0; 26 int x2 = 100; 27 int y2 = 100; 28 29 boolean flyFlag = true; 30 31 boolean UFlag = false; 32 boolean DFlag = false; 33 boolean LFlag = false; 34 boolean RFlag = false; 35 36 Timer alltimer; 37 38 MyFrame(String title){ 39 40 this.setSize(900, 500); 41 this.setTitle(title); 42 this.setLocationRelativeTo(null); 43 44 this.addWindowListener(new WindowAdapter(){ 45 46 public void windowClosing(WindowEvent we){ 47 48 System.exit(0); 49 50 } 51 52 }); 53 54 addMouseMotionListener(this); 55 addMouseListener(this); 56 57 alltimer = new Timer(1, this); 58 59 alltimer.start(); 60 61 } 62 63 public void paint(Graphics g){ 64 65 int BlockX = 50; 66 int BlockY = 450; 67 68 g.setColor(Color.red); 69 70 g.fillRect(PlayerX, PlayerY, 50, 100); 71 72 g.setColor(new Color(0, 255, 255)); 73 74 g.fillRect(BlockX - 50, BlockY, 50, 50); 75 g.fillRect(BlockX * 1, BlockY, 50, 50); 76 g.fillRect(BlockX * 2, BlockY, 50, 50); 77 g.fillRect(BlockX * 3, BlockY, 50, 50); 78 g.fillRect(BlockX * 4, BlockY, 50, 50); 79 g.fillRect(BlockX * 5, BlockY, 50, 50); 80 g.fillRect(BlockX * 6, BlockY, 50, 50); 81 g.fillRect(BlockX * 7, BlockY, 50, 50); 82 g.fillRect(BlockX * 8, BlockY, 50, 50); 83 g.fillRect(BlockX * 9, BlockY, 50, 50); 84 g.fillRect(BlockX * 10, BlockY, 50, 50); 85 g.fillRect(BlockX * 11, BlockY, 50, 50); 86 g.fillRect(BlockX * 12, BlockY, 50, 50); 87 g.fillRect(BlockX * 13, BlockY, 50, 50); 88 g.fillRect(BlockX * 14, BlockY, 50, 50); 89 g.fillRect(BlockX * 15, BlockY, 50, 50); 90 g.fillRect(BlockX * 16, BlockY, 50, 50); 91 g.fillRect(BlockX * 17, BlockY, 50, 50); 92 g.fillRect(BlockX * 18, BlockY, 50, 50); 93 g.fillRect(BlockX * 19, BlockY - 50, 50, 50); 94 95 } 96 97 public void actionPerformed(ActionEvent e){ 98 99 if(PlayerY >= 0 && PlayerY <= 350 && flyFlag == true){ 100 101 PlayerY += 9; 102 repaint(); 103 104 flyFlag = true; 105 106 }else if(PlayerY >= 350){ 107 108 flyFlag = false; 109 110 } 111 112 if(UFlag == true && flyFlag == false){ 113 114 PlayerY -= 50; 115 repaint(); 116 117 } 118 119 if(DFlag == true){ 120 121 System.out.println(DFlag); 122 123 } 124 125 if(RFlag == true && PlayerX <= 850){ 126 127 PlayerX += 10; 128 repaint(); 129 130 } 131 132 if(LFlag == true && PlayerX >= 50){ 133 134 PlayerX -= 10; 135 repaint(); 136 137 } 138 139 } 140 141 protected void processKeyEvent(java.awt.event.KeyEvent e){//キーイベントの関数 142 143 if(e.getID() == java.awt.event.KeyEvent.KEY_PRESSED){ 144 145 if(e.getKeyCode() == java.awt.event.KeyEvent.VK_UP){ 146 147 UFlag = true; 148 149 }else{ 150 151 UFlag = false; 152 153 } 154 155 if(e.getKeyCode() == java.awt.event.KeyEvent.VK_DOWN){ 156 157 DFlag = true; 158 159 }else{ 160 161 DFlag = false; 162 163 } 164 165 if(e.getKeyCode() == java.awt.event.KeyEvent.VK_RIGHT){ 166 167 RFlag = true; 168 169 }else{ 170 171 RFlag = false; 172 173 } 174 175 if(e.getKeyCode() == java.awt.event.KeyEvent.VK_LEFT){ 176 177 LFlag = true; 178 179 }else{ 180 181 LFlag = false; 182 183 } 184 185 } 186 187 } 188 189 public void mouseMoved(MouseEvent e){ 190 191 Point point = e.getPoint(); 192 PX = point.x; 193 PY = point.y; 194 195 } 196 197 public void mouseDragged(MouseEvent e){ 198 199 } 200 201 public void mouseClicked(MouseEvent e){ 202 203 } 204 205 public void mouseEntered(MouseEvent e){ 206 207 } 208 209 public void mouseExited(MouseEvent e){ 210 211 } 212 213 public void mousePressed(MouseEvent e){ 214 215 } 216 public void mouseReleased(MouseEvent e){ 217 218 } 219 220}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。