Closed. 这个问题不符合堆栈溢出指南 /help/closed-questions 。目前不接受答案。
我的场景中有 7 个精灵。所有精灵都添加到可变数组中。当我触摸一个精灵来移动时,其他精灵在我触摸移动方法后不可见
这是我的代码
if( (self=[super init])) {
sprites=[[NSMutableArray alloc]init];
CCLayer *base=[CCSprite spriteWithFile:@"Base.png"];
base.position=ccp(512,384);
[self addChild:base];
x=0;
for(int i=1;i<=7;i++)
{
CCSprite *hole=[CCSprite spriteWithFile:@"ball.png"];
hole.position=ccp(140+x,318);
hole.tag=i;
[self addChild:hole];
hole.visible=YES;
[sprites addObject:hole];
x=x+75;
}
self.isTouchEnabled=YES;
}
return self;
}
My touchesmove method:
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"count:%i",[sprites count]);
UITouch *touch=[touches anyObject];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
location=[self convertToNodeSpace:location];
for(CCSprite *s in sprites)
{
s.position=ccp(location.x,location.y);
}
}
ccTouchesMoved 中的代码将所有精灵移动到一个触摸位置,因此您只能看到一个精灵,而其余精灵实际上堆叠在下面。
如果您想要实现的是在触摸时简单地拖动精灵,则需要在 ccTouchBegan 中测试触摸位置和每个精灵的边界框之间的交集。一旦您循环并找到了您触摸下的精灵,您就可以保存对它的引用,并且在 ccTouchMoved 中,您可以翻译该精灵的位置以及自上次调用 ccTouchMoved 以来移动的量。
查看 Ray Wenderlich 的教程:http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d http://www.raywenderlich.com/2343/how-to-drag-and-drop-sprites-with-cocos2d
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)