チュートリアルを使用して、1つの簡単なアプリを作成しています。また、サンプルアプリとは異なり、発射物/オブジェクトをタッチではなく最初から表示したいと思います。同じように、私はこのようにやっています:
-(id) init
{
if((self=[super init]))
{
[[SimpleAudioEngine sharedEngine]setEnabled:TRUE];
winSize = [CCDirector sharedDirector].winSize;
projectile = [CCSprite spriteWithFile:@"abc.png"];
projectile.position = spriteOffset;
self addChild:projectile];
self.isTouchEnabled = YES;
//some other code
}
}
次に、同じオブジェクト/発射体に触れると、移動した方向に移動する必要があります。私は同じために以下をやっています:
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// Choose one of the touches to work with
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
// Determine offset of location to projectile
int offX = location.x - projectile.position.x; //Projectile returns 0X0 here
int offY = location.y - projectile.position.y;
float scalarX = 1.0f;
if (offX < 0.0f)
scalarX = -1.0f;
int realX = scalarX * (winSize.width + (projectile.contentSize.width/2));//winSize.width + (food.contentSize.width/2);
float ratio = (float) offY / (float) offX;
int realY = (realX * ratio) + projectile.position.y;
CGPoint realDest = ccp(realX, realY);
int offRealX = realX - projectile.position.x;
int offRealY = realY - projectile.position.y;
float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;
[projectile runAction:[CCSequence actions:
[CCMoveTo actionWithDuration:realMoveDuration position:realDest],
[CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
nil]];
[_projectiles addObject:projectile]; //ERROR SIGABRT
}
上記のコードでは、この行でint offX = location.x - projectile.position.x;
私のスプライトは0X0に戻ります。間違いを犯しているところがわかりません。最初は画面にオブジェクトが表示されていましたが、タッチイベントで消えてしまいます。私もCCSpriteを合成しましたが、役に立ちました。他の方法や私が行っている間違いはありますか?何かアイデアがあれば助けてください。ありがとうございました。
ERROR : Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0'