Currently Browsing

idevblogaday

Cocos2D: Changing Filter Data During Gameplay

I’ve been working on making Sewer Wars gaming experience easier for the player. I found an easy solution: disabling collisions between player and enemy objects when the player is jumping and the enemy object is close to it. With this easy to implement solution the player will not die or get hurt (collide) when doing a jump when being very close to the enemy object.

Add this code to the function you’ll call when the player (is jumping) and enemy objects are close enough:

b2Filter filterData;
filterData = b->GetFixtureList()->GetFilterData();
filterData.maskBits = 0xFFFF ^ (0x0002); // Add the categoryBits you want your enemy object to ignore
b->GetFixtureList()->SetFilterData(filterData);

b is a b2Body of your enemy. So in the above code, you are changing b’s enemy filter data to ignore collisions with the player’s categoryBits (0×0002).

If you don’t know what maskBits are or how collision filtering works, read my Cocos2D/Box2D Collision Filtering tutorial.

Cocos2D/Box2D Endless Platformer

It’s been a long time since my last iDevBlogADay contribution. I’ve been (and still am) very busy, but wanted to share with you this code. I’m working on an endless platformer game (www.sewerwars.com) and i didn’t find any tutorial or source code to help me start with the development so I’m sharing with the Cocos2D community part of my code.

I did find a tutorial from www.uchidacoonga.com (some of my code uses snippets from this tutorial) which helped me understand how to move Box2D objects.

With this demo you will be able to:

- Create endless platforms
- Build platforms with different sizes
- Position platforms in random positions
- Assign three CCSprite to the Box2D platform (see figure 1)

The code is optimized for iPhone retina, but with some minor modifications you can make it work under iPhone & iPad screens.

If you find any bugs or can optimize the code please let me know!

Click here to download the source code.

Steve Jobs’ Best Invention

I’ve been a Windows/Linux developer almost all my life. I didn’t own any Apple devices (except an iPod), had no idea what Objective-C was and didn’t even know how OSX worked.
It all changed when I purchased my first iPhone back in 2008. I used to own a Nokia phone by then and that new iPhone device truly seemed magical in comparison.
Since then I work only on Apple devices (MBP, iPhone & iPad) and use Xcode on a daily basis. It has been a great change, working on these outstanding devices is extremely satisfying and it encourages me to develop better software.

I’d like the iDevBlogADay community to take this poll to know which of Steve Jobs’ Inventions is the greatest. My vote clearly goes to the iPhone, if it wasn’t for this device, today I wouldn’t be working on my first game!

There seems to be a problem with PollDaddy’s poll code & my blog site design, please scroll down to view all options available besides the iPod!


Cocos2D/Box2D – Detecting top collisions

Cocos2D/Box2D – Detecting top collisions

Introduction

In this IDevBlogADay post I’d like to share with you my approach to detecting top collisions. By top collisions I refer to when the player character jumps on top of an enemy. In my game, Sewer Wars (www.sewerwars.com), when Joe Gillis (player character) jumps on top of the rats, he squashes them. When I first started thinking how to achieve this I thought there would be plenty of examples out there, but I was not able to find any.

Sewer Wars

Sewer Wars - Detecting top collisions, see the rat squashed at the left

The Code

Inside the collision detection loop:

std::vector<b2Body *>toDestroy;
std::vector<MyContact>::iterator pos;

