在cocos2d中移动精灵

时间:2013-02-21 15:24:53

标签: ios xcode ipad cocos2d-iphone

我想要移动6个精灵:

- (id)init {

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];
}

事情就是这样,我想在那里添加更多的精灵来移动精灵。我在跟随手指移动的过程中添加了更多的精灵,但随后所有精灵都跟随手指运动。我想移动一个精灵。例如:触摸布料1时,移动布料1;触摸布料2时,移动布料2;但不是两个在同一时间。

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

2 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

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

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

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

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

如果您必须在图层中使用有针对性的触摸,则应在YES中返回-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event,如果您要声明此触摸,则NO如果不是,则返回touchBegan。声称触摸的更新仅发送给声明它的代表。

要确定触摸了哪个sprite:创建一个实例变量来存储“selected”sprite,在[cloth1 setPosition:location]; 方法中检查哪个sprite的边界框包含触摸位置并将此sprite存储在实例变量中(同样,看起来像你想要的只有触摸精灵才能声称触摸。

locationInView:

- 你的精灵将“跳”到触摸位置。通常看起来不太好看。我会触摸previousLocationInView:和{{1}},将它们转换为GL,获得差异并根据差异改变精灵的位置。