0
每次觸摸屏幕時都會出現精靈,然後它會拍攝到所需的區域。我如何讓它一次只有一個精靈在場景中,直到它退出場景或擊中一個對象? (即使屏幕被觸摸多次)同時在場景中只有一個精靈Cocos2D 3.x
這是彈丸代碼
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
// 1
CGPoint touchLocation = [touch locationInNode:self];
// 2
CGPoint offset = ccpSub(touchLocation, _player.position);
float ratio = offset.y/offset.x;
int targetX = _player.contentSize.width/2 + self.contentSize.width;
int targetY = (targetX*ratio) + _player.position.y;
CGPoint targetPosition = ccp(targetX,targetY);
// 3
CCSprite *projectile = [CCSprite spriteWithImageNamed:@"projectile.png"];
projectile.position = _player.position;
projectile.physicsBody = [CCPhysicsBody bodyWithCircleOfRadius:projectile.contentSize.width/2.0f andCenter:projectile.anchorPointInPoints];
projectile.physicsBody.collisionGroup = @"playerGroup";
projectile.physicsBody.collisionType = @"projectileCollision";
[_physicsWorld addChild:projectile];
// 4
CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:1.5f position:targetPosition];
CCActionRemove *actionRemove = [CCActionRemove action];
[projectile runAction:[CCActionSequence actionWithArray:@[actionMove,actionRemove]]];
[[OALSimpleAudio sharedInstance] playEffect:@"pew-pew-lei.caf"];
}
如何我會把它初始化成類嗎? 我是新來的,對不起,如果它是一個愚蠢的問題。謝謝:) – Crazycriss
好吧堅持要去更新帖子。 –
如果你正在創建自己的類,你可以從我在這裏更新的一小段代碼開始,如果你使用另一個類作爲你的父節點,可能取決於他們需要的init,那麼你需要重寫它。或者,如果該類已經有init方法,那麼只需在那裏定義isSpritePresent = NO的值。 –