2014-04-18 1 views
0

터치했을 때 이미지를 변경해야합니다.터치했을 때 이미지 변경

현재 변경되는 이미지는 다음 이미지로, 자동으로 생성되지 않습니다.

@implementation MyScene2 
{ 
    Marsugo *marsugo; 
    SKAction *actionMoveDown; 
    SKAction *actionMoveEnded; 
    SKTexture *rescued; 

} 

-(id)initWithSize:(CGSize)size 
{ 
    if (self = [super initWithSize:size]) { 

     // Initializes Background 
     self.currentBackground = [Background generateNewBackground]; 
     [self addChild:self.currentBackground]; 


    } 
    return self; 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    for (UITouch *touch in touches) { 

     NSArray *nodes = [self nodesAtPoint:[touch locationInNode:self]]; 

     for (SKNode *node in nodes) { 
      if ([node.name isEqualToString:playerObject]) { 

       rescued = [SKTexture textureWithImageNamed:@"rescued"]; 
       marsugo.texture = rescued; 

     // this is changing the image of the next marsugo that spawns instead of self.   
      } 
     } 
    } 
} 

-(void)addMarsugos 
{ 
// the marsugo is being initialized inside this method, that might be the issue i believe 
    // Create sprite 
    marsugo = [[Marsugo alloc]init]; 
    marsugo.xScale = 0.3; 
    marsugo.yScale = 0.3; 
    marsugo.zPosition = 75; 

    // Bounds + Spawn Positions 
    int minX = marsugo.size.width; 
    int maxX = self.frame.size.width - marsugo.size.width; 
    int rangeX = maxX - minX; 
    int actualX = (arc4random() % rangeX) + minX; 
    marsugo.position = CGPointMake(actualX, self.frame.size.height + 50); 
    [self addChild:marsugo]; 

    // Spawn Timer 
    int minDuration = 1; 
    int maxDuration = 10; 
    int rangeDuration = maxDuration - minDuration; 
    int actualDuration = (arc4random() % rangeDuration) + minDuration; 

    // Movement Actions 
    actionMoveDown = [SKAction moveTo:CGPointMake(actualX, CGRectGetMinY(self.frame)) duration:actualDuration]; 
    actionMoveEnded = [SKAction removeFromParent]; 
    [marsugo runAction:[SKAction sequence:@[actionMoveDown, actionMoveEnded]]]; 

    NSLog(@"Marsugo X: %f - Speed: %i", marsugo.position.x, actualDuration); 
} 


@end 

내가 이전에 말했듯이, 나는 "다음 스폰되는 스프라이트"가 아니라 텍스처를 바꾸기 위해 자기 스프라이트가 필요합니다.

이 문제를 해결하는 데 도움을 주시면 감사하겠습니다. 감사합니다.

답변

0

Marsugo 클래스에서 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event을 재정의하고 여기에서 텍스처를 변경할 수 있습니다. 그렇게하면 터치가 노드 내부에 있는지 여부를 확인할 필요가 없습니다. 그렇지 않으면 해당 메서드가 호출되지 않기 때문입니다.

1

touchesBegan을 사용하고 싶지 않습니다. 탭 제스처 인식기를 사용하는 것이 좋습니다. 아래에 _testView, viewDidLoad보기에서 만들고 추가하는 인스턴스 변수입니다. 나는 다음보기 탭 때 함수를 호출 탭 제스처 인식기를 생성하고, 그 함수는 뷰의 색상을 변경 -하지만 경우에 당신은 이미지를 변경하여 함수를 호출 할 수 있습니다

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // create the test view and add it as a subview 
    _testView = [[UIView alloc] initWithFrame:CGRectMake(20, 100, 100, 100)]; 
    _testView.backgroundColor = [UIColor redColor]; 
    [self.view addSubview:_testView]; 

    // create the tap gesture recognizer and add it to the test view 
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeColor)]; 
    [_testView addGestureRecognizer:tapGestureRecognizer]; 
} 

- (void)changeColor { 
    // here I'm changing the color, but you can do whatever you need once the tap is recognized 
    _testView.backgroundColor = [UIColor blueColor]; 
} 

이 결과를 처음에는이 지역 :

enter image description here

그럼 내가보기 누를 때 :

enter image description here

+0

을 내 SKScene에는보기가 없습니다. – user3504848

+0

SKScene의 하위 클래스는 무엇입니까? – Mike

+0

그러면 더 나은 옵션은 아래에 언급 된 것처럼'- (void) touchesBegan : (NSSet *) toEvents : (UIEvent *) event'를 오버라이드하는 것입니다. – nikhil84

관련 문제