for(pos = _contactListener->_contacts.begin(); pos != _contactListener->_contacts.end(); ++pos) {

	// Get the box2d bodies for each object
        b2Body *bodyA = contact.fixtureA->GetBody();
        b2Body *bodyB = contact.fixtureB->GetBody();

I added the following code:

if((bodyBVec.y - bodyAVec.y) > 1.0f){ // Detect top collision by getting the difference between y coordinates of both bodies (player - enemy)
	// Player hit on top of enemy
}

Conclusion

I don’t know if this is the best way to detect these type of collisions, or if it would be better to use fixtures, but it works quite well.
I’d love to know how other developers do their “on top” collisions, so please comment!

Cocos2D Audio – CocosDenshion

Introduction

In this iDevBlogADay tutorial I will show you how to add music and effects to your iPhone game. You will learn how to play effects passing pan, pitch & gain parameters, preload background music & adding steps effects. We will use CocosDenshion to achieve this. This audio library is already included in Cocos2D, so you will not need to download or configure anything! For a complete overview of what CocosDenshion is please read its FAQ at http://cocos2d-iphone.org/wiki/doku.php/cocosdenshion:faq

Supported formats

CocosDenshion supports the following audio formats:

MP3 (IOS 3.0 onward)
WAV
IMA4 (1/4 the size of a WAV file)

To convert a WAV file to IMA4:

Open Terminal and type:
afconvert -f caff -d ima4 mysound.wav

The code

Add this line at the beginning of your implementation file (below #import “HelloWorldLayer.h”)

#import "SimpleAudioEngine.h"

Add these lines inside your init method

//Preloading background music
[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"background.mp3"];

//Preloading effects
[[SimpleAudioEngine sharedEngine] preloadEffect:@"asphalt1.mp3"];

//Play background music
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"background.mp3"];

//Play effect
[[SimpleAudioEngine sharedEngine] playEffect:@"effect.wav"];

//Play effect advanced settings
[[SimpleAudioEngine sharedEngine] playEffect:stepEffect pitch:1.0f pan:0.0f gain:0.6f];

Values:
pitch: 0.5f (half the speed) to 2.0f (twice the speed). 1.0f to play normal speed
pan: -1.0f fully left, 1.0f fully right. 0.0f centered.
pitch: 1.0f unchanged. Each division by 2 equals a -6dB reduction

//Pause background music
[[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];

// Background music volume
[CDAudioManager sharedManager].backgroundMusic.volume = 1.0f;

// Get noticed when background music stops
[[CDAudioManager sharedManager] setBackgroundMusicCompletionListener:self selector:@selector(backgroundMusicFinished)]; 

// Loop background music
[[CDAudioManager sharedManager] playBackgroundMusic:@"background.mp3" loop:YES];
[CDAudioManager sharedManager].backgroundMusic.numberOfLoops = 3; //To loop 3 times

Adding Steps:

Use this code to add steps sounds to your characters.

Add this line to your interface file:

CCLabelTTF *label;
BOOL bStepEffect;

Add these functions above your init method:

-(void) backgroundMusicFinished{

    CCLOG(@"Background Music Completed");
    [label setString:@"Music completed"];

}

-(void) playStepEffect {

    [[SimpleAudioEngine sharedEngine] playEffect:@"asphalt1.mp3" pitch:1.0f pan:0.0f gain:0.6f];

}

- (void) playSteps: (ccTime) dt{

    [self playStepEffect];

}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { 

    CCLOG(@"Start/Stop Steps");

    if(bStepEffect == FALSE){

        [self playStepEffect];
        [self schedule:@selector(playSteps:) interval:0.3f];
        bStepEffect = TRUE;

    }else{

        [self unschedule:@selector(playSteps:)];
        bStepEffect = FALSE;

    }

    return TRUE;

}

Finally, add the following lines inside your init method:

bStepEffect = FALSE;

self.isTouchEnabled = YES;
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

Positional Audio:

@Crocodella has an excellent article explaining what positional audio is and its benefits.
“The term itself is pretty self-explanatory, in that positional audio is the simulation of the ability that we humans have to judge the approximate distance and direction of a sound source, and also the physical properties of sound based on the environment.” You can find the complete tutorial here:
http://www.crocodella.com.br/2011/05/positional-audio-with-cocos2d-and-cocosdenshion/

Professional Music:

Our game music is being composed by @wblackall, he’s doing an amazing job, so I recommend him if you plan on adding background music to your game.

Sources
Click here to download the source code of this tutorial