在整个方法中访问数组

时间:2014-04-30 18:02:52

标签: java arrays

我正在用Java写一个视频游戏(一个可怕的想法,是的,我知道)。但我遇到了一个巨大的障碍。我编写了代码来访问文本文档以输出“地牢”。通过使用1s和0s。这被读入一个多维数组[imageArray],然后放置我的"墙'图片。这就像一个魅力。我现在要做的是创建一个可以在try / catch括号外访问的第二个数组。 我的问题是我尝试了几种不同的访问辅助阵列的方法,我只需要帮助。

非常感谢。

这是守则。

import javax.swing.*;  
import java.awt.*;  
import java.awt.event.*;
import java.util.Random;  
import java.io.*;
import java.util.Scanner;

public class main extends JPanel implements KeyListener, ActionListener
{ 

    Timer t = new Timer(8, this);

    int xCoord, yCoord;
    int x=1;
    int y=1;
    int d,v;
    private Image image1, image2, image3, image4;
    boolean pressed;


    Random ran = new Random();
    int cubeDimension = 40;
    int set = cubeDimension + 2;


    public main(){
    xCoord = 0;
    yCoord = 0;
    t.start();  
      addKeyListener(this);  
      setFocusable(true);
      setFocusTraversalKeysEnabled(false);

    }

    public void paintComponent(Graphics g) 
        {
            super.paintComponent(g);
            g.setColor(Color.black);
                g.fillRect(0,0, 1000,1000);         
            g.setColor(Color.white);
            for(int i = 0; i < 795; i += set){
                for(int e = 0; e < 595; e += set){
                    g.fillRect(i, e, cubeDimension, cubeDimension);
                }
            } 

              //random code  (ran.nextInt(9)+1)
            //ImageIcon m1 = new ImageIcon("monster1.jpg");
            //image2=m1.getImage(); 
            ImageIcon w = new ImageIcon("wall.jpg");
            image3 = w.getImage();
            ImageIcon j = new ImageIcon("Hero.jpg");
            image1 = j.getImage();

            try{
            Scanner file = new Scanner(new File("room.dat"));
            int cnt = file.nextInt();
            int cnt2 =file.nextInt();   
            int[][] imageArray = new int[cnt][cnt2];

            for(int i=0;i<cnt;i++)
            {
                yCoord=i;
                for(int c=0;c<cnt2;c++)
                {
                    xCoord=c;
                    int n = file.nextInt();
                    imageArray[i][c] = n;
                    mapArray[xCoord][yCoord]=n;
                    if (n==1)
                    {
                        g.drawImage(image3,c*set,i*set,null);
                    }
                    if (n==2)
                    {
                        g.drawImage(image1,c*set*x+5,i*set*y,null);
                    }   

                }


            }       
            }
            catch(FileNotFoundException e){
                System.out.println("Error");
            } 


        }


        //border code  
        public void keyReleased(KeyEvent e)
        {

        int code = e.getKeyCode();
            if(code==KeyEvent.VK_NUMPAD8)
                if(y>=0)
                    up();
            if(code==KeyEvent.VK_NUMPAD7)
                if(y>=0&&x>=0)
                    upleft();
            if(code==KeyEvent.VK_NUMPAD2)
                if(y<=14)   
                    down();
            if(code==KeyEvent.VK_NUMPAD1)
                    if(y<=14&&x>0)
                        downleft();
            if(code==KeyEvent.VK_NUMPAD6)
                if(x<=19)
                    right();
            if(code==KeyEvent.VK_NUMPAD3)
                if(x<=19&&y<=14)
                    downright();
            if(code==KeyEvent.VK_NUMPAD4)
                if(x>=0)
                    left();
            if(code==KeyEvent.VK_NUMPAD9)
                if(x<=19&&y>0)
                    upright();  
        }

        public void actionPerformed(ActionEvent e){
            repaint();
        }


        public void up(){ y= y-1 ;}
        public void upleft(){y= y-1;x= x-1;}
        public void down(){ y= y+1;}
        public void downleft(){ y= y+1; x= x-1;}
        public void left(){ x = x-1;}
        public void upright(){x = x+1;y= y-1;}
        public void right(){ x = x+1;}
        public void downright(){y= y+1;x = x+1;}


        public void keyTyped(KeyEvent e){}
        public void keyPressed(KeyEvent e){}
}

1 个答案:

答案 0 :(得分:1)

  

我的问题是访问try / catch括号外的辅助数组。

imageArray阻止之前移动try

int[][] imageArray = null;

try{
        Scanner file = new Scanner(new File("room.dat"));
        int cnt = file.nextInt();
        int cnt2 =file.nextInt();   
        imageArray = new int[cnt][cnt2];
        ...

}catch(FileNotFoundException e){
    System.out.println("Error");
}