2013-04-03 24 views
1

我有相同圖像的10個精靈,這些正在移動,我想改變所有精靈的圖像(2圖像)交替使用計時器。 我使用下面的代碼,但它不是爲我工作如何改變使用定時器移動ccsprite的圖像

CCSprite *target = [CCSprite spriteWithFile:@"images1.png" rect:CGRectMake(0, 0, 120, 140)]; 
    // Determine where to spawn the target along the Y axis 

winSize = [[CCDirector sharedDirector] winSize]; 
int minY = target.contentSize.height/2; 
int maxY = (winSize.height/2) - target.contentSize.height/2; 
int rangeY = maxY - minY; 
int actualY = (arc4random() % rangeY) ; 

// Create the target slightly off-screen along the right edge, 
// and along a random position along the Y axis as calculated above 

target.position = ccp(winSize.width + (target.contentSize.width/2), actualY); 
[self addChild:target]; 

// Determine speed of the target 

int minDuration = 2.0; 
int maxDuration = 4.0; 
int rangeDuration = maxDuration - minDuration; 
int actualDuration = (arc4random() % rangeDuration) + minDuration; 


id delayTime1 = [CCDelayTime actionWithDuration:1.0f]; 
id calFun1 = [CCCallBlock actionWithBlock:^{ 
    //HERE SET BLUE TEXTURE.. 
    [target setTexture:[[CCSprite spriteWithFile:@"image1.png"]texture]]; 

}]; 
id delayTime2 = [CCDelayTime actionWithDuration:1.0f]; 
id calFun2 = [CCCallBlock actionWithBlock:^{ 
    //HERE SET RED TEXTURE.. 
    [target setTexture:[[CCSprite spriteWithFile:@"image2.png"]texture]]; 

}]; 

// Create the actions 

id actionMove = [CCMoveTo actionWithDuration:actualDuration*2.5 position:ccp(-target.contentSize.width/2, actualY)]; 
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)]; 
id sequece = [CCSequence actions:delayTime1, calFun1, delayTime2, calFun2,actionMove, actionMoveDone, nil]; 
id repeate = [CCRepeatForever actionWithAction:sequece]; 

[target runAction:repeate]; 

但是,通過使用此代碼只有一個圖像子畫面顯示不斷。圖像沒有改變。

+0

你必須做出spritesheet改變形象不斷 –

+0

我想有這個功能,而無需使用精靈表 – KsK

回答

1

試試這個

-(CCSpriteFrame *)getImageWithName:(NSString *)image{ 
    CCSprite *sprite=[CCSprite spriteWithFile:image]; 
    CCSpriteFrame *frame=[CCSpriteFrame frameWithTexture:sprite.texture rect:CGRectMake(0, 0, sprite.contentSize.width, sprite.contentSize.height)]; 
    return frame; 
} 

然後

[target setDisplayFrame:[self getImageWithName:@"image1"]]; 

[target setDisplayFrame:[self getImageWithName:@"image2"]];