每次触摸屏幕时都会出现精灵,然后拍摄到所需的区域。我怎么能让它一次只有一个精灵在现场,直到它退出现场或击中一个物体? (即使多次触摸屏幕)
这是射弹代码
- (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"];
}
答案 0 :(得分:1)
如果我理解你的观点,你可以简单地添加一个标志,以便能够注意到场景中已有精灵。只需在你的班级上宣布一个
BOOL isSpritePresent;
在您的类自定义ID方法上初始化它。
-(id)init {
self=[super init];
isSpritePresent=NO;
return self; }
然后在TouchBegan的开头添加类似
的内容if(isSpritePresent){
return; //As there's already an sprite on the scene.
}
最后
isSpritePresent=YES;
最后当箭头或w / e到达目标时,调用方法重置布尔值。
或者......如果你期待事情变得更简单,并且你相信你有一个特定的时间让用户再次拍摄,那么只需在其他行动之后加上延迟......
CCActionDelay *delay = [CCActionDelay actionWithDuration:1.2f];
[projectile runAction:[CCActionSequence actionWithArray:@[actionMove,actionRemove,delay]]];