0

我在编码时遇到问题,使特定精灵在数组中移动....

-(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方法中对其进行移动吗?

PS我只使用cocos2d,而不是box2d。

4

1 回答 1

0
-(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将检查触摸的位置是否在给定的矩形内。所以我所做的是迭代你的精灵数组和位置在其矩形(边界框)内的精灵,而不是移动那个精灵。if如果您只想移动第一个触摸的精灵,您可以在语句中添加一个中断。

于 2012-06-18T08:46:08.320 回答