gpt4 book ai didi

ios - 场景中一次只有一个 Sprite Cocos2D 3.x

转载 作者:行者123 更新时间:2023-11-29 03:16:17 27 4
gpt4 key购买 nike

每次触摸屏幕时 Sprite 都会出现,然后它会射向所需区域。我如何才能让它一次只有一个 Sprite 出现在场景中,直到它退出场景或撞到一个物体? (即使多次触摸屏幕)

这是弹丸代码

- (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"];

}

最佳答案

如果我正确理解你的观点,你可以简单地添加一个标志,以便能够注意到场景中已经有 Sprite 。只需在您的类(class)上声明

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 到达目标时,调用一个方法来重置 bool 值。

或者...如果您希望做的事情更简单,并且您相信您有特定的时间让用户再次射击,只需在其他操作之后添加一个延迟,如..

CCActionDelay *delay = [CCActionDelay actionWithDuration:1.2f];
[projectile runAction:[CCActionSequence actionWithArray:@[actionMove,actionRemove,delay]]];

关于ios - 场景中一次只有一个 Sprite Cocos2D 3.x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21666494/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com