Search code examples
iosobjective-ccocos2d-iphone

add two custom sprites into a scene,why do these sprites' methods will effect each other,anyone can find the mistake in my code? thanks


this is custom sprite class named BackGround

#import "BackGround.h"

// -----------------------------------------------------------------
id move02;
double roadX;
@implementation BackGround


+ (instancetype)initWithPicture: (NSString *) pic
{
    return [[self alloc] init:pic];
}

-(id) init: (NSString *) pic
{
    if(self = [super init])
    {
        CCSpriteFrameCache* spriteFrameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
        CCSpriteFrame * bgSpriteFrame = [spriteFrameCache spriteFrameByName:pic];
        self = [BackGround spriteWithSpriteFrame:bgSpriteFrame];
        self.anchorPoint = ccp(0, 0);
        roadX = -(self.contentSize.width-1)*2;
        self.position = ccp((-roadX/2), 0);
        id move01 = [CCActionMoveBy actionWithDuration:10.0f position:ccp(roadX,0.0)];
        move02 = [CCActionRepeatForever actionWithAction:move01];
        [self runAction:move02];
    }
    return self;
}

-(void)bgWhenRun
{
    [self stopAllActions];
    id move = [CCActionSpeed actionWithAction:move02 speed:2];
    [self runAction:move];
}

-(void)bgWhenWalk
{    
    [self stopAllActions];
    [self runAction:move02];
}
// -----------------------------------------------------------------

@end

this is scene class code

    #import "_256Deathes.h"
    #import "IntroScene.h"
    #import "BackGround.h"
    #import "cocos2d.h"
    #import "Person.h"
    // -----------------------------------------------------------------
    Person * personA;

    @implementation _256Deathes
    {
    }
    - (instancetype)init
    {
            if ((self = [super init]))
            {
              NSAssert(self, @"Whoops");
                self.userInteractionEnabled = YES;
                CCSpriteFrameCache* spriteFrameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
                [spriteFrameCache addSpriteFramesWithFile:@"256Deathes.plist"];
                BackGround * bgSprite01 = [BackGround initWithPicture:@"earthA.png"];
                bgSprite01.position = ccp(0, 0);
                [self addChild:bgSprite01 z:0 name:@"bgSpriteA"];
                BackGround * bgSprite02 = [BackGround initWithPicture:@"earthA.png"];
                [self addChild:bgSprite02 z:1 name:@"bgSpriteB"];
                        }
        return self;
    }

- (void)onEnter
{
    // always call super onEnter first
    [super onEnter];
    [self schedule:@selector(updateSprite) interval:0.02];
}

    -(void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event
    {
                    BackGround *spriteA = (BackGround *)[self getChildByName:@"bgSpriteA" recursively:NO];
                    BackGround *spriteB = (BackGround *)[self getChildByName:@"bgSpriteB" recursively:NO];
                    [spriteA bgWhenRun];
                    [spriteB bgWhenRun];
    }

    -(void)touchEnded:(CCTouch *)touch withEvent:(CCTouchEvent *)event
    {
            BackGround *sprite;
            for(sprite in [self children])
            {
                if([sprite.name  containsString:@"bgSprite"])
                {
                    [sprite bgWhenWalk];
                }
            }
    }

    -(void) updateSprite
    {
        [self updateBackGround01];
    }

    -(void) updateBackGround01
    {
        BackGround *sprite;
        for(sprite in [self children])
        {
            if([sprite.name  containsString:@"bgSprite"])
            {
                double nextX = sprite.contentSize.width-3;
                if(sprite.position.x <= (-nextX))
                {
                    sprite.position = ccp(nextX, 0);
                }
            }
        }

    }
    // -----------------------------------------------------------------

    @end

when i touch begin or touch end, spriteA will stop moving, after i tried some times, i found [self stopAllActions] in methods named bgWhenRun and bgWhenWalk can make spriteA and spriteB effect each other. anyone can find out the mistakes in the code then tell me?i have tried many times,now i really have no idea. thank you!


Solution

  • These two sprites effect each other because both are using same instance of variables id move02 and double roadX as being global ones. Declare them within scope of BackGround class i.e. as member variables of that class.