2014-02-26 16 views
0

的Xcode 雪碧套件:雪碧套件:人物移動通過在屏幕上(X軸)draging他設置,但留在一組(y位置)

如何讓我的:

-(SKSpriteNode*) createcharacter{ 
    SKSpriteNode *character = [SKSpriteNode spriteNodeWithImageNamed:@"Character.png"]; 
    character.position = CGPointMake(164, 67); 
    character.name = @"Character"; 
} 

讓我的角色在被拖動時左右移動?

我需要他留在一個y的位置,但x位置是由拖動改變。

感謝答案:)

here's我做到了定期的Xcode:(不確定如何與精靈套件做)

UITouch *Drag = [[event allTouches] anyObject]; 
character.center = [Drag locationInView:self.view]; 

if (character.center.y > 472) { 
    character.center = CGPointMake(character.center.x, 472);  
} 

if (character.center.y < 472) { 
    character.center = CGPointMake(character.center.x, 472); 
}  

if (character.center.x < 21) { 
    character.center = CGPointMake(21, character.center.y); 
} 


if (character.center.x > 299) { 
    character.center = CGPointMake(299, character.center.y);   
} 
+2

character.position = CGPointMake(x,200);假設200是你的位置y pos – LearnCocos2D

+0

你能詳細點嗎?我已經在「常規」xcode中添加了我的代碼。但我不確定如何使用sprite套件解決這個問題;) – user3338571

回答

1

在你SKScene:

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [touches anyObject]; 

    CGPoint positionInScene = [touch locationInNode:self]; 
    CGPoint previousPosition = [touch previousLocationInNode:self]; 
    CGPoint translation = CGPointMake(positionInScene.x - previousPosition.x, 
             positionInScene.y - previousPosition.y); 

    // Animate the ship up or down based on finger position. 
    [self moveCharacterWithTranslation:translation]; 
} 

- (void)moveCharacterWithTranslation:(CGPoint)translation { 
    // Animate the ship up or down based on finger position. 

    [character runAction:[SKAction sequence:@[[SKAction moveByX:translation.x y:0 duration:0.4f]]]]; 
}