将两个自定义精灵添加到场景中,为什么这些精灵的方法会相互影响,任何人都可以在我的代码中找到错误吗?谢谢

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

这是名为 BackGround

的自定义精灵 class
#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

这是场景class代码

    #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

当我触摸begin或touch end时,spriteA会停止移动,尝试了几次后,我发现[self stopAllActions]在名为bgWhenRunbgWhenWalk的方法中可以使spriteA和spriteB相互影响。 任何人都可以找出代码中的错误然后告诉我吗?我已经尝试了很多次,现在我真的不知道了。谢谢!

这两个精灵相互影响,因为它们都使用相同的变量实例 id move02double roadX 作为全局变量。在 BackGround class 范围内声明它们,即作为 class.

的成员变量