iPhone Cocos2d: Use UIView as background of Cocos2d
This came from a blog called "Not Now Nigel", unfortunately it was removed. I'm salvaging this helpful code from there.
Using a hello world cocos2d template, add the following property to your ____AppDelegate.h:
@property (nonatomic, retain) UIView *overlay;
in the ____AppDelegate.m, synthesize the overlay:
then replace the contents of:
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if(![CCDirector setDirectorType:kCCDirectorTypeDisplayLink])
[CCDirector setDirectorType:kCCDirectorTypeNSTimer];
CCDirector *director = [CCDirector sharedDirector];
[director setDeviceOrientation:kCCDeviceOrientationPortrait];
[director setDisplayFPS:YES];
[director setAnimationInterval:1.0/60];
EAGLView *glView = [EAGLView viewWithFrame:[[UIScreen mainScreen] bounds] pixelFormat:kEAGLColorFormatRGBA8 depthFormat:0 preserveBackbuffer:NO];
[director setOpenGLView:glView];
[glView setMultipleTouchEnabled:YES];
glView.opaque = NO;
glClearColor(0.0f,0.0f,0.0f,0.0f);
glClear(GL_COLOR_BUFFER_BIT);
overlay = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
overlay.opaque = YES;
overlay.backgroundColor = [UIColor blackColor];
[overlay addSubview: glView];
[window addSubview:overlay];
[window makeKeyAndVisible];
// Sets landscape mode
// [director setDeviceOrientation:kCCDeviceOrientationLandscapeLeft];
// Turn on display FPS
// [director setDisplayFPS:YES];
[CCTexture2D setDefaultAlphaPixelFormat: kTexture2DPixelFormat_RGBA8888];
[director runWithScene: [HelloWorld scene]];
}
In your scene.m, just initialize your UIView (or view controller) then insert the subview as the back-most subview for overlay. For example:
MyNewController *cont = [[MyNewController alloc] init];
cont.wantsFullScreenLayout = YES;
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.overlay insertSubview:cont.view belowSubview:[[CCDirector sharedDirector] openGLView]];
The glView for cocos2d should not be transparent with the UIView you just placed in the background.
iPhone: Implement Swipe-to-Delete in UITableView
Make sure your controller is a delegate of your table view:
Implement this in your controller:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView beginUpdates];
if (editingStyle == UITableViewCellEditingStyleDelete) {
/* delete your entry here */
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
}
[tableView endUpdates];
}
Also make sure that your method numberOfRowsInSection reflects the change in data.
iPhone: Sort NSMutableArray of Custom Objects by Property
Bookmark.h
//
// Bookmark.h
// Mapjack
//
// Created by Benjamin Rafael F. Intal on 8/2/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import
@interface Bookmark : NSObject {
NSString *name;
NSString *address;
NSString *position;
}
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *address;
@property (nonatomic, copy) NSString *position;
@end
Bookmark.m
//
// Bookmark.m
// Mapjack
//
// Created by Benjamin Rafael F. Intal on 8/2/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "Bookmark.h"
@implementation Bookmark
@synthesize name;
@synthesize address;
@synthesize position;
- (id) initWithCoder: (NSCoder *)coder
{
self = [[Bookmark alloc] init];
if (self != nil)
{
self.name = [coder decodeObjectForKey:@"name"];
self.address = [coder decodeObjectForKey:@"address"];
self.position = [coder decodeObjectForKey:@"position"];
}
return self;
}
- (void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject:name forKey:@"name"];
[coder encodeObject:address forKey:@"address"];
[coder encodeObject:position forKey:@"position"];
}
@end
Somewhere in the controller, declare your NSMutableArray of Bookmark objects (or any other objects), then sort using:
In this sample, I sorted using the name property
// Sort the bookmarks by name
NSSortDescriptor *firstDescriptor =
[[[NSSortDescriptor alloc]
initWithKey:@"name"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray * descriptors =
[NSArray arrayWithObjects:firstDescriptor, nil];
NSArray * sortedArray =
[localBookmarks sortedArrayUsingDescriptors:descriptors];
iPhone: Save / Load NSMutableArray of Custom Object from NSUserDefaults
Bookmark.h
//
// Bookmark.h
// Mapjack
//
// Created by Benjamin Rafael F. Intal on 8/2/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import
@interface Bookmark : NSObject {
NSString *name;
NSString *address;
NSString *position;
}
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *address;
@property (nonatomic, copy) NSString *position;
@end
Bookmark.m
//
// Bookmark.m
// Mapjack
//
// Created by Benjamin Rafael F. Intal on 8/2/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "Bookmark.h"
@implementation Bookmark
@synthesize name;
@synthesize address;
@synthesize position;
- (id) initWithCoder: (NSCoder *)coder
{
self = [[Bookmark alloc] init];
if (self != nil)
{
self.name = [coder decodeObjectForKey:@"name"];
self.address = [coder decodeObjectForKey:@"address"];
self.position = [coder decodeObjectForKey:@"position"];
}
return self;
}
- (void) encodeWithCoder: (NSCoder *)coder
{
[coder encodeObject:name forKey:@"name"];
[coder encodeObject:address forKey:@"address"];
[coder encodeObject:position forKey:@"position"];
}
@end
Retrieve procedure in controller:
localBookmarks = [[NSMutableArray alloc] init];
// Load from prefs
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"bookmarks"];
if (dataRepresentingSavedArray != nil) {
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
localBookmarks = [[NSMutableArray alloc] initWithArray:oldSavedArray];
else
localBookmarks = [[NSMutableArray alloc] init];
}
Save procedure in controller:
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
[currentDefaults setObject:[NSKeyedArchiver archivedDataWithRootObject: localBookmarks] forKey:@"bookmarks"];
[currentDefaults synchronize];
Helpful Python Methods
import os
def getFreeSpace(path):
# Gets the number of free bytes in the given path
st = os.statvfs(path)
return st.f_bsize * st.f_bavail
def getFileSize(filepath):
# Gets the size of the file in bytes
try:
st = os.stat(filepath)
except IOError:
print "Failed to get size of", filepath
else:
return st[6]
Disable Image Dragging in Firefox 3
Put this line inside the image tag:
onmousedown="if (event.preventDefault) { event.preventDefault(); }"