From: chsieh Date: Sun, 8 May 2011 04:39:47 +0000 (-0700) Subject: Dragging reference. X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=ce25f3480c9a58fd29a34c6dcddfd78d8d6d72bd;p=tanks-ios.git Dragging reference. --- diff --git a/tanks/tanks.xcodeproj/project.pbxproj b/tanks/tanks.xcodeproj/project.pbxproj index 18a38f9..defd577 100644 --- a/tanks/tanks.xcodeproj/project.pbxproj +++ b/tanks/tanks.xcodeproj/project.pbxproj @@ -30,6 +30,7 @@ 4996691A136930E8006E8125 /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 49966915136930E8006E8125 /* OpenGLES.framework */; }; 4996691B136930E8006E8125 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 49966916136930E8006E8125 /* QuartzCore.framework */; }; 4996691E13693180006E8125 /* Game.m in Sources */ = {isa = PBXBuildFile; fileRef = 4996691D13693180006E8125 /* Game.m */; }; + 4B7EFC0813763A5700EC81CB /* Unit.m in Sources */ = {isa = PBXBuildFile; fileRef = 4B7EFC0713763A5700EC81CB /* Unit.m */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -95,6 +96,8 @@ 49966916136930E8006E8125 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; 4996691C13693180006E8125 /* Game.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Game.h; sourceTree = ""; }; 4996691D13693180006E8125 /* Game.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Game.m; sourceTree = ""; }; + 4B7EFC0613763A5700EC81CB /* Unit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Unit.h; path = tanks/Game/Unit/Unit.h; sourceTree = ""; }; + 4B7EFC0713763A5700EC81CB /* Unit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Unit.m; path = tanks/Game/Unit/Unit.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -177,6 +180,7 @@ children = ( 499668D513692E2D006E8125 /* tanksAppDelegate.h */, 499668D613692E2D006E8125 /* tanksAppDelegate.m */, + 4B7EFC04137639FE00EC81CB /* Game */, 499668D813692E2D006E8125 /* iPhone */, 499668DF13692E2D006E8125 /* iPad */, 499668CD13692E2D006E8125 /* Supporting Files */, @@ -237,6 +241,24 @@ name = "Supporting Files"; sourceTree = ""; }; + 4B7EFC04137639FE00EC81CB /* Game */ = { + isa = PBXGroup; + children = ( + 4B7EFC0513763A1400EC81CB /* Unit */, + ); + name = Game; + path = ..; + sourceTree = ""; + }; + 4B7EFC0513763A1400EC81CB /* Unit */ = { + isa = PBXGroup; + children = ( + 4B7EFC0613763A5700EC81CB /* Unit.h */, + 4B7EFC0713763A5700EC81CB /* Unit.m */, + ); + name = Unit; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -371,6 +393,7 @@ 499668DB13692E2D006E8125 /* tanksAppDelegate_iPhone.m in Sources */, 499668E213692E2D006E8125 /* tanksAppDelegate_iPad.m in Sources */, 4996691E13693180006E8125 /* Game.m in Sources */, + 4B7EFC0813763A5700EC81CB /* Unit.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/tanks/tanks/Game.h b/tanks/tanks/Game.h index 92a1fec..7bd6a65 100644 --- a/tanks/tanks/Game.h +++ b/tanks/tanks/Game.h @@ -8,8 +8,11 @@ #import #import "Sparrow.h" +#import "Game/Unit/Unit.h" @interface Game : SPStage +@property(nonatomic, assign) Unit* unit; + @end diff --git a/tanks/tanks/Game.m b/tanks/tanks/Game.m index 7948dbb..3f58a87 100644 --- a/tanks/tanks/Game.m +++ b/tanks/tanks/Game.m @@ -11,20 +11,21 @@ @implementation Game +@synthesize unit; + - (id)initWithWidth:(float)width height:(float)height { - if ((self = [super initWithWidth:width height:height])) - { - // this is where the code of your game will start. - // in this sample, we add just a simple quad to see - // if it works. + if ((self = [super initWithWidth:width height:height])) + { + // this is where the code of your game will start. + // in this sample, we add just a simple quad to see + // if it works. - SPQuad *quad = [SPQuad quadWithWidth:100 height:100]; - quad.color = 0xff0000; - quad.x = 50; - quad.y = 50; - [self addChild:quad]; - } - return self; + unit = [[Unit alloc] initWithWidth:100 height:100 X:50 Y:50]; + [self addEventListener:@selector(onTouch:) atObject:unit forType:SP_EVENT_TYPE_TOUCH]; + [self addChild:unit.quad]; + } + return self; } + @end \ No newline at end of file diff --git a/tanks/tanks/Game/Unit/Unit.h b/tanks/tanks/Game/Unit/Unit.h new file mode 100644 index 0000000..1c8fea5 --- /dev/null +++ b/tanks/tanks/Game/Unit/Unit.h @@ -0,0 +1,20 @@ +// +// Unit.h +// tanks +// +// Created by Doris Chen on 5/7/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import +#import "Sparrow.h" + + +@interface Unit : NSObject { +} + +@property(nonatomic, retain) SPQuad* quad; + +-(Unit*)initWithWidth:(int)width height:(int)height X:(int)x Y:(int)y; +-(void)onTouch:(SPTouchEvent*)event; +@end diff --git a/tanks/tanks/Game/Unit/Unit.m b/tanks/tanks/Game/Unit/Unit.m new file mode 100644 index 0000000..66da593 --- /dev/null +++ b/tanks/tanks/Game/Unit/Unit.m @@ -0,0 +1,34 @@ +// +// Unit.m +// tanks +// +// Created by Doris Chen on 5/7/11. +// Copyright 2011 __MyCompanyName__. All rights reserved. +// + +#import "Unit.h" + + +@implementation Unit + +@synthesize quad; + +-(Unit*)initWithWidth:(int)width height:(int)height X:(int)x Y:(int)y { + quad = [SPQuad quadWithWidth:width height:height]; + quad.color = 0xff0000; + quad.x = x; + quad.y = y; + + return self; +} + +-(void)onTouch:(SPTouchEvent*)event { + SPTouch* touch = [[event touchesWithTarget:quad.parent] anyObject]; + if (touch) { + SPPoint* touchPosition = [touch locationInSpace:quad.parent]; + quad.x = touchPosition.x - quad.width / 2.0f; + quad.y = touchPosition.y - quad.height / 2.0f; + } +} + +@end