iphone - Cocos2d。降低射击(子弹)的速度(速率)?

标签 iphone ios cocos2d-iphone joypad

我有一个带有方向键和 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/

相关文章:

iphone - Sqlite打开错误“无法打开数据库”

iOS UITabBar : Remove top shadow gradient line iOS 10

xcode - Cocos2D Sprite 受限于屏幕

uiview - Cocos2d 2.0 添加 UIView 到 CClayer openglview 已弃用

objective-c - XCode 警告 - 尝试更改项目名称时复制 CMakeLists.txt

iphone - 如何同步两个 NSManagedObjectContext

iphone - 编辑 TableView 时自定义 TableCell 中没有缩进

web-applications - 如何从 Javascript 中清除 HTML5 应用程序缓存?

ios - 解密 AES-256-CBC 字符串(需要 IV、字符串/数据格式?)

ios - NSOperationQueues 中的信号量