Objective c 为什么在执行UIPanGestureRecognizer时,此代码不能正确地将场景居中?
我是一个使用Xcode的sprite工具包为AppStore开发程序的初学者。我做了一个“测试”程序,这样我可以在添加到游戏之前尝试新的东西。现在我正在摆弄一个场景——我有一个SKNode(称为“background”),在这里我添加了几个孩子作为SKSpriteNodes。一个精灵节点在初始场景(中心,位置160240)上可见,另外两个不可见:场景左侧(位置-160240)和场景右侧(位置480240) 我希望我的游戏能够向左或向右滑动,当它向左或向右滑动时,视图将自动居中(带有动画)到三个SKSpritodes中的一个。我使用UIPanGestureRecognizer移动背景节点的代码工作正常,我用于自动居中视图的代码大部分工作正常(背景位置设置为0,0或-320,0或+320,0),但有时它有一个奇怪的偏移,不能完全居中(例如,当我向右或向左平移时,背景位置将为7,0或-34,0)。我做错了什么 注:我正在使用RayWenderlich的“iOS游戏”中的代码来实现SKTMoveEffect。我还想指出,如果我使用函数f(t)=t,没有问题(至少在我的几个测试中是这样),但是f(t)=t^2或其他任何东西似乎都有问题;如果它有助于查看这方面的代码,我也可以发布它Objective c 为什么在执行UIPanGestureRecognizer时,此代码不能正确地将场景居中?,objective-c,sprite-kit,centering,uipangesturerecognizer,Objective C,Sprite Kit,Centering,Uipangesturerecognizer,我是一个使用Xcode的sprite工具包为AppStore开发程序的初学者。我做了一个“测试”程序,这样我可以在添加到游戏之前尝试新的东西。现在我正在摆弄一个场景——我有一个SKNode(称为“background”),在这里我添加了几个孩子作为SKSpriteNodes。一个精灵节点在初始场景(中心,位置160240)上可见,另外两个不可见:场景左侧(位置-160240)和场景右侧(位置480240) 我希望我的游戏能够向左或向右滑动,当它向左或向右滑动时,视图将自动居中(带有动画)到三个S
@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
@implementation LTMyScene
{
SKNode*背景;
SKSpriteNode*太空船1号,*太空船2号;
}
-(id)initWithSize:(CGSize)size{
if(self=[super initWithSize:size]){
/*在这里设置场景*/
背景=[SKNode];
[自我添加孩子:背景];
self.backgroundColor=[SKColor COLOR WITHRED:0.15绿色:0.15蓝色:0.3阿尔法:1.0];
SKLabelNode*myLabel=[SKLabelNode labelNodeWithFontNamed:@“粉笔除尘器”];
myLabel.text=@“你好,世界!”;
myLabel.fontSize=30;
myLabel.position=CGPointMake(CGRectGetMidX(self.frame)),
CGRectGetMidY(self.frame));
[背景addChild:myLabel];
Spaceship 1=[SKSpriteNode SpritentodeWithimageNamed:@“Spaceship.png”];
spaceship1.position=CGPointMake(-self.size.width/2,self.size.height/2);
太空船1.锚点=CGPointMake(0.5,0.5);
[背景:太空船1号];
Spaceship 2=[SKSpriteNode SpritentodeWithimageNamed:@“Spaceship.png”];
spaceship2.position=CGPointMake(self.size.width*3/2,self.size.height/2);
太空飞船2.锚定点=CGPointMake(0.5,0.5);
[背景:太空飞船2];
}
回归自我;
}
-(void)didMoveToView:(SKView*)视图
{
UIPanGestureRecognizer*滑动=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(dragPlayer:)];
[[self-view]AddGestureRecognitor:滑动];
}
-(无效)拖动层:(UIPangestureRecognitor*)手势{
[[self view]layer]removeAllAnimations];
CGPoint trans=[手势转换视图:self.view];
SKAction*moveAction=[SKAction moveByX:trans.x y:0持续时间:0];
[背景运行操作:移动操作];
[手势集翻译:CGPointMake(0,0)inView:self.view];
如果([手势状态]==UIGestureRecognitizerStateEnded){
cglx=0;
如果(abs(background.position.x)您必须记住,您正在移动较大的背景节点,其他节点作为其子节点。请记住,您需要以特定节点为中心的代码是:
_worldNode.position = CGPointMake(-(myNode.position.x-(self.size.width/2)), -(myNode.position.y-(self.size.height/2)));
以上假设您的主要背景“画布”称为\u worldNode
,并且您的目标节点是\u worldNode
的子节点