0

对于使用 cocos2d-android 的游戏应用程序,我已经给出了编码,但是当我运行项目时,它会强制关闭游戏层中屏幕的 Ontouch。并在 logcat 中获取空指针异常,但所有对象和变量都已正确声明。
在此方法中,java 文件中的 public boolean ccTouchesEnded(MotionEvent event)_nextProjectile;被正确声明,但仍然是空指针异常。公共类 GameL 扩展 CCLayer{

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();
}

日志跟踪。

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)
4

1 回答 1

0

您正在正确声明精灵,但在运行 ccTouchesEnded 方法中的操作之前没有将精灵添加到图层。在运行任何操作之前将弹丸添加到图层。这是异常的原因

于 2013-05-08T10:45:48.963 回答