-2

我的图层中有 15 个精灵。我将这些精灵添加到可变数组中我想使用移动单个精灵ccTouchesMoved。当触摸结束ccTouchesEnded精灵移回其起点或原点时。

我的编码:

if( (self=[super init])) {

    collection=[[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;
        [collection addObject:hole];
        x=x+75;
    }

    self.isTouchEnabled=YES;

}
return self;
 }
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
location=[self convertToNodeSpace:location];
p=location;


}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"count:%i",[collection count]);
UITouch *touch=[touches anyObject];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];

location=[self convertToNodeSpace:location];


 for(CCSprite *s in collection)
 {
   if(CGRectContainsPoint([s boundingBox], location))
    s.position=ccp(location.x,location.y);

 }
}
 -(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for(CCSprite *s in collection)
{

        s.position=p;

 }
}

当我移动一个精灵时,其他精灵在图层上也可见,不要隐藏。请帮我写代码。

4

1 回答 1

0
for(CCSprite *s in collection)
 {
   if(CGRectContainsPoint([s boundingBox], location))
    s.position=ccp(location.x,location.y);

 }

由于您所有的精灵都在同一个位置,上面的代码正在移动该位置的所有精灵。

for(CCSprite *s in collection)
{
    if(CGRectContainsPoint([s boundingBox], location))
    {
        s.position=ccp(location.x,location.y);
        break;
    }
}

一旦检测到第一个精灵,上面的代码将跳出循环,因此应该只移动数组中的第一个精灵。

不确定这是否正是您想要的,但希望这会有所帮助。

- - 编辑

就我个人而言,我要做的是为精灵创建一个实例变量。

CCSprite * touchedSprite;

并执行以下操作...

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch=[touches anyObject];
    CGPoint location=[touch locationInView:[touch view]];
    location=[[CCDirector sharedDirector]convertToGL:location];
    location=[self convertToNodeSpace:location];

    for((CCSprite * sprite in collection)
    {
        if(CGRectContainsPoint([sprite boundingBox], location))
        {
            touchedSprite = sprite;
            p = sprite.position;
            break;
        }
    }
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"count:%i",[collection count]);
    UITouch *touch=[touches anyObject];
    CGPoint location=[touch locationInView:[touch view]];
    location=[[CCDirector sharedDirector]convertToGL:location];

    location=[self convertToNodeSpace:location];

    touchedSprite.position = location;
}

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    touchedSprite.position = p;
}

i didn't test the code so i am not sure if it will work just by copying and pasting, but i think this will give you the right idea. There are obviously other ways of doing this, but i think its the simplest if you are to do bunch of stuff to the sprite that is being touched.

于 2013-01-30T03:48:52.817 回答