0
이이 사용자 정의 스프라이트 클래스 이름 배경두 개의 사용자 정의 스프라이트를 장면에 추가하면 왜 이러한 스프라이트의 메소드가 서로 영향을 미치며 누구나 내 코드에서 실수를 찾을 수 있습니까? 덕분에
#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
내가 시작하거나 내가 몇 번 시도 후 터치 끝, spriteA는, 이동이 중지됩니다 터치 할 때, 내가 발견
#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
이 장면 클래스 코드 bgWhenRun
및 bgWhenWalk
이라는 메서드에서 [self stopAllActions]
을 사용하면 spriteA와 spriteB가 서로 영향을 줄 수 있습니다. 누구든지 코드에서 실수를 발견하면 나에게 말해 줄 수 있습니까? 나는 여러 번 시도했지만 지금은 전혀 모른다. 고맙습니다!
감사의 문제가 해결책으로 해결되었지만 여전히 질문을 이해할 수 없습니다. 두 스프라이트 모두 'id move02'와 'double roadX'를 사용하지만 변수는 항상 존재합니다. '[spriteA stopAllActions]'일 때 spriteB는 move02의 액션을 멈추지 만 괜찮습니다.하지만 move02는 여전히 존재하며 변경되지 않았습니다. 왜 spriteA의 액션에 영향을 줍니까? –
공유 변수를 사용하면 스프라이트가 서로 영향을줍니다. 마지막으로 초기화 된 값이 포함됩니다. 따라서 하나의 스프라이트에서는 효과가 있고 다른 스프라이트에서는 그렇지 않습니다. – bunty
감사합니다. –