这是一大堆代码。评论应该非常有用。我想要做的是迭代一个CGRects的NSArray(包裹在NSValues中)并检查我们当前正在触摸的地方是否在其中一个CGRects中。
当我运行此代码时(注意for循环中的NSLog语句),它所做的就是说当前循环值为0.此外,当我触摸CGRects附近的地方时它会这样做应该。我的评论解释了我是如何创建CGRect的。谢谢!
#import "GameUI.h"
@implementation GameUI // This is a scene with just 1 layer on it.
+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
GameUI *layer = [GameUI node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// initialize your instance here
-(id) init
{
if( (self=[super initWithColor:ccc4(255,255,255,255)] )) {
// enable touches
self.isTouchEnabled = YES;
// unlock touching ingredients to start with
touchLocked = NO;
// attach nothing
attachedSprite = -1;
winSize = [CCDirector sharedDirector].winSize;
// Build the UI
// Counter
CCSprite *counter = [CCSprite spriteWithFile:@"counter.png"
rect:CGRectMake(0, 0, 480, 320)];
counter.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:counter];
// Let's hardcode some hitbox bounds.
// I got these values by using Photoshop's info panel and simply using the rectangular marquee tool.
// That gave me the origin point and then the two width+height values.
breadTouchbox = CGRectMake(241, 12, 71, 61);
onionTouchbox = CGRectMake(166, 11, 63, 53);
hamTouchbox = CGRectMake(115, 60, 56, 58);
cheeseTouchbox = CGRectMake(108, 128, 74, 74);
tomatoTouchbox = CGRectMake(47, 162, 52, 47);
lettuceTouchbox = CGRectMake(4, 78, 70, 77);
ketchupTouchbox = CGRectMake(2, 166, 38, 69);
mayoTouchbox = CGRectMake(6, 242, 31, 75);
NSValue *breadTouchboxValue = [NSValue valueWithCGRect:breadTouchbox];
NSValue *onionTouchboxValue = [NSValue valueWithCGRect:onionTouchbox];
NSValue *hamTouchboxValue = [NSValue valueWithCGRect:hamTouchbox];
NSValue *cheeseTouchboxValue = [NSValue valueWithCGRect:cheeseTouchbox];
NSValue *tomatoTouchboxValue = [NSValue valueWithCGRect:tomatoTouchbox];
NSValue *lettuceTouchboxValue = [NSValue valueWithCGRect:lettuceTouchbox];
NSValue *ketchupTouchboxValue = [NSValue valueWithCGRect:ketchupTouchbox];
NSValue *mayoTouchboxValue = [NSValue valueWithCGRect:mayoTouchbox];
// Put them in the array
touchboxArray = [[NSArray alloc] initWithObjects:breadTouchboxValue, onionTouchboxValue, hamTouchboxValue, cheeseTouchboxValue, tomatoTouchboxValue, lettuceTouchboxValue, ketchupTouchboxValue, mayoTouchboxValue, nil];
// Create the sprites we're going to drag around. Spawn them off screen somewhere.
breadSprite = [CCSprite spriteWithFile:@"breadslice.png" rect:CGRectMake(0,0,-200,-200)];
onionSprite = [CCSprite spriteWithFile:@"onionslices.png" rect:CGRectMake(0,0,-200,-200)];
hamSprite = [CCSprite spriteWithFile:@"hamslice.png" rect:CGRectMake(0,0,-200,-200)];
cheeseSprite = [CCSprite spriteWithFile:@"cheeseslices.png" rect:CGRectMake(0,0,-200,-200)];
tomatoSprite = [CCSprite spriteWithFile:@"tomatoslices.png" rect:CGRectMake(0,0,-200,-200)];
lettuceSprite = [CCSprite spriteWithFile:@"lettuceslice.png" rect:CGRectMake(0,0,-200,-200)];
ketchupSprite = [CCSprite spriteWithFile:@"ketchupsplurge.png" rect:CGRectMake(0,0,-200,-200)];
mayoSprite = [CCSprite spriteWithFile:@"mayosplurge.png" rect:CGRectMake(0,0,-200,-200)];
// Make sure you add them to this layer.
[self addChild:breadSprite];
[self addChild:onionSprite];
[self addChild:hamSprite];
[self addChild:cheeseSprite];
[self addChild:tomatoSprite];
[self addChild:lettuceSprite];
[self addChild:ketchupSprite];
[self addChild:mayoSprite];
// Put them in the array
spriteArray = [[NSArray alloc] initWithObjects:breadSprite, onionSprite, hamSprite, cheeseSprite, tomatoSprite, lettuceSprite, ketchupSprite, mayoSprite, nil];
}
return self;
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// Set the currently touched location to LOCATION.
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
// NSLog(@"%@", NSStringFromCGPoint(location));
// Check for a touch in any of the touchboxes. If yes, break out of the loop so you preserve arrayCount's value.
int arrayCount = 0;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for (int i = 0; i < [touchboxArray count]; i++) {
CGRect rect = [[touchboxArray objectAtIndex:i] CGRectValue];
if (CGRectContainsPoint(rect, location)) {
NSLog(@"The value of arrayCount is %i", arrayCount);
break;
}
}
[pool release];
// Now use that value to find the appropriate sprite and set it up for finger attachment.
// Remember to lock further touches [DO WE EVEN NEED TOUCHLOCKED? CAN'T SWIPE THROUGH SHIT ANYMORE]
//attachedSprite = arrayCount;
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
// Set the currently touched location to LOCATION.
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
// Make a holder sprite.
CCSprite *currentIngredient;
// Check and see if there is a sprite to be attached. If there is, attach it to the touch location.
if (attachedSprite != -1) {
currentIngredient = [spriteArray objectAtIndex:attachedSprite];
currentIngredient.position = location;
}
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// Remove any attachment.
attachedSprite = -1;
}
-(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
// Remove any attachment.
attachedSprite = -1;
}
@end
答案 0 :(得分:1)
@deanWombourne
在评论中提供的答案行
NSLog(@"The value of arrayCount is %i", arrayCount);
应该是
NSLog(@"The value of arrayCount is %i", i);