My little piece of the digital multiverse
Archive for February, 2011
cocos2d-iphone: A Binary Clock
02 years ago
Here is a CCScene based Binary Clock implementation in cocos2d-iphone. I used 0.99.5 for this, but it should compile flawlessly on 0.99.4 as well…
ClockScene.h
//
// ClockScene.h
// BinaryClock
//
// Created by Peter Mares on 23/02/2011.
// Copyright 2011. All rights reserved.
//
// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"
// HelloWorld Layer
@interface ClockScene : CCScene
{
CCLabelTTF *_arrLabels[6];
}
+(id) scene;
@end
ClockScene.m
//
// ClockScene.m
// BinaryClock
//
// Created by Peter Mares on 23/02/2011.
// Copyright 2011. All rights reserved.
//
// Import the interfaces
#import "ClockScene.h"
////////////////////////////////////////////////////////////////////////////////////////////////
@interface ClockScene ()
- (void) onTick:(ccTime)dt;
- (void) updateClockDisplay;
@end
////////////////////////////////////////////////////////////////////////////////////////////////
// HelloWorld implementation
@implementation ClockScene
+(id) scene
{
ClockScene *scene = [ClockScene node];
return scene;
}
////////////////////////////////////////////////////////////////////////////////////////////////
-(id) init
{
if( (self=[super init] ))
{
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCLabelTTF *label = nil;
float yOffset = 0;
for ( int i = 0; i < 6; i++ )
{
yOffset = (i % 2) * 25;
label = [CCLabelTTF labelWithString:@"0000" fontName:@"Helvetica-Bold" fontSize:16];
// some arbitrary magic numbers to make it work - you can make this more dynamic...
label.position = ccp( winSize.width / 2 - ( (1-(i/2)) * 100 ), winSize.height / 2 - yOffset);
[label setColor:ccc3(255, 255, 255)];
_arrLabels[i] = label;
[self addChild:label];
}
[self updateClockDisplay];
[self schedule:@selector(onTick:) interval:1];
}
return self;
}
////////////////////////////////////////////////////////////////////////////////////////////////
- (void) dealloc
{
[super dealloc];
}
////////////////////////////////////////////////////////////////////////////////////////////////
- (void) onTick:(ccTime)dt
{
[self updateClockDisplay];
}
////////////////////////////////////////////////////////////////////////////////////////////////
- (void) updateClockDisplay
{
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:now];
int timeComponents[6];
// now transform the hours, minutes and seconds into binary nibbles
timeComponents[0] = [components hour] / 10;
timeComponents[1] = [components hour] % 10;
timeComponents[2] = [components minute] / 10;
timeComponents[3] = [components minute] % 10;
timeComponents[4] = [components second] / 10;
timeComponents[5] = [components second] % 10;
// translate each component into a binary nibble string
for (int i = 0; i < 6; i++ )
{
NSMutableString* str = [NSMutableString string];
for ( int bit = 8; bit > 0; bit >>= 1 )
timeComponents[i] & bit ? [str appendString:@"1"] : [str appendString:@"0"];
[_arrLabels[i] setString:str];
}
}
@end