objective c - Why does this code not properly center scene when doing UIPanGestureRecognizer? -
i beginner developing program appstore using xcode's sprite kit. made 'test' program can try out new things before adding game. right fiddling around swiping scene - have sknode (called "background") adding several children skspritenodes. 1 sprite node visible on initial scene (the center, position 160,240), , 2 more not visible: left of scene (position -160,240), , right of scene (position 480,240).
i game able swipe left or right, , when swipes left or right, view auto-center (with animation) 1 of 3 skspritenodes. code using uipangesturerecognizer move background node works properly, , code auto-centering view works (background position set 0,0 or -320,0 or +320,0), has strange offset , doesn't center (for example, background position 7,0 or -34,0 when pan right or left). doing wrong?
p.s: using code raywenderlich's "ios games" sktmoveeffect. want note if make function f(t)=t, there no problem (at least in several tests), f(t)=t^2 or else seems have issue; if helps see code can post too
@implementation ltmyscene { sknode *background; skspritenode *spaceship1, *spaceship2; } -(id)initwithsize:(cgsize)size { if (self = [super initwithsize:size]) { /* setup 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 ... parabola solution (1,0) , (0,1) intercepts , vertex @ (1,0) return (t*t) ;}; skaction *upaction = [skaction actionwitheffect:upeffect]; [background runaction:upaction]; } } -(void)update:(cftimeinterval)currenttime { /* called before each frame rendered */ nslog(@"%f,%f",background.position.x,background.position.y); } @end
you have remember moving larger background node other nodes children. keeping in mind, code need center on specific node is:
_worldnode.position = cgpointmake(-(mynode.position.x-(self.size.width/2)), -(mynode.position.y-(self.size.height/2)));
the above assumes main background "canvas" called _worldnode
, target node child of _worldnode
Comments
Post a Comment