iphone - Infinite scrolling on a sprite(Parallax) -
i newbie world of cocos2d developing first tutorial , facing 1 problem problem have image (1024 x 320) , orientation landscape need move image continuously right left purpose have used space shooter tutorial ray(thanks him) image doesn't seem appearing again , again. code is..
-(id) init { if( (self=[super init])) { cgsize screensize = [ccdirector shareddirector].winsize; // 1) create ccparallaxnode backgroundnode = [ccparallaxnode node]; [self addchild:backgroundnode z:-1]; // 2) create sprites we'll add ccparallaxnode = [ccsprite spritewithfile:@"bg_front_spacedust.png"]; //back.position=ccp(screensize.width/2, screensize.height/2); back.rotation = -90; back1 = [ccsprite spritewithfile:@"bg_front_spacedust.png"]; back1.rotation = -90; // 3) determine relative movement speeds space dust , background cgpoint dustspeed = ccp(0.1, 0.1); // 4) add children ccparallaxnode [backgroundnode addchild:back z:0 parallaxratio:dustspeed positionoffset:ccp(screensize.width/2, screensize.height/2)]; nslog(@"back.content width is...%f",back.contentsize.width); [backgroundnode addchild:back1 z:1 parallaxratio:dustspeed positionoffset:ccp(screensize.width/2, screensize.height*2)]; // 5) enable updates [self scheduleupdate]; } return self; } - (void)update:(cctime)dt { // 1) update background position cgpoint backgroundscrollvel = ccp(0,-1000); backgroundnode.position = ccpadd(backgroundnode.position, ccpmult(backgroundscrollvel, dt)); // 2) check background elements moving offscreen nsarray *spacedusts = [nsarray arraywithobjects:back, back1, nil]; (ccsprite *spacedust in spacedusts) { if ([backgroundnode converttoworldspace:spacedust.position].x < -spacedust.contentsize.width) { [backgroundnode incrementoffset:ccp(2*spacedust.contentsize.width,0) forchild:spacedust]; } } }
please me out of this
in advance.
try one
if (backgroundnode.position.y <-screensize.height*2) backgroundnode.position = ccp(0,0);
as init method called once approach doing done 1 time need again set position of backgroundnode 0 in update method. here multiple may vary
Comments
Post a Comment