Cocos2d iphone 触摸CCArray中的特定CCSprite
我在编码时遇到了问题,无法在数组中移动特定的精灵Cocos2d iphone 触摸CCArray中的特定CCSprite,cocos2d-iphone,Cocos2d Iphone,我在编码时遇到了问题,无法在数组中移动特定的精灵 -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch* myTouch = [touches anyObject]; CGPoint location = [myTouch locationInView: [myTouch view]]; location = [[CCDirector sharedDirect
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView: [myTouch view]];
location = [[CCDirector sharedDirector]convertToGL:location];
CCSprite*grade=[grades lastObject];
[grade runAction:[CCMoveTo actionWithDuration:3 position:location]];
}
数组移动的最后一个对象
你能帮我更正代码,让它在数组中触摸特定的精灵,并用ccTouchesMove方法移动它吗
另外,我只使用cocos2d,不使用box2d
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch* myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView: [myTouch view]];
location = [[CCDirector sharedDirector]convertToGL:location];
for (CCSprite *grade in grades) {
if (CGRectContainsPoint(grade.boundingBox, location)) {
[grade runAction:[CCMoveTo actionWithDuration:3 position:location]];
}
}
}
CGRectContainsPoint将检查所接触的位置是否在给定的rect内。所以我所做的就是迭代你的精灵数组,对于位置在矩形边框内的精灵,移动那个精灵。如果只想移动第一个被触摸的精灵,可以在if语句中添加一个中断