-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCenterBall.m
More file actions
119 lines (93 loc) · 2.94 KB
/
CenterBall.m
File metadata and controls
119 lines (93 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* TouchesTest (c) Valentin Milea 2009
*/
#import "CenterBall.h"
#import "cocos2d.h"
#import "CharMovementDelegate.h"
@implementation CenterBall
@synthesize movementListener = movementListener;
- (id)init
{
hasBeenTouched = NO;
[super init];
return self;
}
- (CGRect)rectInPixels
{
CGSize s = [texture_ contentSizeInPixels];
return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}
- (CGRect)rect
{
CGSize s = [texture_ contentSize];
return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}
+ (id)centerBallWithTexture:(CCTexture2D *)aTexture
{
return [[[self alloc] initWithTexture:aTexture] autorelease];
}
- (id)initWithTexture:(CCTexture2D *)aTexture
{
if ((self = [super initWithTexture:aTexture]) ) {
}
return self;
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *t = [[touches allObjects] objectAtIndex:0];
CGPoint tPoint = [t locationInView:[t view]];
tPoint = [[CCDirector sharedDirector] convertToGL:tPoint];
self.position = CGPointMake(tPoint.x, tPoint.y);
mTouchInitialPoint = self.position;
self.visible=YES;
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[touches allObjects] objectAtIndex:0];
CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
CGFloat dx = mTouchInitialPoint.x - touchPoint.x;
CGFloat dy = mTouchInitialPoint.y - touchPoint.y;
CGFloat r = 25;
if (hasBeenTouched) {
if (sqrt(dx*dx + dy*dy) < r) {
self.position = CGPointMake(touchPoint.x, touchPoint.y);
} else {
self.position = CGPointMake(mTouchInitialPoint.x - (dx*r)/sqrt(pow(dy,2) + pow(dx,2)),mTouchInitialPoint.y - (dy*r)/sqrt(pow(dy,2) + pow(dx,2)));
}
} else {
hasBeenTouched = YES;
}
mLastTouchPoint.x = touchPoint.x;
mLastTouchPoint.y = touchPoint.y;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
hasBeenTouched = NO;
self.visible=NO;
}
- (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{
}
- (void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addStandardDelegate:self priority:0];
[super onEnter];
}
- (void)onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
[super onExit];
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
// If it weren't for the TouchDispatcher, you would need to keep a reference
// to the touch from touchBegan and check that the current touch is the same
// as that one.
// Actually, it would be even more complicated since in the Cocos dispatcher
// you get NSSets instead of 1 UITouch, so you'd need to loop through the set
// in each touchXXX method.
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
hasBeenTouched = NO;
self.visible=NO;
}
@end