前提・実現したいこと
現在RPGの矢印キーによるキャラクターの移動を作っています。今まではキャラクターが直接移動する方法2
にしていたのですがキャラの移動をこの方法ではなく背景を動かしてキャラクターが動いているように見せたいです。
そしたら、キャラクターを直接動かす方法2
はできたのですが背景を動かしてキャラクターが動いているようにする方法1
がエラーが起きてしまってできません。
エラーの内容と直し方を教えてください。
キーを押した時のエラーコード
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at story.keyReleased(story.java:79) at java.desktop/java.awt.Component.processKeyEvent(Component.java:6597) at java.desktop/java.awt.Component.processEvent(Component.java:6413) at java.desktop/java.awt.Container.processEvent(Container.java:2263) at java.desktop/java.awt.Window.processEvent(Window.java:2041) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2764) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844) at java.desktop/java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1950) at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:870) at java.desktop/java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1139) at java.desktop/java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:1009) at java.desktop/java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:835) at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:4893) at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321) at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2764) at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844) at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721) at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745) at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743) at java.base/java.security.AccessController.doPrivileged(AccessController.java:391) at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85) at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742) at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203) at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124) at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109) at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
該当のソースコード
1
1import javax.swing.*; 2import javax.swing.border.*; 3import java.awt.*; 4import java.awt.event.*; 5public class story extends JFrame implements KeyListener{ 6 JPanel board; 7 int x,y; 8 public static void main(String[] args){ 9 story f = new story("緋色","勇者"); 10 while(true){ 11 try{System.in.read();}catch(Exception e){} 12 System.out.println(f.getSize()); 13 } 14 } 15 story(String name,String work){ 16 setTitle("ゲーム画面"); 17 //setSize(584,482);//64*9+8,64*7+34 18 setSize(590,485);//64*9+14,64*7+37 19 setResizable(true); 20 setLocationRelativeTo(null); 21 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 22 setLayout(null); 23 24 ImageIcon glass = new ImageIcon(new ImageIcon("見せられないよ/new_glass.png").getImage().getScaledInstance(64,64,Image.SCALE_DEFAULT)); 25 ImageIcon chara_icon = new ImageIcon("見せられないよ/2.png"); 26 27 JPanel board = new JPanel(); 28 board.setLayout(new GridLayout(7,9)); 29 x=0; 30 y=0; 31 board.setBounds(x,y,576,448); 32 33 JPanel p_chara = new JPanel(); 34 p_chara.setOpaque(false); 35 JLabel l_chara = new JLabel(); 36 37 p_chara.setBounds(272,200,32,48); 38 39 l_chara.setIcon(chara_icon); 40 41 p_chara.add(l_chara); 42 add(p_chara); 43 44 int p=0; 45 JPanel[] p_box = new JPanel[63]; 46 JLabel[] l_box = new JLabel[63]; 47 for(int i=0;i<63;i++){ 48 p_box[i] = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));//FlowLayoutはデフォルトで、コンポーネント間に5px?隙間を開けるから 49 l_box[i] = new JLabel(glass); 50 p_box[i].add(l_box[i]); 51 p_box[i].setBorder(new LineBorder(Color.BLACK)); 52 board.add(p_box[i]); 53 } 54 add(board); 55 56 addKeyListener(this); 57 setVisible(true); 58 } 59 @Override 60 public void keyTyped(KeyEvent e){/*何かのキー(aなど)を押した場合の処理*/} 61 @Override 62 public void keyPressed(KeyEvent e){/*キーを入力した際の処理*/} 63 @Override 64 public void keyReleased(KeyEvent e){ 65 switch(e.getKeyCode()){ 66 case KeyEvent.VK_UP://上キー board上移動 67 y-=64; 68 break; 69 case KeyEvent.VK_DOWN://下キー board下移動 70 y+=64; 71 break; 72 case KeyEvent.VK_LEFT://左キー board左移動 73 x-=64; 74 break; 75 case KeyEvent.VK_RIGHT://右キー board右移動 76 x+=64; 77 break; 78 } 79 board.setBounds(x,y,576,448); 80 } 81}
2
1import javax.swing.*; 2import javax.swing.border.*; 3import java.awt.*; 4import java.awt.event.*; 5public class story extends JFrame implements KeyListener{ 6 JPanel p_chara; 7 JLabel l_chara; 8 int x,y; 9 public static void main(String[] args){ 10 story f = new story("緋色","勇者"); 11 while(true){ 12 try{System.in.read();}catch(Exception e){} 13 System.out.println(f.getSize()); 14 } 15 } 16 story(String name,String work){ 17 setTitle("ゲーム画面"); 18 //setSize(584,482);//64*9+8,64*7+34 19 setSize(590,485);//64*9+14,64*7+37 20 setResizable(false); 21 setLocationRelativeTo(null); 22 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 23 setLayout(null); 24 25 ImageIcon glass = new ImageIcon(new ImageIcon("見せられにないよ/new_glass.png").getImage().getScaledInstance(64,64,Image.SCALE_DEFAULT)); 26 ImageIcon chara_icon = new ImageIcon("見せられないよ2.png"); 27 28 p_chara = new JPanel(); 29 p_chara.setOpaque(false); 30 l_chara = new JLabel(); 31 32 x=64*4+16; 33 y=64*3+8; 34 p_chara.setBounds(x,y,32,48); 35 /* 36 x=64*4; 37 y=64*3; 38 p_chara.setBounds(x,y,64,96); 39 */ 40 l_chara.setIcon(chara_icon); 41 42 p_chara.add(l_chara); 43 add(p_chara); 44 45 int p=0; 46 JPanel[] p_box = new JPanel[63]; 47 JLabel[] l_box = new JLabel[63]; 48 for(int i=0;i<63;i++){ 49 p_box[i] = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));//FlowLayoutはデフォルトで、コンポーネント間に5px?隙間を開けるから 50 l_box[i] = new JLabel(glass); 51 p_box[i].add(l_box[i]); 52 } 53 for(int bi=0;bi<448;bi+=64){ 54 for(int i=0;i<576;i+=64){ 55 p_box[p].setBounds(i,bi,64,64); 56 add(p_box[p]); 57 p++; 58 } 59 } 60 61 addKeyListener(this); 62 setVisible(true); 63 } 64 @Override 65 public void keyTyped(KeyEvent e){/*何かのキー(aなど)を押した場合の処理*/} 66 @Override 67 public void keyPressed(KeyEvent e){/*キーを入力した際の処理*/} 68 @Override 69 public void keyReleased(KeyEvent e){ 70 switch(e.getKeyCode()){ 71 case KeyEvent.VK_UP://上キー 上移動 72 if(y==8){ 73 break; 74 } 75 y-=64; 76 break; 77 case KeyEvent.VK_DOWN://下キー 下移動 78 if(y==392){ 79 break; 80 } 81 y+=64; 82 break; 83 case KeyEvent.VK_LEFT://左キー 左移動 84 if(x==16){ 85 break; 86 } 87 x-=64; 88 break; 89 case KeyEvent.VK_RIGHT://右キー 右移動 90 if(x==528){ 91 break; 92 } 93 x+=64; 94 break; 95 } 96 p_chara.setBounds(x,y,32,48); 97 } 98}
補足情報(FW/ツールのバージョンなど)
java jdk-14.0.2
java version "14.0.2" 2020-07-14
Java(TM) SE Runtime Environment (build 14.0.2+12-46)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/12/27 04:23