Saturday 12 January 2013

Tank Rampages Tank 3 -Fire!


The tank will fire when a player touches the right side of the screen and ends his touch: 


- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (!gameEnded) {
        
    
    UITouch *touch= [touches anyObject];
    
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: location];  
    
    
    if (location.x>screenSize.width/2) {
               
            [tank fire];
         
                
    }
    
    else {
        [tank stopDriving];
    }
       }}



The fire method is once again a setter for the BOOL fire (a BOOL is a variable with either a YES or a NO value):
-(void)fire{
    if (reloadTime<=0){
    fire=YES;
    }
}






First, it checked whether the tank is still reloading. If not, fire is set to YES.
In the update method, the following statement is checked every frame:

if (fire&&(fabsf( normalizedAngle)<0 data-blogger-escaped-.01=".01" data-blogger-escaped-barreljoint-="barreljoint-">GetJointAngle()>=barrelJoint->GetUpperLimit()&&normalizedAngle>0)||(barrelJoint->GetJointAngle()<=barrelJoint->GetLowerLimit()&&normalizedAngle<0 data-blogger-escaped--barrel.rotation="-barrel.rotation" data-blogger-escaped-barrel.rotation="barrel.rotation" data-blogger-escaped-barrel="barrel" data-blogger-escaped-body="body" data-blogger-escaped-ccp="ccp" data-blogger-escaped-ccpforangle="ccpforangle" data-blogger-escaped-cgpoint="cgpoint" data-blogger-escaped-cosf="cosf" data-blogger-escaped-createbatchspritewithuniquename:="createbatchspritewithuniquename:" data-blogger-escaped-float="float" data-blogger-escaped-fromshscene:="fromshscene:" data-blogger-escaped-irebarrel="irebarrel" data-blogger-escaped-lhsprite="lhsprite" data-blogger-escaped-originalpoint="ccpAdd(barrel.position," data-blogger-escaped-playanimation="playanimation" data-blogger-escaped-prepareanimationnamed:="prepareanimationnamed:" data-blogger-escaped-prites="prites" data-blogger-escaped-shell.scale="shell.scale" data-blogger-escaped-shell="shell" data-blogger-escaped-sinf="sinf" data-blogger-escaped-tankvec="ccpMult(" data-blogger-escaped-transformposition:originalpoint="transformposition:originalpoint" data-blogger-escaped-x="x" data-blogger-escaped-y="y">ApplyLinearImpulse(b2Vec2(tankVec.x,tankVec.y ),[shell body]->GetWorldCenter());
    
   
    [shell setColor:ccc3(120,120,120)];
    
    [shell transformRotation:barrel.rotation];
    
    [shell.userInfo setDamage:damage];
    
        fire=NO;
        reloadTime=reloadDelay;
    }
    
    reloadTime=reloadTime-dt;










Let’s explain that a little. First, it’s checked whether:

·      The player wants to fire and:
o   The barrel is less than 0.01 radians away from its aiming position (so the barrel isn’t moving whilst firing, or;
o   The barrel has reached its upper or lower aiming limit.

When this is the case, an animation is played by the barrel and a shell is created. The shell is then positioned at the right position, calculated using some goniometric math. Next, the vector of the direction of the barrel is calculated and multiplied by 0.8 times the scale of the shell, in order to cancel out the weight of the shell for different scales (if the shell scales up, it will still be fired at the same speed). Finally an impulse is applied to the shell and its color, rotation and damage property are set. Fire is changed back to no and the reloadTime is reset. The last line of code subtracts the time passed this frame (dt) from the reloadTime.

No comments:

Post a Comment