【问题标题】:Moving sprite from a group of sprites从一组精灵中移动精灵
【发布时间】:2023-03-15 01:00:02
【问题描述】:

我的图层中有 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;

 }
}

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

【问题讨论】:

  • 触摸结束时,您将所有 (!) 精灵设置到同一位置。

标签: ios xcode cocos2d-iphone uitouch ccsprite


【解决方案1】:
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;
}

我没有测试代码,所以我不确定它是否可以通过复制和粘贴来工作,但我认为这会给你一个正确的想法。显然还有其他方法可以做到这一点,但我认为如果你要对被触摸的精灵做一堆事情,这是最简单的。

【讨论】:

  • 你在移动时编码对我很有帮助。当我的触摸结束时,所有精灵都在同一个位置。我也在 cctouchesEnd 方法中使用了相同的代码,但没有得到解决方案
  • 好的,在 touches 结束时你到底想做什么?我假设你只是想把你移动到原来位置的精灵移回原来的位置......我会编辑我的答案