Cocos2d iphone 尝试触摸阵列中的项目不工作
我有一个数组在我的触摸开始的方法(我想能够触摸精灵) 为了让它记录触摸)是我忘记做什么了还是我做错了什么 我可以将触摸记录在屏幕上,但当我触摸气泡时,什么也没有发生。 任何帮助都会很好Cocos2d iphone 尝试触摸阵列中的项目不工作,cocos2d-iphone,Cocos2d Iphone,我有一个数组在我的触摸开始的方法(我想能够触摸精灵) 为了让它记录触摸)是我忘记做什么了还是我做错了什么 我可以将触摸记录在屏幕上,但当我触摸气泡时,什么也没有发生。 任何帮助都会很好 -(id) init { if((self=[super initWithColor:ccc4(10, 10, 10,10)]) ) //sand 101, 116, 88 { size = [[CCDirector sharedDirector] winSize]; self.touchEn
-(id) init
{
if((self=[super initWithColor:ccc4(10, 10, 10,10)]) ) //sand 101, 116, 88
{
size = [[CCDirector sharedDirector] winSize];
self.touchEnabled = YES;
//other stuff here
Bubble01 = [[Bubble alloc]initWithBubbleWithLabel:@"_Bubble.png" opacity:255 gloss:@"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:@"%@", [Sortingarray objectAtIndex:BubbleAnswerBubble_1_IndexValue]]];
Bubble02 = [[Bubble alloc]initWithBubbleWithLabel:@"_Bubble.png" opacity:255 gloss:@"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:@"%@", [Sortingarray objectAtIndex:BubbleAnswerBubble_2_IndexValue]]];
Bubble03 = [[Bubble alloc]initWithBubbleWithLabel:@"_Bubble.png" opacity:255 gloss:@"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:@"%@", [Sortingarray objectAtIndex:BubbleAnswerBubble_3_IndexValue]]];
Bubble04 = [[Bubble alloc]initWithBubbleWithLabel:@"_Bubble.png" opacity:255 gloss:@"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:@"%@", [Sortingarray objectAtIndex:BubbleAnswerBubble_4_IndexValue]]];
Bubble05 = [[Bubble alloc]initWithBubbleWithLabel:@"_Bubble.png" opacity:255 gloss:@"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:@"%@", [Sortingarray objectAtIndex:BubbleAnswerBubble_5_IndexValue]]];
Bubble06 = [[Bubble alloc]initWithBubbleWithLabel:@"_Bubble.png" opacity:255 gloss:@"_Bubble_overlay.png" posX:0 posY:0 data:[NSString stringWithFormat:@"%@", [Sortingarray objectAtIndex:AnswerBubble_6_IndexValue]]];
//other stuff here
}
return self;
}
接触开始了
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
//set up touches
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];
//log if touches are working if I touch the screen area
NSLog(@"touches screen");
//create an array from bubble class (CCSprites with labels,
//I need to be able to determine which sprite was touched and run an action on it)
BubbleArray = [[NSMutableArray alloc]initWithObjects:Bubble01,
Bubble02,
Bubble03,
Bubble04,
Bubble05,
Bubble06,
nil];
for(int i = 0; i < [BubbleArray count]; i++)
{
Bubble *sprite = (Bubble *)[BubbleArray objectAtIndex:i];
//create a rect to find the position and size of the sprite
//BackBubble is a sprite that i'm using to detect the content size
CGRect targetRect = CGRectMake(
sprite.BackBubble.position.x - (sprite.BackBubble.contentSize.width/2),
sprite.BackBubble.position.y - (sprite.BackBubble.contentSize.height/2),
sprite.BackBubble.contentSize.width,
sprite.BackBubble.contentSize.height);
//use the rect and touch location to determine hit
if(CGRectContainsPoint(targetRect, location))
//this doesn't work possibly because Bubble class is a CClayer?
//if(CGRectContainsPoint([sprite boundingBox], location))
{
selectedSprite = sprite;
NSLog(@"touches bubble sprite");
}
}
你的代码有点难读,但你的直接问题是为什么触摸不起作用,因为你假设你的层(气泡)有一个内容宽度和高度。如果您没有设置此选项,它就不会有此选项,这就是为什么
[sprite boundingBox]
的注释行不起作用的原因。请尝试[sprite.BackBubble boundingBox]
。向图层添加项目不会自动调整该图层的内容大小
您还可以尝试添加以下内容:
location = [sprite convertToNodeSpace:location];
如果气泡层或后气泡在任意点移动,那么简单地将后气泡的位置添加到CGRect
可能不起作用。先试试第一个主意,如果不行,就试试这个
希望这对你有帮助,娜塔莉。我看不出你在场景中添加了精灵。[[[CCDirector sharedDirector]openGLView]窗口]添加子视图:];下一步[addTarget:self action:@selector(yourfunction:)forControlEvents:uicontrolEventTouchDragins];泡泡精灵是在init中添加的(对不起,我也会把代码放在那里)谢谢你,艾伦,嗯,对不起,我不明白。。。所以,当我创建“CGRect targetlect”时,它没有给出精灵的宽度和高度吗?(我是新来的,所以我只是想了解这一切是如何工作的,我尝试过边界框解决方案-但不幸的是,这不起作用(泡泡*)层也处理自己的触摸(拖动但不碰撞)-但这是我第一次制作自己的sprite类来进行自己的触摸检测(在游戏层,我试图让碰撞与另一个类一起工作)-因此,关于如何正确使用它的一些初期问题抱歉,我正在谈论
气泡
实例。您在代码中的注释会询问气泡的边界框是否因为它是CCLayer
而不起作用。当您向其添加对象时,层不会设置其宽度和高度,除非您特别为该层指定宽度和高度d高度。这意味着您的[sprite boundingBox]
将无法工作,因为对象是气泡(声明为层),将不会有内容大小。此外,在修改后的代码中,由于未使用原始触摸位置,因此未获得正确的结果。每次迭代时,您都会将一个值转换为一个气泡的空间,并尝试将其转换为另一个气泡的空间,而不是在每次计算中使用原始触摸位置。尝试类似于CGPoint localLocation=[sprite convertToNodeSpace:location];
的方法,并在CGRectContainsPoint()中使用它
call。还有一件事。我建议你做一个或另一个,而不是两个。在你的操作代码中,你手动创建了一个CGRect来测试,这将需要将触摸位置转换为节点空间。如果你只是使用气泡背面的边界框属性,你应该可以,因为这会在内部进行父级转换。你可以应该能够拿出节点空间转换方法,我认为它会起作用。如果不行,你可以试着调试代码,看看触摸位置和矩形的值是多少,看看是否有什么奇怪的地方。你好,艾伦,谢谢你的想法!我已经将Bubble类从CCLayer更改为CCSprite(为了让内容大小和触感在游戏层注册)也尝试了你的建议,它们听起来不错,我只知道我可能对我的类的设置做了一些奇怪的事情。我会更新代码,也许你会看到错误
#import "Bubble.h"
#import "Common.h"
#define ButtonFlashTime .4
#define KBubbleColourTurqoiseBlueFlash 2323
#define ScrollSpeed 5.2f
#define DecoyTextY 5
#define DecoyTextX -2
@implementation Bubble
@synthesize BackBubble,FrontShine,BubbleLabel,startX,startY,currentY,currentX,isNotTouchActivated,myInt,bubblespeed,tagNumber; //isTouched
-(id)init
{
self=[super init];
{
//touches
[[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
isNotTouchActivated = false;
isTouched = NO;
bubblespeed = 0;
//start scrolling
[self MoveWithoutProblem];
[self setAnchorPoint:ccp(0.5,0.5)];
BackBubble = [CCSprite spriteWithSpriteFrameName:Bubblepng];
BackBubble.position = ccp(X,Y);
[BackBubble setAnchorPoint:ccp(0,0)];
[self addChild: BackBubble z:5];
NSLog(@"BackBubble, %f %f",BackBubble.position.x,BackBubble.position.y);
//other code here
[self setContentSize:[BackBubble boundingBox].size];
}
return self;
}
-(BOOL) isTouchOnSprite:(CGPoint)touch{
CGPoint local = [self convertToNodeSpace:touch];
CGRect r = self.boundingBox;
r.origin = CGPointZero;
Boolean b = CGRectContainsPoint( r, local );
//CCLOG(@"touch %f : %f : %d",touch.x,touch.y,b);
if (b) {
return YES;
}else {
return NO;
}
}
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
if([self isTouchOnSprite:touchPoint]){
//CGPoint move = [self convertTouchToNodeSpace:touch];
isNotTouchActivated = TRUE;
//isTouched = YES;
//NSLog(@"isTouched = %@", self.isTouched ? @"YES" : @"NO");
currentX = touchPoint.x;
currentY = touchPoint.y;
self.position = touchPoint;
return YES;
}
// NSLog(@"isTouched = %@", self.isTouched ? @"YES" : @"NO");
return NO;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{
CGPoint touchPoint = [touch locationInView:[touch view]];
touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
if (isNotTouchActivated) {
currentX = touchPoint.x;
currentY = touchPoint.y;
self.position = touchPoint;
}
}
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
isNotTouchActivated = FALSE;
//isTouched = NO;
}
-(void)MoveWithoutProblem{
CGSize size = [[CCDirector sharedDirector] winSize];
int MaxHeightofBubbles = 350;
int minHeightofBubbles = 150;
int RandomNumber = [self generateRandomNumberBetweenMin:minHeightofBubbles Max:MaxHeightofBubbles];
float ConvertedRandom = [[NSNumber numberWithInt: RandomNumber] floatValue];
int MaxWidthofBubbles = 0;
int minWidthofBubbles = 900;
int RandomNumber02 = [self generateRandomNumberBetweenMin:MaxWidthofBubbles Max:minWidthofBubbles];
float ConvertedRandom02 = [[NSNumber numberWithInt: RandomNumber02] floatValue];
startX = ConvertedRandom02;
startY = ConvertedRandom;
currentX = startX+myInt;
currentY = startY;
self.position = ccp(startX,startY);
[self schedule:@selector(startMoving)];
}
-(void)startMoving{
if (!isNotTouchActivated) {
currentX+=bubblespeed;
[self setPosition:ccp(currentX,currentY)];
}
if (self.position.x >= 1024+50) {
//NSLog(@"off screen");
isrestartscrolling = YES;
}
if (isrestartscrolling == YES) {
//[self RandomYPOs];
[self scheduleOnce:@selector(newRandomX) delay:0.2];
isrestartscrolling = NO;
}
}
@end
location = [sprite convertToNodeSpace:location];