Cocos2D iOS tour: How to Write a hamster game (5): Set the background

Source: Internet
Author: User

Cocos2D iOS tour: How to Write a hamster game (5): Set the background

 

Set background

Next, open the HelloWorldScene. m file, find your init method, delete the four lines that create the HelloWorld tag, and replace them with the following code:

// Determine names of sprite sheets and plists to loadNSString *bgSheet = @background.pvr.ccz;NSString *bgPlist = @background.plist;NSString *fgSheet = @foreground.pvr.ccz;NSString *fgPlist = @foreground.plist;NSString *sSheet = @sprites.pvr.ccz;NSString *sPlist = @sprites.plist;if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {    bgSheet = @background-hd.pvr.ccz;    bgPlist = @background-hd.plist;    fgSheet = @foreground-hd.pvr.ccz;    fgPlist = @foreground-hd.plist;    sSheet = @sprites-hd.pvr.ccz;    sPlist = @sprites-hd.plist;            }// Load background and foreground[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:bgPlist];       [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:fgPlist];// Add backgroundCGSize winSize = [CCDirector sharedDirector].winSize;CCSprite *dirt = [CCSprite spriteWithSpriteFrameName:@bg_dirt.png];dirt.scale = 2.0;dirt.position = ccp(winSize.width/2, winSize.height/2);[self addChild:dirt z:-2]; // Add foregroundCCSprite *lower = [CCSprite spriteWithSpriteFrameName:@grass_lower.png];lower.anchorPoint = ccp(0.5, 1);lower.position = ccp(winSize.width/2, winSize.height/2);[self addChild:lower z:1];CCSprite *upper = [CCSprite spriteWithSpriteFrameName:@grass_upper.png];upper.anchorPoint = ccp(0.5, 0);upper.position = ccp(winSize.width/2, winSize.height/2);[self addChild:upper z:-1];// Add more here later...

Let's take a look at it for a while, because there are many new things:

Determine the name of the loaded sprite table and plists. this section lists the names of the genie table to be loaded and the plists File Created by TexturePacker. note that in iphoen, Cocos2D will automatically select an image of the appropriate version based on the device's screen when HD display is enabled. however, the image of the "-hd" version is not loaded in the ipad unless you tell it. therefore, check whether it is an ipad. If yes, use a high-definition image. load background and foreground. the next step is to load every genie related to the background and foreground you use into the sprite frame buffer. note that these genie will not actually be loaded to any location of CCSpriteBachNode-because these images will only be used once. add background. the background image is loaded to this layer and becomes its subnode (the z-order is-2 so that it can appear below all the eastern branches ). enlarge the image by 2 times because we intentionally make it easier to live and save space. add foreground. the foreground has two parts. to easily place an image, it sets the anchor of the top half of the image to center/bottom, the lower half to center/top, and centers the anchor on the screen. this allows you to display the information correctly on any device without any complicated mathematics. note that some background will be displayed on the iphone beyond the screen, but there is no problem with the background, and I will not even notice it. note that the z-order of the two images is different, and the so low z-order images appear on the top.

Compile and run the code. You should see the following picture! Try the iphone and ipad simulators to make sure all devices are correctly displayed.

If you run and zoom in on the HD screen, you will notice that the normal image is still used: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwPjxpbWcgYWx0PQ = "here write picture description" src = "http://www.bkjia.com/uploads/allimg/151204/0416441X5-1.jpg" title = ""/>

This is because we didn't do the first step in the previous "HD screen and Cocos2D" section: Call the enableRetinaDisplay method of CCDirector to enable HD support.

After HD support is enabled, compile and run your code. Now you will find that HD images are used on the HD screen device.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.