Cocos2D iOS之旅:如何写一个敲地鼠游戏(五):设置背景
发布时间:2020-12-14 16:46:08 所属栏目:百科 来源:网络整理
导读:大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流之用,请勿进行商业用途。同时,转载时不要移除本申明。如
设置背景接下来,打开HelloWorldScene.m文件,然后找到你的init方法.删除创建HelloWorld标签的4行,用以下代码替换: // Determine names of sprite sheets and plists to load
NSString *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 background
CGSize 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 foreground
CCSprite *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...
让我们一段一段的看一下,因为这里有好多新的东东:
编译运行代码,你应该看到如下画面!在iphone和ipad模拟器上都试一下,确保所有设备都显示正确. 如果你在高清屏上运行并且放大,你将注意到仍然使用的是普通的图片: 这是因为我们并没有在之前”高清屏和Cocos2D”章节里做其中的第一步:调用CCDirector的enableRetinaDisplay方法去开启高清支持. 在开启高清支持之后再编译运行你的代码,现在你会发现在高清屏设备上使用的是HD图片了. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |