简体   繁体   English

在场景中添加两个自定义精灵,为什么这些精灵的方法会互相影响,谁能在我的代码中发现错误? 谢谢

[英]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 这是名为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. 当我触摸开始或触摸结束时,spriteA将停止移动,经过几次尝试,我发现名为bgWhenRunbgWhenWalk方法[self stopAllActions]可以使spriteA和spriteB相互影响。 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! 谢谢!

These two sprites effect each other because both are using same instance of variables id move02 and double roadX as being global ones. 这两个精灵相互影响,因为它们都使用变量id move02double roadX相同实例作为全局变量。 Declare them within scope of BackGround class ie as member variables of that class. BackGround类的范围内声明它们,即作为该类的成员变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM