1. Introduce
The realization screen background automatic scrolling is the game often encounters the function, thus we do not have to draw the very long background picture, as long as designs one to be possible, saves time and effort. This chapter will implement this function, and the source code contribution to everyone, nonsense not to say, the first picture, in fact, is dynamic, just cut the dynamic diagram a little trouble.
2. Code Implementation Section
The dynamic scrolling of the screen is primarily a matter of refreshing the mechanism.
The first step is to enter the object that defines some nodes in the HelloWorldLayer.h
<span style= "color: #362e2b" > #import <GameKit/GameKit.h>
/When your import this file, and import all the C ocos2d classes
#import "cocos2d.h"
//Helloworldlayer
@interface Helloworldlayer:cclayer < Gkachievementviewcontrollerdelegate, gkleaderboardviewcontrollerdelegate>
{
Ccparallaxnode * Backgroundnode;//</span><span style= "color: #ff6666" > This node is the key node for implementing scrolling </span><pre name= "code" class= "OBJC" style= "Color:rgb (54,46,43); line-height:26px "></pre><span style=" color: #362e2b "> ccsprite *mainbg; Ccspritebatchnode *batchnode; } Returns a ccscene that contains the Helloworldlayer as the only child+ (Ccscene *) scene; @end </span>
The second step is of course to come to HELLOWORLDLAYER.M.
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/
First, write down the refreshed function.
-(void) Updatebackground: (cctime) dt {
cgsize size = [Ccdirector shareddirector].winsize;
Cgpoint backgroundscrollvel = CCP (-size.width, 0);
Backgroundnode.position =
Ccpadd (backgroundnode.position,
ccpmult (backgroundscrollvel, DT));
Cgsize winsize = [Ccdirector shareddirector].winsize;
Nsarray *backgrounds = [Nsarray arraywithobjects:mainbg,nil];
For (Ccsprite *background in backgrounds) {
if (Backgroundnode converttoworldspace:background.position].x <- Background.contentSize.width) {
//if is judged if the screen exceeds the picture range
backgroundnode.position = CCP (winsize.width*4,0); Picture is moving at this speed}}}
Next, add the previous function to the update to implement a live update.
Real-time update
-(void) Update: (cctime) dt {
[self updatebackground:dt];
}
Modifying the Init function
<span style= "color: #362e2b" >-(ID) init {//Always call ' super ' init//Apple recommends to re-assign "Self" with the "Super" "Return value if ((Self=[super init])) {cgsize winsize = [Ccdirector shareddir
Ector].winsize;
1 Create Ccparallaxnode parallax scrolling node backgroundnode = [Ccparallaxnode node];
[Self addchild:backgroundnode z:-2];
2 Create the wizard object that needs to be added to the Ccparallaxnode parallax scrolling node MAINBG = [Ccsprite spritewithfile:@ "Bg.png"];
3 Set the floating speed and background speed of the cloud cgpoint bgspeed = CCP (0.05, 0.05); 4 Add the wizard object as a child node of the Ccparallaxnode Parallax scrolling node [backgroundnode addchild:mainbg z:-1 parallaxratio:b
Gspeed POSITIONOFFSET:CCP (200,winsize.height*0.5)]; [Self Scheduleupdate];//</span><span style= "color: #ff6666" > Call Update, note that this invocation method is peculiar </span><span style= "color: #362e2b" >} return self; } </span>
Source code resource Download Address: http://download.csdn.net/detail/gshengod/6425997
Author: csdn Blog Rae Garvin