对于使用cocos2d-android的游戏应用程序,我已经给出了编码,但是当我运行项目时,它会在游戏层的屏幕上强制关闭。
并在logcat中获取空指针异常,但所有对象和变量都被正确声明
在这个方法中,java文件_nextProjectile;
中的公共布尔值ccTouchesEnded(MotionEvent事件)被正确声明但仍然是空指针异常。
公共类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();
}
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 :(得分:0)
你正在正确地声明精灵,但你没有在你的图层中添加你的精灵,然后在ccTouchesEnded方法中运行动作。在运行任何动作之前将你的射弹添加到图层。这是异常的原因