2011-10-18 3 views
1

학교에서 프로젝트 작업 중입니다. 저는 프로그래밍 초보자이며 버블 슈터 제작에 큰 문제가 있습니다. 이전에지도의 모든 공을 가져와야합니다. map2로 변경.Java Greenfoot, 파일간에 메소드를 연결할 수 없습니다.

모든 볼을 나열하려고했지만 프로그램이 첫 번째지도의 끝에 충돌하고 음수 값을로드 할 수 없다는 오류보고가 표시됩니다. 나는 그것이 총을 적재하려고 할 때이고, "allowedBallTypes! = null"또는 0 일 수도 있다고 말하면서 총을 적재해야하는 것보다 if-statement를 넣고 싶다고 생각했습니다.

기호를 찾을 수 없습니다. - getAllowedBallTypes(); greenfoot 자바 방법 클래스

bubbleworld 클래스 :

public BubbleWorld() 
{  
     super(Map.MAX_WIDTH*Map.COLUMN_WIDTH, Map.MAX_HEIGHT*Map.ROW_HEIGHT, 1,false); 

     // Max speed. We use time-based animation so this is purely for smoothness, 
     // because Greenfoot is plain stupid. I can't find a way to get 60 Hz so this is 
     // what we have to do. Note: Exporting the game seems to cap this to some value < 100. :(
     Greenfoot.setSpeed(100); 

     // Load the map. 
     map = new Map(this, testMap1); 

     // Update the allowed ball types. (i.e. we don't want to spawn a 
     // certain color of balls if the map doesn't contain them!) 
     map.updateAllowedBallTypes(); 

     // Create the cannon. 
     Cannon cannon = new Cannon(); 
     addObject(cannon, getWidth()/2, getHeight()); 

지도 클래스 :

 public int[] getAllowedBallTypes() 
    { 
     return allowedBallTypes; 
    } 


public void updateAllowedBallTypes() 
    { 
     int allowedCount = 0; 
     boolean[] allowed = new boolean[Ball.typeCount]; 

     // Only ball types that exist in the map RIGHT NOW as attached balls will be allowed. 
     for(Cell c : cells) 
     { 
      if(c != null && c.getBall() != null && c.isAttached()) 
      { 
       int type = c.getBall().getType(); 

       if(!allowed[type]) 
        allowedCount++; 

       allowed[type] = true; 
      } 
     } 

     allowedBallTypes = new int[allowedCount]; 
     int writeIndex = 0; 
     for(int type = 0; type < Ball.typeCount; ++type) 
     { 
      if(allowed[type]) 
      { 
       allowedBallTypes[writeIndex++] = type; 
      } 
     } 
    } 

대포 클래스 : 코드를 기반으로

private void prepareBall() 
    { 
     // Get a random ball type from the list of allowed ones. Only balls currently in the map 
     // will be in the list. 
     int[] allowedBallTypes = ((BubbleWorld)getWorld()).getMap().getAllowedBallTypes(); 
     int type = allowedBallTypes[Greenfoot.getRandomNumber(allowedBallTypes.length)]; 

     // Create it and add it to the world. 
     ball = new Ball(type); 
     getWorld().addObject(ball, getX(), getY()); 
    } 

답변

0

,지도 클래스 클래스 수준의 변수 선언이 int []에 없습니다. allowedBall 유형;

+0

심볼을 찾을 수 없습니다. - 메서드 getAllowedBallTypes() –

+0

저는이 프로젝트를 도와주었습니다.하지만 전에 JAVA 나이 프로그램을 건드리지 않았으므로 여기에서 물어볼 것이라고 제안했습니다. –

+0

저는 초급자이고 클래스 레벨 변수 선언으로 무엇을 의미하는지 확신 할 수 없으므로 저에게 설명해주십시오 :) –

1

캐논 (Cannon) 클래스의 붙여 넣은 스 니펫에서 오류가 발생한다고 가정하면 오류는 BubbleWorld의 getMap() 메서드에 문제가 있음을 나타냅니다.이를 볼 수 있도록 붙여 넣을 수 있습니까? 올바른 유형을 반환하지 않을 수도 있습니다. 일반적으로 완전한 클래스를 포함하여 더 많은 코드를 붙여 넣고 오류가 발생한 정확한 위치를 말해야합니다. 더 쉬운 방법은 소스 코드를 사용하여 시나리오를 greenfoot 웹 사이트 (www.greenfoot.org - Greenfoot의 공유 기능을 사용하고 소스 코드 상자를 확인하십시오)에 업로드하고 그 링크를 제공하는 것입니다.