每次触摸屏幕都会出现精灵,然后它会锁定在指定区域。要如何操作,我才能保证,即使多次触摸屏幕,每次在退出场景或碰到物体之前,场景中只出现一个精灵?
我现在采用的代码如下:
- (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"];
}
答:Joel Hernandez
以我的理解,你可以简单的添加一个flag,这样可以让精灵在场景中更容易辨识。只要在类别上添加:
然后,在自定义ID的方法中将其初始化
然后在TouchBegan的启动项中,添加:
在末尾项中添加:
最终,当箭头到达目标位置时,可以调用一种适应的方法重置Boolean。
如果你想要更加简便的实现预期效果,可以在其它动作效果之后添加如下的延迟方法: