cocos2d-android を使用したゲーム アプリのコーディングを行いましたが、プロジェクトを実行すると、ゲーム レイヤーの画面の Ontouch で強制的に閉じられます。およびlogcatでnullポインター例外が発生しますが、すべてのオブジェクトと変数が正しく宣言されています。
このメソッドでは、Java ファイルの public boolean ccTouchesEnded(MotionEvent event)_nextProjectile;
が正しく宣言されていますが、それでもヌル ポインター例外が多すぎます。public class GameL extends 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)