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

时间:2016-12-26 11:32:08

标签: ios objective-c cocos2d-iphone

这是名为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

这是场景类代码

    #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

当我触摸开始或触摸结束时,spriteA将停止移动,在我尝试了几次之后,我发现[self stopAllActions]方法bgWhenRunbgWhenWalk可以使每个方法都有spriteA和spriteB效果其他。 任何人都可以找出代码中的错误然后告诉我?我已经尝试了很多次,现在我真的不知道。谢谢!

1 个答案:

答案 0 :(得分:0)

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