shrimp-hamburger 2013-04-10 07:47 采纳率: 0%
浏览 1320

使用计时器变换ccsprite图片

一个图片有10个sprite图片,这些图片不是固定的,我想用计时器实现sprite中两个子图片的变换,下面的代码没实现成功:

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

运作之后,只有一个图片在连续显示。没发生改变。

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2022-12-20 19:28
    关注

    ChatGPT尝试为您解答,仅供参考
    看起来你的代码尝试使用延迟和调用块动作在精灵中循环切换两个子图像。但是,您的代码中有一些问题。


    首先,你的代码将使用的延迟时间错误地设置为1秒,但是您希望在这之间切换子图像。你应该使用更短的延迟时间来实现这个目标,例如0.5秒。


    其次,你的代码尝试使用CCCallBlock动作在精灵中循环切换子图像。这个动作只会在它被调用一次时执行一次,然后就会停止。你应该使用一个不同的动作,例如CCRepeatForever,来循环执行切换子图像的操作。


    为了解决这个问题,你可以修改你的代码,如下所示:

    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;
    
    // 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:)];
    
    // Create the delay action
    id delayTime = [CCDelayTime actionWithDuration:0.5f];
    
    // Create the change texture action
    id changeTexture1 = [CCCallBlock actionWithBlock:^{
        // Set the first texture
        [target setTexture:[[CCSprite spriteWithFile:@"image1.png"] texture]];
    }];
    id changeTexture2 = [CCCallBlock actionWithBlock:^{
        // Set the second texture
        [target setTexture:[[CCSprite spriteWithFile:@"image2.png"] texture]];
    }];
    
    // Create the sequence action to switch between textures
    id textureSequence = [CCSequence actions:changeTexture1, delayTime, changeTexture2, delayTime, nil];
    
    // Create the repeat forever action to continuously switch between textures
    id textureRepeat = [CCRepeatForever actionWithAction:textureSequence];
    
    // Run the actions
    [target runAction:[CCSequence actions:textureRepeat, actionMove, actionMoveDone, nil]];
    

    这样,你的代码将创建一个序列动作来循环切换纹理,然后使用CCRepeatForever动作来无限循环这个序列动作。这样,精灵就会在右侧显示并移动到左侧,同时循环切换子图像。

    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