Search code examples
ioscocos2d-iphonefloating-pointincrement

Increase float certain time period?


I am trying to create a somewhat easy of a method for my game however I am not quite sure how to implement it. I have a float right now that starts at .1, I would like it to increment to 1 in a certain time frame.

Lets say this time frame was .5 seconds. Now clearly this method would be called on an update loop to increase it every time however I am not sure where to begin.

I hate to post a question without any code but I just don't know the logistics of it. Would I divide the result number by the deltaTime? Any advice would be appreciated.


Solution

  • If this float is a property of a CCNode descendant, try CCActionTween : here is the excerpt from the docs (version 2.1):

     /** CCActionTween
    
     CCActionTween is an action that lets you update any property of an object.
     For example, if you want to modify the "width" property of a target from 200 to 300 in 2     
     seconds, then:
    
     id modifyWidth = [CCActionTween actionWithDuration:2 key:@"width" from:200 to:300];
     [target runAction:modifyWidth];
    
     Another example: CCScaleTo action could be rewriten using CCPropertyAction: (sic) CCActionTween
    
    // scaleA and scaleB are equivalents
     id scaleA = [CCScaleTo actionWithDuration:2 scale:3];
     id scaleB = [CCActionTween actionWithDuration:2 key:@"scale" from:1 to:3];
    
     @since v0.99.2
     */
    

    EDIT :

    Example : say you have a Cannon class, which derives from CCNode (as in the .h below)

    @interface Cannon:CCNode {
    
        float _bulletInitialVelocity;
        float _firingRate;
    }
    
    @property (nonatomic, readwrite) float bulletInitialVelocity;
    @property (nonatomic, readwrite) float firingRate;
    @end
    
    in the cannon logic, you could
    
    CCTweenAction *fr = [CCTweenAction actionWithDuration:60.0 key:@"firingRate" from:.25 to:.75];
    [self runAction:fr];
    

    this could increase the firing rate over a period of 60 seconds. You could do the same for bullet's initial velocity. Notice these properties are not CCNode properties, but some you created yourself by extending CCNode. I wrote it old style so you can see that the properties are actually 'backed' by an iVar.