Can someone explain to me how cocos2d uses png as a font?
I would like to do something similar as my font only contains numbers.
if you do a global search on the string fps_images.png , your IDE should take you real close to the following lines in cocos CCDirector class (version 2.0) :
FPSLabel_ = [[CCLabelAtlas alloc] initWithString:#"00.0" charMapFile:#"fps_images.png" itemWidth:12 itemHeight:32 startCharMap:'.'];
SPFLabel_ = [[CCLabelAtlas alloc] initWithString:#"0.000" charMapFile:#"fps_images.png" itemWidth:12 itemHeight:32 startCharMap:'.'];
drawsLabel_ = [[CCLabelAtlas alloc] initWithString:#"000" charMapFile:#"fps_images.png" itemWidth:12 itemHeight:32 startCharMap:'.'];
then look up CCLabelAtlas. Your image must be for a fixed width font.
If you want to reuse the same image included to make something other than the FPS display the following code should work:
CCLabelAtlas *scoreLabel = [[CCLabelAtlas alloc] initWithString:#"00.0" charMapFile:#"fps_images.png" itemWidth:12 itemHeight:32 startCharMap:'.'];
Then to set it to the numbers you want something like this works:
[scoreLabel setString:[NSString stringWithFormat:#"%d", (int)score]];
Related
I just started learning cocos2d-x.
I am trying to create menu using sprite sheet, below:
CCSpriteFrameCache::sharedSpriteFrameCache()>addSpriteFramesWithFile("my_menu.plist");
CCMenuItem *play = CCMenuItemImage::create("play.png", NULL,NULL,this , menu_selector(StartScene::clickStart));
CCMenu *pMenu = CCMenu::create(play,NULL);
addChild(pMenu);
I got error message:
get data from file (play.png) failed.
I realise something wrong with my create function. I am just wondering how to get the image from sharedSpriteFrameCache?
OK, I just figure it out:
CCMenuItemSprite *play = CCMenuItemSprite::create(CCSprite::createWithSpriteFrameName("play.png"), NULL,NULL,this ,menu_selector(StartScene::clickStart));
spriteWithSpriteFrameName is deprecated, instead, we can use:
CCSprite::createWithSpriteFrameName();
u need to take the plist file in a ccspritebatchnode object and manipulate it using this object.
CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:#"AnimBear.png"];
[self addChild:spriteSheet];
Simply trying to test retina display. I setup the director like this:
CCDirectorIOS* director = (CCDirectorIOS*)[CCDirector sharedDirector];
director.wantsFullScreenLayout = NO;
director.projection = kCCDirectorProjection2D;
director.animationInterval = 1.0 / 60.0;
director.displayStats = YES;
[director enableRetinaDisplay:YES];
I create two versions of the file in Photoshop - outline-hd.png and outline.png. I color the HD version red so I can tell if it's being displayed.
Display code:
CCSprite *border = [CCSprite spriteWithFile:#"outline.png"];
[self addChild:border];
Yet it is the non-hd image that gets displayed on my iPhone5. Why?
I came across this question while trying to solve the exact same problem in my own project. Had to dig around in the cocos2d source to figure it out. The problem is that the director's enableRetinaDisplay:YES method doesn't work unless the director's view is set. So, it needs to be called after the glView is set up, and you've called setView on the director:
CCGLView *glView = [CCGLView viewWithFrame:aFrame
pixelFormat:kEAGLColorFormatRGBA8
depthFormat:0
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
[[CCDirector sharedDirector] setView:glView];
NSLog(#"glView is set, enable retina...");
[[CCDirector sharedDirector] enableRetinaDisplay:YES];
This should fix the problem for you!
May be you forgot:
CCFileUtils *sharedFileUtils = [CCFileUtils sharedFileUtils];
[sharedFileUtils setEnableFallbackSuffixes:NO];
[sharedFileUtils setiPhoneRetinaDisplaySuffix:#"-hd"];
I have a Tabbar control (as the root control).
In the first tab, I have a split view, which I programmatically created like so:
UISplitViewController *split = [[[UISplitViewController alloc] init] autorelease];
split.navigationItem.title = #"All";
MasterSplitViewController *root = [[[MasterSplitViewController alloc] init] autorelease];
root.navigationItem.title = #"Areas";
root.detailViewController = detail;
UINavigationController *nav = [[[UINavigationController alloc] initWithRootViewController:root] autorelease];
split.tabBarItem = controller.tabBarItem;
split.viewControllers = [NSArray arrayWithObjects: nav, detail, nil];
split.delegate = detail;
Everything works fine, except... If I start the app in Landscape mode, then the layout breaks a bit on the Master view.
If the app starts in Portrait, then I rotate to Landscape, then it is fine.
The only scenario when it breaks is when the app starts in Landscape.
When this happens, I check the console and see this message:
Using two-stage rotation animation. To use the smoother single-stage animation, this application must remove two-stage method implementations. BUT I am not using a two-stage rotation anywhere!!
These 2 functions are overridden:
shouldAutorotateToInterfaceOrientation and willAnimateRotationToInterfaceOrientation
Please see the attached screen shot.
http://img97.imageshack.us/i/screenshot20110405at308.png/
Please help me. Any suggestion would be greatly appreciated. Thanks very much!!
I had this problem.
Add this code to AppDelegate.m.
self.tabBarController.selectedIndex = 0;
Is it possible? I guess it is, but I can't figure it out.
So, the code for the movie thus far:
shrigString = [[NSBundle mainBundle] pathForResource:#"shrig_h264_720p" ofType:#"mov"];
shrigURL = [NSURL fileURLWithPath:shrigString];
//shrigMovie = [QTMovie movieWithFile:#"shrig_h264_720p.mov" error:nil];
shrigMovie = [QTMovie movieWithURL:shrigURL error:nil];
shrigMovieView = [QTMovieLayer layerWithMovie:shrigMovie];
[[[[CCDirectorMac sharedDirector] openGLView] window] addChildWindow:shrigWindow ordered:NSWindowAbove];
[shrigWindow setContentView:shrigNSView];
[shrigNSView setLayer:shrigMovieView];
shrigMovieView.frame = CGRectMake(size.width /2, size.height /2, 1024, 576);
[shrigMovie autoplay];
[shrigMovie play];
NSLog(#"shrigMovie: %#", shrigMovie);
NSLog(#"shrigMovie is: %#", QTStringFromTime([shrigMovie currentTime]));
However, I can't figure out how to ad that to my scene. This is Cocos2D for Mac btw.
In the console:
shrigMovie is: 0:00:00:00.00/2500 So I guess it's loading ok, but simply not displaying?
Any ideas would be hugely appreciated.
Thanks.
I ran into the same problem and spent quite a while trying to figure out how to solve it using QTMovie in combination with cocos2D. Eventually I gave up and started looking if there was a good existing solution to this. And voilĂ , I found VideoPlayer. Very easy to use and very lightweight, you can find it on https://github.com/psineur/CCVideoPlayer. Just be sure to copy only the necessary VideoPlayer-folder from the zip-file and don't forget the CustomVideoView.nib in the Resources folder. Add those to some lib-folder within your project, then you can start playing Video-files using those two lines:
[VideoPlayer setDelegate:self];
[VideoPlayer playMovieWithFile:#"intro.mov"];
You can then add the following delegate methods to your class
- (void) moviePlaybackFinished
{
}
- (void) movieStartsPlaying
{
}
Got it working in less than 10 minutes :)
Cheers
I am creating a game of questions and answers for iphone using cocos2d, and I wonder how can I do to display the question on the screen using ccLabel seeking data from a plist. can someone help me with this!
What you want to do is grab it from your resources bundle, and save it into a dictionary. From there you have access to every value inside of your plist. That can be achieved with something like this:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:#"myList.plist"];
NSDictionary *plistData = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
Then when you're ready to display the question just use the [plistData objectForKey: ] method. Do you need help setting up the label also?
Sure man. You want to set it up like this:
CCLabel* questionLabel = [CCLabel labelWithString:#"Your Question"
fontName:#"Marker Felt" fontSize:64];
CGSize size = [[CCDirector sharedDirector] winSize];
label.position = ccp( size.width /2 , size.height/2 );
[self addChild: questionLabel];
That'll display your label in the middle of the screen. You can change the position, font, etc. The way you access your data from the plist is determined by exactly how you set it up. But using the technique I gave your earlier you shouldn't have a problem.