strange problem playing cocos2d animation from another class?

随声附和 提交于 2019-12-13 20:22:23

问题


strange thing. if someone could please,please help me. its 3 days, and i think i am going to be fired :(

i have cocos2d class with animation function,and another xcode class. if i call from the cocos2d class init function to animation function, the animation is being played when app is starts.

if i call from another class to the cocos2d class-animation, so it does the init function and enter the animation,but i cant see it playing.

so the animation is working only if called from within the cocos class only. WHY ?

this is how i call the animation :

ran=[[HelloWorld alloc] init];
    [ran animation];

this is the animation:

-(void)animation
{
    //[self removeChild:background cleanup:YES];
    //[b_pic.parent removeChild:b_pic cleanup:YES];



    //animation
    CCSpriteBatchNode *danceSheet = [ CCSpriteBatchNode batchNodeWithFile:@"head.png"]; 
    [self addChild:danceSheet];

    CCSprite *danceSprite = [CCSprite spriteWithTexture:danceSheet.texture rect:CGRectMake(0, 0, 480, 320)];
    [danceSheet addChild:danceSprite];
    //danceSprite.anchorPoint=CGPointMake(0, 0);

    CGSize s = [[CCDirector sharedDirector] winSize];
    danceSprite.position = ccp(s.width/2,s.height/2);

    CCAnimation *danceAnimation = [CCAnimation animation];
    [danceAnimation setDelay:0.1f];

    int frameCount = 0;
    for (int y = 0; y < 4; y++) 
    {
        NSLog(@"%@",animation);
        for (int x = 0; x < 5; x++) 
        {
            CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:danceSheet.texture rect:CGRectMake(x*320,y*440,320,440)];
            [danceAnimation addFrame:frame];

            frameCount++;

            if (frameCount == 25)
                break;
        }
    }

回答1:


Can you call the animation function twice within that same class...Try

[self animation]; 

and

[[CCScheduler sharedScheduler] scheduleSelector:@selector(animation) forTarget:self 
interval:10 paused:NO]; 

and see if the animation gets called the second time.

If it doesn't which at this point I don't think it will, you should define all that you can in the init and then only run the pieces you need in the animation method (i.e. add the batchNode and Sprite in the init as ivars and reuse them in the animation).




回答2:


solved , and i dont know why.

but if you want to call a cocos2d function from the outside, you have to do :

[(HelloWorld*)[[[CCDirector sharedDirector] runningScene] getChildByTag:42] HardwareEvent:DollPart];

and tag your layer like :

 layer.tag=42;

on the scene method !

thats the only way it works great.



来源:https://stackoverflow.com/questions/6710389/strange-problem-playing-cocos2d-animation-from-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!