我有一个带有方向键和 2 个按钮的游戏 handle 。所有这些都是数字的(不是模拟的)。
我有一个处理他们的事件的程序:
-(void)gamepadTick:(float)delta
{
...
if ([gamepad.B active]) {
[ship shoot];
}
...
}
我通过命令调用它:
[self schedule:@selector(gamepadTick:) interval:1.0 / 60];
[船舶射击]从Weapon类调用射击函数:
-(void)shoot
{
Bullet *bullet = [Bullet bulletWithTexture:bulletTexture];
Ship *ship = [Ship sharedShip];
bullet.position = [ship getWeaponPosition];
[[[CCDirector sharedDirector] runningScene] addChild:bullet];
CGSize winSize = [[CCDirector sharedDirector] winSize];
int realX = ship.position.x;
int realY = winSize.height + bullet.contentSize.height / 2;
CGPoint realDest = ccp(realX, realY);
int offRealX = realX - bullet.position.x;
int offRealY = realY - bullet.position.y;
float length = sqrtf((offRealX*offRealX) + (offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length / velocity;
[bullet runAction:[CCSequence actions:[CCMoveTo actionWithDuration:realMoveDuration position:realDest],
[CCCallFuncN actionWithTarget:self selector:@selector(bulletMoveFinished:)], nil]];
}
-(void)bulletMoveFinished:(id)sender
{
CCSprite *sprite = (CCSprite *)sender;
[[[CCDirector sharedDirector] runningScene] removeChild:sprite cleanup:YES];
}
问题是武器发射了太多子弹。是否可以减少它们的数量,而无需编写使用每个按钮和方向板单独的计时器和功能?
最佳答案
使用 Delta 来跟踪拍摄之间的时间,并且仅在 Delta 增加一定量时才进行拍摄。这是一种常见的处理方式,但您并不希望每一帧都这样做。
您需要保留一个 iVar 计数器来记录所耗时,在每次拍摄时将其增加 Delta,然后测试所耗时量,看看它是否满足您所需的间隔阈值;如果是,则射击并将耗时重置为 0。
关于iphone - Cocos2d。降低射击(子弹)的速度(速率)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9957593/