0

我有 6 个想要移动的精灵:

-(id)初始化{

if( (self=[super init]) )
{
    CGSize winSize = [[CCDirector sharedDirector] winSize];

    CCSprite * backGround = [CCSprite spriteWithFile:@"background_ipad.png"];
    backGround.position = ccp(winSize.width/2, winSize.height/2);
    [self addChild:backGround z:0];

    cloth1 = [CCSprite spriteWithFile:@"clothe_1.png"];
    cloth1.position = ccp(-200, -15);
    [self addChild:cloth1 z:1];
    cloth2 = [CCSprite spriteWithFile:@"clothe_2.png"];
    cloth2.position = ccp(130, 225);
    [self addChild:cloth2 z:2];

    cloth3 = [CCSprite spriteWithFile:@"clothe_3.png"];
    cloth3.position = ccp(365, 225);
    [self addChild:cloth3 z:3];

    cloth4 = [CCSprite spriteWithFile:@"clothe_4.png"];
    cloth4.position = ccp(-110, -15);
    [self addChild:cloth4 z:4];

    cloth5 = [CCSprite spriteWithFile:@"clothe_5.png"];
    cloth5.position = ccp(130, -20);
    [self addChild:cloth5 z:5];

    cloth6 = [CCSprite spriteWithFile:@"clothe_6.png"];
    cloth6.position = ccp(365, -15);
    [self addChild:cloth6 z:6];

}
return self;
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

}

以及这种移动方法:

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: location];
    //Sprite follows the finger movement.
    [cloth1 setPosition:location];
}

问题是,我想在那里添加更多精灵来移动精灵。我尝试在 //follow the finger Movement 中添加更多精灵,但随后所有精灵都跟随手指移动。我想移动一个精灵。例如:接触布料1时,移动布料1;接触布料2时,移动布料2;但不是同时。

有人可以告诉我该怎么做吗?

4

2 回答 2

1
@interface YourClass : NSObject
{
    NSMutableArray  *mSpriteArray;
    CCSprite  *mSpriteOnHand;
}

//在实现中:

-(id) init 
{
  .. //your old code
  ..

   [mSpriteArray addObject: cloth1];
   [mSpriteArray addObject: cloth2];
   [mSpriteArray addObject: cloth3];
   [mSpriteArray addObject: cloth4];
   [mSpriteArray addObject: cloth5];
   [mSpriteArray addObject: cloth6];

}

-(void)onEnter
{
    [super onEnter];
    self.touchEnabled = YES; //    self.isTouchEnabled = YES;

}


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

    mSpriteOnHand = nil;

    for(CCSprite *cloth in mSpriteArray)
    {
        if(CGRectContainsPoint([cloth boundingBox], location))
        {
            mSpriteOnHand = cloth;
            break;
        }
    }
}


- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    if(mSpriteOnHand)
       mSpriteOnHand.position = location;

 }

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    mSpriteOnHand = nil;
}

-(void)onExit
{
   [mSpriteArray release]; 
   mSpriteArray = nil;
   [super onExit];
}
于 2013-02-21T15:53:28.120 回答
0

return语句总是终止函数并将控制权返回给调用函数。你写了:

 return self;
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

[[CCTouchDispatcher sharedDispatcher]...行将永远不会被执行。

你的类是 的子类CCLayer吗?如果您将图层的isTouchEnabled属性设置为YES,它将将此图层添加为标准(非目标)触摸委托。

如果您必须在您的层中使用目标触摸,如果您声明此触摸,则您应该返回YES,如果您没有声明。已声明触摸的更新仅发送给声明它的委托。-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)eventNO

要确定哪个精灵被触摸:创建一个实例变量来存储“选定”精灵,在touchBegan方法中检查哪个精灵的边界框包含触摸位置并将这个精灵存储在实例变量中(另外,看起来你只想在触摸时声明触摸一个精灵)。

[cloth1 setPosition:location];

-- 你的精灵会“跳”到触摸位置。通常看起来不太好。我会得到 touch's locationInView:and previousLocationInView:,将它们转换为 GL,得到差异并通过该差异改变精灵的位置。

于 2013-02-21T15:46:25.137 回答