我是初学者,使用Xcode的sprite工具包为AppStore开发程序。我制作了一个“测试”程序,所以我可以先尝试新的东西,然后再添加到我的游戏中。现在我正在摆弄一个场景 - 我有一个SKNode(称为“背景”),我在这里添加几个孩子作为SKSpriteNodes。一个精灵节点在初始场景(中心,位置160,240)上可见,另外两个不可见:在场景的左侧(位置-160,240),以及场景的右侧(位置480,240)。
我希望我的游戏能够向左或向右滑动,当它向左或向右滑动时,视图会自动将自身(带动画)自动居中到三个SKSpriteNodes中的一个。我使用UIPanGestureRecognizer移动背景节点的代码工作正常,我自动居中视图的代码工作MOSTLY(背景位置设置为0,0或-320,0或+320,0),但有时它有一个奇怪的偏移并且不完全居中(例如,当我向右或向左平移时,背景位置将是7,0或-34,0)。我做错了什么?
P.S:我使用RayWenderlich的“iOS游戏”中的代码作为SKTMoveEffect。我还要注意,如果我使函数f(t)= t,没有问题(至少在我的几个测试中),但f(t)= t ^ 2或其他任何东西似乎都有问题;如果它有助于查看代码,我也可以发布它
@implementation LTMyScene
{
SKNode *background;
SKSpriteNode *spaceship1, *spaceship2;
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
background=[SKNode node];
[self addChild:background];
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
myLabel.text = @"Hello, World!";
myLabel.fontSize = 30;
myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
[background addChild:myLabel];
spaceship1=[SKSpriteNode spriteNodeWithImageNamed:@"Spaceship.png"];
spaceship1.position=CGPointMake(-self.size.width/2, self.size.height/2);
spaceship1.anchorPoint=CGPointMake(0.5, 0.5);
[background addChild:spaceship1];
spaceship2=[SKSpriteNode spriteNodeWithImageNamed:@"Spaceship.png"];
spaceship2.position=CGPointMake(self.size.width*3/2, self.size.height/2);
spaceship2.anchorPoint=CGPointMake(0.5, 0.5);
[background addChild:spaceship2];
}
return self;
}
- (void)didMoveToView:(SKView *)view
{
UIPanGestureRecognizer *swipe = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(dragPlayer:)];
[[self view] addGestureRecognizer:swipe];
}
-(void)dragPlayer: (UIPanGestureRecognizer *)gesture {
[[[self view] layer] removeAllAnimations];
CGPoint trans = [gesture translationInView:self.view];
SKAction *moveAction = [SKAction moveByX:trans.x y:0 duration:0];
[background runAction:moveAction];
[gesture setTranslation:CGPointMake(0, 0) inView:self.view];
if([gesture state] == UIGestureRecognizerStateEnded) {
CGFloat finalX=0;
if (abs(background.position.x)<self.size.width/2) {
finalX=0;
} else if (abs(background.position.x)<self.size.width*3/2) {
finalX=self.size.width*background.position.x/abs(background.position.x);
}
NSLog(@"%f",finalX);
SKTMoveEffect *upEffect =
[SKTMoveEffect effectWithNode:background duration:0.5
startPosition:background.position
endPosition:CGPointMake(finalX, 0)];
upEffect.timingFunction = ^(float t) {
// return powf(2.0f, -3.0f * t) * fabsf(cosf(t * M_PI * 1.0f)) //has bounce
// return (-1.0f*t*t+1) //no bounce ... for parabola this is only solution with (1,0) and (0,1) as intercepts and vertex at (1,0)
return (t*t)
;};
SKAction *upAction = [SKAction actionWithEffect:upEffect];
[background runAction:upAction];
}
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
NSLog(@"%f,%f",background.position.x,background.position.y);
}
@end
答案 0 :(得分:0)
您必须记住,您正在移动较大的背景节点,其他节点作为其子节点。牢记这一点,您需要以特定节点为中心的代码是:
_worldNode.position = CGPointMake(-(myNode.position.x-(self.size.width/2)), -(myNode.position.y-(self.size.height/2)));
以上假设您的主要背景&#34;画布&#34;被称为_worldNode
,您的目标节点是_worldNode