0

对于使用cocos2d-android的游戏应用程序,我给出了编码,但是当我运行该项目时,游戏层。 ,并在logcat获得空指针异常,但所有的对象和变量被正确声明。
在这个方法中公共布尔值ccTouchesEnded(MotionEvent event)在java文件_nextProjectile;中声明正确,但仍然太空指针异常。 公共类GameL延伸CCLayer {cocos2d-android中的空指针异常错误即使所有的方法都被声明并正确初始化了

protected LinkedList<CCSprite> _targets; 
protected LinkedList<CCSprite> _projectiles; 
protected int _projectilesDestroyed; 
protected CCSprite _player; 
protected CCSprite _nextProjectile; 

public static CCScene scene() 
{ 
    CCScene scene = CCScene.node(); 
    CCLayer layer = new GameL(); 

    scene.addChild(layer); 

    return scene;  
} 
CCMenuItem item1,item2,item3; 

protected GameL() 
{ 
this.setIsTouchEnabled(true); 

    _targets = new LinkedList<CCSprite>(); 
    _projectiles = new LinkedList<CCSprite>(); 
    _projectilesDestroyed = 0; 

    CCSprite background = CCSprite.sprite("bg.png"); 
    background.setTag(1); 
    background.setAnchorPoint(0, 0); 
    addChild(background); 



    CCSprite player2 = CCSprite.sprite("gun2.png"); 
    player2.setPosition(CGPoint.ccp(65,120)); 
    player2.setAnchorPoint(CGPoint.ccp(0,0)); 
    addChild(player2); 

    this.schedule("gameLogic", 1.0f); 
    this.schedule("update"); 
    } 



@Override 
public boolean ccTouchesEnded(MotionEvent event) 
{ 
    // Choose one of the touches to work with 
    CGPoint location = CCDirector.sharedDirector().convertToGL(CGPoint.ccp(event.getX(), event.getY())); 

    // Set up initial location of projectile 
    CGSize winSize = CCDirector.sharedDirector().displaySize(); 
    _nextProjectile = CCSprite.sprite("Projectile.png"); 

    _nextProjectile.setPosition(20, winSize.height/2.0f); 

    // Determine offset of location to projectile 
    int offX = (int)(location.x - _nextProjectile.getPosition().x); 
    int offY = (int)(location.y - _nextProjectile.getPosition().y); 

    // Bail out if we are shooting down or backwards 
    if (offX <= 0) 
     return true; 

    _nextProjectile.setTag(2); 

    // Determine where we wish to shoot the projectile to 
    int realX = (int)(winSize.width + (_nextProjectile.getContentSize().width/2.0f)); 
    float ratio = (float)offY/(float)offX; 
    int realY = (int)((realX * ratio) + _nextProjectile.getPosition().y); 
    CGPoint realDest = CGPoint.ccp(realX, realY); 

    // Determine the length of how far we're shooting 
    int offRealX = (int)(realX - _nextProjectile.getPosition().x); 
    int offRealY = (int)(realY - _nextProjectile.getPosition().y); 
    float length = FloatMath.sqrt((offRealX * offRealX) + (offRealY * offRealY)); 
    float velocity = 480.0f/1.0f; // 480 pixels/1 sec 
    float realMoveDuration = length/velocity; 

    // Move projectile to actual endpoint 
    _nextProjectile.runAction(CCSequence.actions(
      CCMoveTo.action(realMoveDuration, realDest), 
      CCCallFuncN.action(this, "spriteMoveFinished"))); 

    // Determine angle to face 
    double angleRadians = Math.atan((double)offRealY/(double)offRealX); 
    double angleDegrees = Math.toDegrees(angleRadians); 
    double cocosAngle = -1 * angleDegrees; 
    double rotationSpeed = 0.5/Math.PI; 
    double rotationDuration = Math.abs(angleRadians * rotationSpeed); 
    _player.runAction(CCSequence.actions(
      CCRotateTo.action((float)rotationDuration, (float)cocosAngle), 
      CCCallFunc.action(this, "finishShoot"))); 

    // Pew! 
    Context context = CCDirector.sharedDirector().getActivity(); 
    SoundEngine.sharedEngine().playEffect(context, R.raw.pew_pew_lei); 

    return true; 
} 

public void finishShoot() 
{ 
    addChild(_nextProjectile); 
    _projectiles.add(_nextProjectile); 
} 

public void gameLogic(float dt) 
{ 
    addTarget(); 
} 

logtrace。

05-01 09:43:50.591: E/AndroidRuntime(794): FATAL EXCEPTION: GLThread 75 
05-01 09:43:50.591: E/AndroidRuntime(794): java.lang.NullPointerException 
05-01 09:43:50.591: E/AndroidRuntime(794): at com.trialcocos.GameL.ccTouchesEnded(GameL.java:139) 
05-01 09:43:50.591: E/AndroidRuntime(794): at org.cocos2d.events.CCTouchHandler.ccTouchesEnded(CCTouchHandler.java:75) 
05-01 09:43:50.591: E/AndroidRuntime(794): at  org.cocos2d.events.CCTouchDispatcher.touchesEnded(CCTouchDispatcher.java:395) 
05-01 09:43:50.591: E/AndroidRuntime(794):  at org.cocos2d.events.CCTouchDispatcher.update(CCTouchDispatcher.java:355) 
05-01 09:43:50.591: E/AndroidRuntime(794):  at org.cocos2d.nodes.CCDirector.onDrawFrame(CCDirector.java:646) 
05-01 09:43:50.591: E/AndroidRuntime(794):  at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1516) 
05-01 09:43:50.591: E/AndroidRuntime(794):  at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240) 
05-01 09:43:52.881: E/libEGL(794): call to OpenGL ES API with no current context (logged once per thread) 

回答

0

您正确声明精灵,但你是不是在ccTouchesEnded跑步动作method.Add您的抛射运行任何行动that.This前层之前将你的精灵到你的层是异常

的原因