【问题标题】:how to change image of moving ccsprite using timer如何使用计时器更改移动 ccsprite 的图像
【发布时间】:2013-04-03 08:16:57
【问题描述】:

我有大约 10 个相同图像的精灵并且这些精灵正在移动,我想通过使用计时器交替更改所有精灵的图像(2 图像)。 我正在使用以下代码,但它不适合我

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

但是通过使用此代码,只有一张图像会持续显示在 sprite 上。图像没有变化。

【问题讨论】:

  • 你必须制作spritesheet才能不断改变图像
  • 我想在不使用精灵表的情况下拥有此功能

标签: iphone ios cocos2d-iphone ccsprite


【解决方案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"]];

【讨论】:

    猜你喜欢
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多