0

을 사용하여 참조로 스플라이트를 전달하면 널 포인터 예외가 발생합니다. 매개 변수에 스프라이트를 전달하고 메소드에서 해당 객체를 초기화 할 때 Object의 참조가 여전히 메소드에서 전달한 null입니다. 여기 내 방법이 있습니다.andengine GLES2

public void createAndLoadSimpleSprite(Sprite sprite ,String name, 
     SimpleBaseGameActivity activity, int width, int height) { 

    BitmapTextureAtlas atlasForBGSprite = new BitmapTextureAtlas(
      activity.getTextureManager(), width, height); 
    TextureRegion backgroundSpriteTextureRegion = BitmapTextureAtlasTextureRegionFactory 
      .createFromAsset(atlasForBGSprite, activity, name, 0, 0); 
    sprite = new Sprite(0, 0, backgroundSpriteTextureRegion, 
      activity.getVertexBufferObjectManager()); 
    activity.getTextureManager().loadTexture(atlasForBGSprite); 

} 

여기 내가 어떻게 부르고 있는지.

createAndLoadSimpleSprite(defualtCageSprite,"bg.png", this, 450, 444); 

그러나 defaultCageSprite에 액세스하면 여전히 null 포인터 예외가 발생합니다 ... AndEngine 문제인지 확실하지 않습니다. 하지만 주위에 어떤 것이 없으면 AndEngine에서 매개 변수로 스프라이트를 전달할 수 없습니까 ??

답변

1

당신은 스프라이트 개체를 반환 귀하의 방법을 변경해야

defualtCageSprite = createAndLoadSimpleSprite("bg.png", this, 450, 444); 

scene에 스프라이트를 첨부하는 것을 잊지 마십시오.

+0

예이 메서드는 정확히 같은 방식으로 변경되었습니다. 그러나 모든 개체에 고유 한 스프라이트 참조가 있도록 클래스의 생성자에서 스프라이트를 전달해야합니다. 그래서 Sprite를 Reference로 전달하려고합니다. – Waqas

+0

나는 무엇을 성취하려고하는지 이해하지 못한다. 그러나 이것은 자바 문제이다. 질문에'java' 태그를 추가해 보라. –

+0

당신 말이 맞아요. 내 잘못이야 :). 다시 변수의 범위를 무시합니다 :). 감사 – Waqas

3

Thst는 AndEngine 문제가 아닙니다. 당신은 Java가 작동하는 방식을 다루고 있습니다. AFAIK 출력 매개 변수를 가질 수 없습니다. 당신이해야 할 일은 "return"을 사용하거나 필드를 만들어 데이터를 저장하는 것입니다.

public Sprite createAndLoadSimpleSprite(String name, 
    SimpleBaseGameActivity activity, int width, int height) { 

BitmapTextureAtlas atlasForBGSprite = new BitmapTextureAtlas(
     activity.getTextureManager(), width, height); 
TextureRegion backgroundSpriteTextureRegion = BitmapTextureAtlasTextureRegionFactory 
     .createFromAsset(atlasForBGSprite, activity, name, 0, 0); 
activity.getTextureManager().loadTexture(atlasForBGSprite); 

Sprite sprite = new Sprite(0, 0, backgroundSpriteTextureRegion, 
     activity.getVertexBufferObjectManager()); 

return sprite; 

} 

지금 당신이 호출 할 수 있습니다 :

+0

당신 말이 맞습니다. thanks – Waqas