Skip to content

Instantly share code, notes, and snippets.

@mkhl
mkhl / autoscoped.h
Created December 8, 2008 18:27
Objective-C macros
// GCC Attribute for autoscoped Obj-C objects
// Source: http://www.cocoabuilder.com/archive/message/cocoa/2009/3/13/232287
#define autoscoped __attribute__((cleanup(releaseObject)))
static inline void releaseObject(id *object)
{
[*object release];
}
@solidline
solidline / detect_cocos2d.m
Created September 10, 2009 22:44
Simple touch delegation method to find if a object has been hit
- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView:touch.view]];
CGRect myRect = CGRectMake(sprite.position.x, sprite.position.y, sprite.contentSize.width, sprite.contentSize.height);
if(CGRectContainsPoint(myRect, location)) {
// particularSprite touched
return kEventHandled;
glEnable(GL_SCISSOR_TEST);
// Some region to clip to
CGRect rect = CGRect(100, 100, 200, 200);
CGRect frame = [[[CCDirector sharedDirector] openGLView] frame];
// Adjust for position
CGPoint worldPos = [self convertToWorldSpaceAR:position_];
@kernel1983
kernel1983 / gist:646532
Created October 26, 2010 08:24
create looping background in cocos2D
-(id) init
{
if( (self=[super init] )) {
CGSize size = [[CCDirector sharedDirector] winSize];
CCTexture2D *bg = [[CCTexture2D alloc] initWithImage:[UIImage imageNamed:@"Default.png"]];
CCSprite *staticBackground0 = [CCSprite spriteWithTexture:bg];
staticBackground0.position = ccp(-size.width/2,size.height/2);
CCSprite *staticBackground1 = [CCSprite spriteWithTexture:bg];
staticBackground1.position = ccp(size.width/2,size.height/2);
@ecpplus
ecpplus / gist:713131
Created November 24, 2010 04:51
Cocos2d(0.99.4) create animation by CCSpriteSheet
CCSpriteSheet *animationSheet = [CCSpriteSheet spriteSheetWithFile:@"animation_sheet.png"];
[self addChild:animationSheet];
CCSprite *animationSprite = [CCSprite spriteWithTexture:animationSheet.texture
rect:CGRectMake(0, 0, 256, 290)];
animationSprite.position = ccp(self.contentSize.width / 2, self.contentSize.height / 2);
[animationSheet addChild:animationSprite];
CCAnimation *animation = [CCAnimation animationWithName:@"an_animation" delay:0.04f];
@rummelonp
rummelonp / NSObject+Swizzle.h
Created January 21, 2011 02:48
Objective-Cでメソッド定義を差し替えるメソッド
@interface NSObject(Swizzle)
+ (void)swizzleMethod:(SEL)orig_sel withMethod:(SEL)alt_sel;
@end
@rummelonp
rummelonp / SomeController.m
Created January 23, 2011 04:12
Objective-Cのクロージャ
// Make button block.
UIBarButtonItem* (^buttonWithTitle)(NSString*, SEL) = ^(NSString* title, SEL action)
{
UIBarButtonItem* button = [UIBarButtonItem alloc];
[button initWithTitle:title
style:UIBarButtonItemStyleBordered
target:self
action:action];
[button setWidth:65.0f];
[button autorelease];
@sanemat
sanemat / gist:923282
Created April 16, 2011 16:58
Objective-C gitignore
curl -o .gitignore https://github.com/github/gitignore/raw/master/Objective-C.gitignore
@alexbw
alexbw / AutoEncoder.h
Created June 28, 2011 19:58
Autoencoder for Objective-C
//
// AutoEncoder.h
// QuartzTest
//
// Created by Alex Wiltschko on 6/27/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
@pratikshabhisikar
pratikshabhisikar / gist:1136244
Created August 10, 2011 06:26
Dismissing UIView manually
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[self.view removeFromSuperview];
}
-(void) dismiss {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];