Friday 15 April 2011

animation - Infinite horizontal background ios using SKAaction -



animation - Infinite horizontal background ios using SKAaction -

i have long image want set "rollable" background . i'm trying utilize 2 instance of sprite , 1 time image reached (frame - imageheight) add together sec 1 above , maintain smooth movement.

however, i'm getting unexpected behaviour - @ lastly part of animation screen overlapped - sec screen overlapping first one.

this code:

- (void) repositionbackground{ skspritenode *currentbackground = self.backgroundarray[self.currentbackgoundindex]; if(!currentbackground.parent && !self.currentbackgoundindex) //first time running { skaction *imageminusframeanimation = [skaction movetoy:-currentbackground.size.height+self.frame.size.height/2.0f duration:self.backgroundanimationlength]; skaction *sequence = [skaction sequence:@[imageminusframeanimation, [skaction runblock:^{ [self repositionbackground]; }]]]; [currentbackground runaction:sequence]; [self addchild:currentbackground]; nslog(@"calling reposition first time"); } else { self.currentbackgoundindex = (self.currentbackgoundindex + 1) % [self.backgroundarray count]; skspritenode *newbackground = self.backgroundarray[self.currentbackgoundindex]; //reposition th enew backfround on top of current 1 [newbackground setposition:cgpointmake(0, self.frame.size.height/2.0f)]; //add actions backfround - top backgound vanished screen , new 1 appear , start scrolling downwards skaction *imageminusframeanimation = [skaction movetoy:-currentbackground.size.height+self.frame.size.height/2.0f duration:self.backgroundanimationlength]; skaction *sequence = [skaction sequence:@[imageminusframeanimation, [skaction runblock:^{ [self repositionbackground]; }]]]; [newbackground runaction:sequence]; if(!newbackground.parent) { [self addchild:newbackground]; } float newbackgroundanimationlength = self.frame.size.height / background_animation_velocity; skaction *framelengthanimation = [skaction movebyx:0 y:-self.frame.size.height duration:newbackgroundanimationlength]; [currentbackground runaction:framelengthanimation]; }

}

i figure out myself. aa error in algorithm. post here solution: (spritekit)

//creating 2 background - positioning 1 above screen , 1 stand on (0.5,0} axis skspritenode *firstbackground = [skspritenode spritenodewithimagenamed:@"mtimage.jpeg"]; firstbackground.anchorpoint = cgpointmake(0.5f,0); firstbackground.zposition = 1; skspritenode *secondbackground =[firstbackground copy]; self.backgroundarray = @[firstbackground,secondbackground]; [firstbackground setposition:cgpointmake(0, -self.frame.size.height/2)]; [secondbackground setposition:cgpointmake(0, self.frame.size.height/2)]; self.currentbackgoundindex = 0; self.backgroundanimationlength = firstbackground.size.height / background_animation_velocity; [self repositionbackground]; - (void) repositionbackground{ skspritenode *currentbackground = self.backgroundarray[self.currentbackgoundindex]; skaction *majorpartanimation; //first time running if(!currentbackground.parent && !self.currentbackgoundindex) { float firstanimationduration = (currentbackground.size.height - self.frame.size.height) / background_animation_velocity; majorpartanimation = [skaction movetoy:-currentbackground.size.height+self.frame.size.height/2.0f duration:firstanimationduration]; skaction *sequence = [skaction sequence:@[majorpartanimation, [skaction runblock:^{ [self repositionbackground]; }]]]; [currentbackground runaction:sequence]; [self addchild:currentbackground]; } else { //current background needs move frameheight , new 1 has set above him , start animation majorpartanimation = [skaction movetoy:-currentbackground.size.height+self.frame.size.height/2.0f duration:self.backgroundanimationlength]; self.currentbackgoundindex = (self.currentbackgoundindex + 1) % [self.backgroundarray count]; skspritenode *newbackground = self.backgroundarray[self.currentbackgoundindex]; //reposition th enew backfround on top of current 1 [newbackground setposition:cgpointmake(0, self.frame.size.height/2.0f)]; //add actions backfround - top backgound vanished screen , new 1 appear , start scrolling downwards skaction *sequence = [skaction sequence:@[majorpartanimation, [skaction runblock:^{ [self repositionbackground]; }]]]; [newbackground runaction:sequence]; if(!newbackground.parent) { [self addchild:newbackground]; } float currentbackgroundanimationlength = self.frame.size.height / background_animation_velocity; skaction *minorpartaniamiton = [skaction movebyx:0 y:-self.frame.size.height duration:currentbackgroundanimationlength]; [currentbackground runaction:minorpartaniamiton]; }

}

ios animation sprite-kit skaction

No comments:

Post a Comment