#import "GameplayLayer.h"
@implementation GameplayLayer
-(id)init {
self = [super init];
[self scheduleUpdate];
CGSize size =[[CCDirector sharedDirector] winSize];
if (self != nil) {
screenHeight = size.height;
srceenWidth = size.width;
// enable touches
duckSprite = [CCSprite spriteWithFile:@"duck1.png"
rect:CGRectMake(0, 0, 20, 20)];
duckSprite.position = ccp(100, screenHeight/2);
[self addChild:duckSprite];
CCAnimation *duckAnim = [CCAnimation animation];
[duckAnim addFrameWithFilename:@"duck1.png"];
[duckAnim addFrameWithFilename:@"duck2.png"];
id duckAnimationAction =
[CCAnimate actionWithDuration:0.5f
animation:duckAnim
restoreOriginalFrame:YES];
id repeatduckAnimation =
[CCRepeatForever actionWithAction:duckAnimationAction];
[duckSprite runAction:repeatduckAnimation];
crosshair = [CCSprite spriteWithFile:@"crosshair.png"
rect:CGRectMake(0, 0, 125, 125)];
[self addChild:crosshair];
[self schedule:@selector(updatecrosshair:) interval:1.0/60.f];
crosshair.position = CGPointMake( size.width / 2, size.height / 2);
self.isAccelerometerEnabled = YES;
CCMenuItem *shoot = [CCMenuItemImage itemFromNormalImage:@"Shootbutton.png" selectedImage:@"Shootbuttons.png" target:self selector:@selector(shootbuttonin:)];
shoot.position = ccp(450,30);
[self addChild:shoot];
}
return self;
}
-(void) updatecrosshair:(ccTime) delta{
if (crosshair.position.x >= 0 && crosshair.position.x <= srceenWidth) {
crosshair.position = ccp(crosshair.position.x + tiltHorizontal , crosshair.position.y );
} else if (crosshair.position.x < 0) {
crosshair.position = ccp (0, crosshair.position.y );
} else if ( crosshair.position.x > srceenWidth) {
crosshair.position = ccp ( srceenWidth ,crosshair.position.y );
}
if (crosshair.position.y >= 0 && crosshair.position.y <= screenHeight) {
crosshair.position = ccp (crosshair.position.x , crosshair.position.y + tiltVertically);
} else if (crosshair.position.y < 0) {
crosshair.position = ccp ( crosshair.position.x ,0 );
} else if ( crosshair.position.y > screenHeight) {
crosshair.position = ccp ( crosshair.position.x , screenHeight);
}
}
- (void)shootbuttonin:(id)sender {
[[CCDirector sharedDirector] winSize];
CGRect duckRect = CGRectMake(
duckSprite.position.x - (duckSprite.contentSize.width/2),
duckSprite.position.y - (duckSprite.contentSize.height/2),
duckSprite.contentSize.width,
duckSprite.contentSize.height);
CGRect crosshairRect = CGRectMake(
crosshair.position.x - (crosshair.contentSize.width/2),
crosshair.position.y - (crosshair.contentSize.height/2),
crosshair.contentSize.width,
crosshair.contentSize.height);
if (CGRectIntersectsRect(duckRect, crosshairRect)) {
CCLOG(@"Collision!");
}
}
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{
CCLOG(@"Acceleration Values are x:%f y:%f z:%f", acceleration.x , acceleration.y, acceleration.z );
tiltHorizontal = acceleration.y * 20;
tiltVertically = (acceleration.x + .4) *20;
CCLOG(@"tiltHorizontal is %i", tiltHorizontal );
}
- (void) dealloc
{
// in case you have something to dealloc, do it in this method
// in this particular example nothing needs to be released.
// cocos2d will automatically release all the children (Label)
// don't forget to call "super dealloc"
[super dealloc];
}
@end
2012-02-04 09:56:38.549 DuckHunting[1004:707] cocos2d: cocos2d v1.0.1
2012-02-04 09:56:38.554 DuckHunting[1004:707] cocos2d: Using Director Type:CCDirectorDisplayLink
2012-02-04 09:56:38.903 DuckHunting[1004:707] cocos2d: OS version: 5.0.1 (0x05000100)
2012-02-04 09:56:38.904 DuckHunting[1004:707] cocos2d: GL_VENDOR: Imagination Technologies
2012-02-04 09:56:38.906 DuckHunting[1004:707] cocos2d: GL_RENDERER: PowerVR SGX 543
2012-02-04 09:56:38.907 DuckHunting[1004:707] cocos2d: GL_VERSION: OpenGL ES-CM 1.1 IMGSGX543-63.14.2
2012-02-04 09:56:38.910 DuckHunting[1004:707] cocos2d: GL_MAX_TEXTURE_SIZE: 4096
2012-02-04 09:56:38.911 DuckHunting[1004:707] cocos2d: GL_MAX_MODELVIEW_STACK_DEPTH: 16
2012-02-04 09:56:38.912 DuckHunting[1004:707] cocos2d: GL_MAX_SAMPLES: 4
2012-02-04 09:56:38.914 DuckHunting[1004:707] cocos2d: GL supports PVRTC: YES
2012-02-04 09:56:38.915 DuckHunting[1004:707] cocos2d: GL supports BGRA8888 textures: YES
2012-02-04 09:56:38.917 DuckHunting[1004:707] cocos2d: GL supports NPOT textures: YES
2012-02-04 09:56:38.918 DuckHunting[1004:707] cocos2d: GL supports discard_framebuffer: YES
2012-02-04 09:56:38.919 DuckHunting[1004:707] cocos2d: compiled with NPOT support: NO
2012-02-04 09:56:38.921 DuckHunting[1004:707] cocos2d: compiled with VBO support in TextureAtlas : YES
2012-02-04 09:56:38.922 DuckHunting[1004:707] cocos2d: compiled with Affine Matrix transformation in CCNode : YES
2012-02-04 09:56:38.924 DuckHunting[1004:707] cocos2d: compiled with Profiling Support: NO
2012-02-04 09:56:39.025 DuckHunting[1004:707] cocos2d: surface size: 480x320
2012-02-04 09:56:39.237 DuckHunting[1004:707] cocos2d: Frame interval: 1
2012-02-04 09:56:39.266 DuckHunting[1004:707] -[GameplayLayer update:]: unrecognized selector sent to instance 0x4361d0
2012-02-04 09:56:39.268 DuckHunting[1004:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[GameplayLayer update:]: unrecognized selector sent to instance 0x4361d0'
*** First throw call stack:
(0x36ff88bf 0x316c61e5 0x36ffbacb 0x36ffa945 0x36f55680 0x4f5c9 0x6d7d5 0x6f525 0x33552423 0x33552379 0x3067af93 0x360b0891 0x36fc1f43 0x36fcc553 0x36fcc4f5 0x36fcb343 0x36f4e4dd 0x36f4e3a5 0x31104fcd 0x325c0743 0x916eb 0x2724)
terminate called throwing an exception(gdb)