Ios 精灵阵列对触摸移动没有反应
我正在实现一种移动精灵的方法,我把一个数组放在我放置所有精灵的地方,我想给这个数组一个触摸移动的动作。但当我运行它时,什么都不会发生:Ios 精灵阵列对触摸移动没有反应,ios,xcode,ipad,cocos2d-iphone,Ios,Xcode,Ipad,Cocos2d Iphone,我正在实现一种移动精灵的方法,我把一个数组放在我放置所有精灵的地方,我想给这个数组一个触摸移动的动作。但当我运行它时,什么都不会发生: -(void)onEnter { [super onEnter]; self.isTouchEnabled = YES; } - (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *myTouch = [touches anyObject
-(void)onEnter
{
[super onEnter];
self.isTouchEnabled = YES;
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
mSpriteOnHand = nil;
for(CCSprite *cloth in mSpriteArray)
{
if(CGRectContainsPoint([cloth boundingBox], location))
{
mSpriteOnHand = cloth;
break;
}
}
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
if(mSpriteOnHand)
mSpriteOnHand.position = location;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
mSpriteOnHand = nil;
}
-(void)onExit
{
[mSpriteArray release];
mSpriteArray = nil;
[super onExit];
}
有什么不对劲吗?我想问题是从触摸开始的,但我不确定
编辑:
我的精灵没有反应,但方法似乎还可以。这是我的建议:
-(id) init
{
if( (self=[super init]) )
{
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite * backGround = [CCSprite spriteWithFile:@"background_ipad.png"];
backGround.position = ccp(winSize.width/2, winSize.height/2);
[self addChild:backGround z:0];
cloth1 = [CCSprite spriteWithFile:@"clothe_1.png"];
cloth1.position = ccp(380, 500);
[self addChild:cloth1 z:1];
cloth2 = [CCSprite spriteWithFile:@"clothe_2.png"];
cloth2.position = ccp(530, 500);
[self addChild:cloth2 z:2];
cloth3 = [CCSprite spriteWithFile:@"clothe_3.png"];
cloth3.position = ccp(700, 500);
[self addChild:cloth3 z:3];
cloth4 = [CCSprite spriteWithFile:@"clothe_4.png"];
cloth4.position = ccp(380, 270);
[self addChild:cloth4 z:4];
cloth5 = [CCSprite spriteWithFile:@"clothe_5.png"];
cloth5.position = ccp(530, 270);
[self addChild:cloth5 z:5];
cloth6 = [CCSprite spriteWithFile:@"clothe_6.png"];
cloth6.position = ccp(700, 270);
[self addChild:cloth6 z:6];
[mSpriteArray addObject: cloth1];
[mSpriteArray addObject: cloth2];
[mSpriteArray addObject: cloth3];
[mSpriteArray addObject: cloth4];
[mSpriteArray addObject: cloth5];
[mSpriteArray addObject: cloth6];
}
return self;
}
如果这是从CCSprite派生的,它将不起作用。在这种情况下,请使用:
-(void) onEnter {
[[CCDirector sharedDirector].touchDispatcher addStandardDelegate:self priority:0];
[super onEnter];
}
-(void) onExit {
[[CCDirector sharedDirector].touchDispatcher removeDelegate:self];
[super onExit];
}
编辑1:
CGPoint location = [touch locationInView:[touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
MPLOG(@"Touch at location %@", NSStringFromCGPoint(convertedLocation));
编辑2:
在init中:
mSpriteArray = [[NSMutableArray array] retain]; // without ARC
在dealloc中:
-(void) dealloc {
[mSpritesArray release];
[super dealloc];
}
我没有足够的“ARC”悟性提出任何建议。此外,根据“self”是什么,即您在其中有哪些子节点,您可能不需要阵列,因为每个CCNode都已经有一个。详情如下:
for (CCSprite* cloth in self.children) {
if(CGRectContainsPoint([cloth boundingBox], location))
{
mSpriteOnHand = cloth;
break;
}
}
编辑3:排除背景:
iVar:
int tag:UNIQUE_TAG_FOR_BACKGROUND;
在init中:
UNIQUE_TAG_FOR_BACKGROUND = 4367; // pick a number
[self addChild:backGround z:0 tag:UNIQUE_TAG_FOR_BACKGROUND];
循环中:
for (CCSprite* cloth in self.children) {
if(cloth.tag == UNIQUE_TAG_FOR_BACKGROUND) continue;
if(CGRectContainsPoint([cloth boundingBox], location))
{
mSpriteOnHand = cloth;
break;
}
}
如果这是从CCSprite派生的,它将不起作用。在这种情况下,请使用:
-(void) onEnter {
[[CCDirector sharedDirector].touchDispatcher addStandardDelegate:self priority:0];
[super onEnter];
}
-(void) onExit {
[[CCDirector sharedDirector].touchDispatcher removeDelegate:self];
[super onExit];
}
编辑1:
CGPoint location = [touch locationInView:[touch view]];
CGPoint convertedLocation = [[CCDirector sharedDirector] convertToGL:location];
MPLOG(@"Touch at location %@", NSStringFromCGPoint(convertedLocation));
编辑2:
在init中:
mSpriteArray = [[NSMutableArray array] retain]; // without ARC
在dealloc中:
-(void) dealloc {
[mSpritesArray release];
[super dealloc];
}
我没有足够的“ARC”悟性提出任何建议。此外,根据“self”是什么,即您在其中有哪些子节点,您可能不需要阵列,因为每个CCNode都已经有一个。详情如下:
for (CCSprite* cloth in self.children) {
if(CGRectContainsPoint([cloth boundingBox], location))
{
mSpriteOnHand = cloth;
break;
}
}
编辑3:排除背景:
iVar:
int tag:UNIQUE_TAG_FOR_BACKGROUND;
在init中:
UNIQUE_TAG_FOR_BACKGROUND = 4367; // pick a number
[self addChild:backGround z:0 tag:UNIQUE_TAG_FOR_BACKGROUND];
循环中:
for (CCSprite* cloth in self.children) {
if(cloth.tag == UNIQUE_TAG_FOR_BACKGROUND) continue;
if(CGRectContainsPoint([cloth boundingBox], location))
{
mSpriteOnHand = cloth;
break;
}
}
您测试过触摸事件是否触发了吗?触摸和精灵坐标是为同一个节点空间计算的吗?我测试过,但没有。我不知道,如何检查?如果触摸事件根本没有触发,需要检查的可能是,我们是否确定节点已添加到场景中,节点是否为CCLayer(其他节点不能通过简单设置isTouchEnabled启用触摸),在这个节点有机会响应触摸之前,我们确定没有其他节点吞下触摸或类似的东西吗?我正在检查,但根本没有。。。如果你需要更多的代码,问问它。你测试过触摸事件是否触发了吗?触摸和精灵坐标是为同一个节点空间计算的吗?我测试过,但没有。我不知道,如何检查?如果触摸事件根本没有触发,需要检查的可能是,我们是否确定节点已添加到场景中,节点是否为CCLayer(其他节点不能通过简单设置isTouchEnabled启用触摸),在这个节点有机会响应触摸之前,我们确定没有其他节点吞下触摸或类似的东西吗?我正在检查,但根本没有。。。如果您需要更多的代码,只需询问它。不,仍然没有发生任何事情。日志记录是您的朋友:首先确认您正在获得CCTouchesBegind事件。如果是,则计算出几何图形。我不确定你在ccToucheMoved中的“位置”是否能满足你的需求。查看我的编辑。我放了一些日志,发现开始被识别,然后,当我单击精灵并移动它时,它识别移动!但是精灵没有响应…我刚刚编辑并添加了我的INIT方法,可能有错误。我没有看到mSpriteArray正在初始化。没有,仍然没有发生任何事情。日志记录是您的朋友:首先确认您正在获取CCTouchesBegined事件。如果是,则计算出几何图形。我不确定你在ccToucheMoved中的“位置”是否能满足你的需求。查看我的编辑。我放了一些日志,发现开始被识别,然后,当我单击精灵并移动它时,它识别移动!但是精灵没有响应…我刚刚编辑并添加了我的INIT方法,可能有错误。我没有看到mSpriteArray被初始化。