Basic Tutorial – Flying Planes

Hello All !

I am glad to tell you that this post will add some basic actions & sound effects.

Panes

Panes



Just follow the steps to have output as shown in video.

Step 1) Create a new project using Cocos2D Project Template.

Step 2) Open HelloWorldLayer.m file & replace with following code.

+(CCScene *) scene
{
	CCScene *scene = [CCScene node];
	HelloWorldLayer *layer = [HelloWorldLayer node];
	[scene addChild: layer];
	return scene;
}

-(id) init
{
	if( (self=[super initWithColor:ccc4(255, 255, 255, 255)])) {
        [self addPlanes];
        [self schedule:@selector(addPlanes) interval:2.5];
	}
	return self;
}

- (void)addPlanes {
    int imageNo = arc4random()%6; imageNo++;
    NSString *imageName = [NSString stringWithFormat:@"Plane%i.png",imageNo];
    NSString *soundName = [NSString stringWithFormat:@"Plane%i.wav",imageNo];
    CCSprite *spritePlane = [CCSprite spriteWithFile:imageName];

    int duration ;

    switch (imageNo) {
        case 1:
            duration = 4; // sound duration for plane1.wav file
            break;
        case 2:
            duration = 6; // sound duration for plane2.wav file
            break;
        case 3:
            duration = 7;
            break;
        case 4:
            duration = 8;
            break;
        case 5:
            duration = 8;
            break;
        case 6:
            duration = 5;
            break;
        default:
            break;
    }
    [[SimpleAudioEngine sharedEngine] playEffect:soundName];

    int minY = spritePlane.contentSize.height/2;
    int maxY = WINSIZE.height - spritePlane.contentSize.height/2;
    int yCo = arc4random() % ( maxY-minY ) + minY;
    [spritePlane setPosition:ccp(-spritePlane.contentSize.width/2,yCo)];

    [self addChild:spritePlane];

    [spritePlane runAction:
     [CCSequence actions:
      [CCMoveTo actionWithDuration:duration position:ccp(WINSIZE.width+spritePlane.contentSize.width/2,yCo)],
      [CCCallFunc actionWithTarget:self selector:@selector(removePlanes:)]
      , nil]
     ];
}

- (void)removePlanes :(CCSprite *)plane {
    [self removeChild:plane cleanup:YES];
}

- (void) dealloc
{
	[super dealloc];
}

Grab the sample code.

Thanks & Regards,
Neon Spark

Leave a Reply

Your email address will not be published. Required fields are marked *