i am trying to set texture with this :
[[self setSpriteByTag] setTexture:[[CCTextureCache sharedTextureCache] addImage:[NSString stringWithFormat:#"%#0001.png",face]]];
when [self setSpriteByTag] return a sprite face is an image string, and they both ok because i have logged them.
this sprite is a child of a page in ccscrolayer :
one page of the layer pages :
CCLayer *page1 = [[CCLayer alloc] init];
BACK.position=ccp(screenSize.width/2,screenSize.height/2);
[page1 addChild:BACK];
when i was trying to change BACK's image , with the command i wrote here.
what happen is that i see a white screen.
i have check that the image is in my assets .
what could be the problem ?
the facr that i change a sprite that is a child of a page ?
edit :
this is the function that gives me the sprite BACK:
-(CCSprite*)setSpriteByTag
{
int currentPage=[scroller currentScreen];
[globals sharedGlobals].currentPageG=currentPage; //move to touch ended?
currentPage=currentPage+1;
//NSLog(#"PAGE:%d",currentPage);
if(currentPage==1)
{[globals sharedGlobals].WhatFace =#"BeastFace"; return BACK;}
else if(currentPage==2)
{[globals sharedGlobals].WhatFace =#"BlueFace"; return BACK1;}
else if(currentPage==3)
It gets white only in case when image is not present in the resources.. I checked running same code.. It works fine.. If I put wrong name of the image which is not present.. It changes to white... Please check for the image
[NSString stringWithFormat:#"%#0001.png",face]
M sure problem is in the image name.. :)
[NSString stringWithFormat:#"%#0001.png",face]
Your filenames will have to have the format "xxxxx0001.png". Assuming the face variable is the string "BeastFace" then the filename to load will be "BeastFace0001.png".
So far the obvious but keep in mind that the file "Beastface0001.png" will not load on the device, and neither will "beastface0001.png" because iOS devices use a case-sensitive file system. It will work on the iOS Simulator though.
Related
I'm trying to set a background image for a CCButton and make sure it stretches or minimizes to the exact size of the CCButton:
-(void)setBackgroundSpriteFrame: (NSString*)spriteFrameFileName
{
NSString* fullFileName = [NSString stringWithFormat:#"%#.png", spriteFrameFileName];
CCSpriteFrame *spriteFrame = [CCSpriteFrame frameWithImageNamed:fullFileName];
[super setBackgroundSpriteFrame:spriteFrame forState:CCControlStateNormal];
double scaleValue = self.contentSize.height / spriteFrame.rect.size.height;
[self.background setScale:scaleValue];
self.fileName = spriteFrameFileName;
}
This method resides inside a class that inherits from CCButton.
The problem is that I see the background image but it is squeezed in a weird way (see image) and not minimized\stretched to the size of the button.
Please note that the image and the button are squares so no need to scale X\Y separately, and also note that the CCButton is generated with SprtieBuilder and not by code.
'Weird look':
Original look:
Thanks
Thanks 'YvesLeBorg' for trying to help, I managed to solve this issue.
I'm posting the code that helped me.
Hope it will help others.
NSString* fullFileName = [NSString stringWithFormat:#"%#.png", spriteFrameFileName];
CCTexture *texture = [CCTexture textureWithFile:fullFileName];
texture.contentScale = 2.0;
[super setBackgroundSpriteFrame:[texture createSpriteFrame] forState:CCControlStateNormal];
[self setScale:0.40];
self.fileName = spriteFrameFileName;
Have not enough reputation to comment, so please excuse to write an answer instead.
When I encountered the same problem I found a net solution on the cocos2d forum (no link, sry). Summarizing it - there is no need to scale the image separately. But one shall follow 2 rules:
1) the image should be at least same size than the element (CCButton) and
2) not forget to explicitly set the forState:CCControlStateNormal
the usage of contentScale as you did is not needed in that case.
I have a project in which I am trying to implement a pathfinding class but I have stumbled upon an issue I can't seem to fix.
A video of the error as it occurs can be found here:
https://www.dropbox.com/s/25ajb0mc5p4a3a9/Tilemap%20Error.mov
The error message thrown is: Assertion failure in -[CCTMXLayer tileGIDAt:withFlags:], /Users/Dave/Desktop/AI_DISS_new copy/AI_DISS/libs/cocos2d/CCTMXLayer.m:304
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'TMXLayer: invalid position'.
The point returns as pixel values while the map and layer sizes return in tile values so I tried dividing variables holding pixel values by the width or height of the tiles but nothing seems to have worked.
GID for tileAt:point returns too high when using pixel values (understandably) and when converted to tile values, searches for a very long time, eventually moving off the map and continuously searching until Xcode crashes.
I have tried to explain in as much detail as possible I apologize if this isn't the kind of question best suited to this forum. I would really appreciate any assistance.
If you need further details please ask.
The original instructions for using the class were on cocos2d forums which have just had an overhaul and the thread appears to have been moved or deleted so I can't quote them but I am sure the tilemap (a TMX Tilemap) and layer attributes have been passed in correctly.
From my HelloWorldLayer class the values are:
//Pathfinding related code
////////////////////////////
_tileMap = [CCTMXTiledMap tiledMapWithTMXFile:#"NewMap.tmx"];
_pathFinder = [[AStar alloc] initWithTileMap:_tileMap groundLayer:#"Collision"]; //collidable Tiles
[_pathFinder setCollideKey:#"Collidable"]; // defaults to COLLIDE
[_pathFinder setCollideValue:#"True"]; // defaults to 1
_pathFinder2 = [[AStar alloc] initWithTileMap:_tileMap groundLayer:#"Collision"]; //collidable Tiles
[_pathFinder2 setCollideKey:#"Collidable"]; // defaults to COLLIDE
[_pathFinder2 setCollideValue:#"True"]; // defaults to 1
/////////////////////////////
and the move method is called as:
/////////////// player path finding related code //////////////////
[_pathFinder moveSprite:_player from:s to:f atSpeed:0.003];
///////////// end player path finding related code //////////////
Thanks in advance.
GID Code as commented on below:
if (tileGid)
{
// check the tile for the collide property.
NSDictionary *dict = [tileMap propertiesForGID:tileGid];
if (dict)
{
//check the tile for the collideKey and return Yes if a match
NSString *collide = [dict valueForKey:collideKey];
if (collide && [collide compare:collideValue] == NSOrderedSame)
return YES;
}
}
I have a problem in objective-c with a for loop. I want to wait the end of the loop before printing the elements on screen
for (NSDictionary *item in allItems) {
[self print:item];
};
when the loop ends, everything is print, but I would like printing during the loop.
how can i resolve it?
i tried with blocks without success
the print method:
-(void)print:(NSDictionary*)item{
NSString *message=[item objectForKey:#"message"];
if(message){
int userid = 0;
NSString *nome=[item objectForKey:#"name"];
NSString *sesso=[item objectForKey:#"sex"];
NSString *foto=[item objectForKey:#"photo"];
.........
It's not clear what your -print: method does, but I assume that it draws something on screen. Drawing in iOS and MacOS X doesn't happen immediately -- you draw into a graphics context that's managed by the graphics system, and that context is composed with others to generate the image that's ultimately drawn on the screen.
In other words, the screen drawing doesn't happen when you execute drawing commands, it happens when the graphics system periodically updates the screen. This is normal behavior -- it's not a problem.
I am attempting to create an application that goes through various images from the net and aim to cache them onto the iPhone for offline use. The code I am currently working with is:
NSMutableDictionary *Cache;
- (UIImage *)CachedImage: (NSString*)url {
UIImage *image = [Cache objectForKey:url];
if (image == nil) {
image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
[Cache setObject:image forKey:url];
//NSLog (#"Stored");
return image;
} else {
//NSLog (#"Taken");
return image;
} }
I call the function and place the image into an ImageView using the strip of code below.
[self.imageView setImage:[self CachedImage:url]]; // Change url to desired URL.
Using the NSLog, the problem I found is that the code doesn't actually store the value because the value is always reading nil. Why is that and are there other ways of storing images for offline use?
Thanks in advance.
-Gon
Use NSCache to cache UIImages. You can also save the image locally (if you reuse these images a lot and during multiple launch) so whenever your app closes or you flush your cache, you can get the images immediately from your local directory.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSCache_Class/Reference/Reference.html
You are looking for
NSCache
Check it out here: http://nshipster.com/nscache/
Poor NSCache, always being overshadowed by NSMutableDictionary in the
most inappropriate circumstances. It’s like no one knows its there,
ready to provide all of that garbage collection behavior that
developers take great pains to re-implement themselves.
I am developing an iOS application with many images. because i have a designer on my side every moment i am using many images that i don't need anymore.
So at the moment, to clean up this mess i was thinking: If i use a category of UIImage can i override the +imageNamed: to log the name of the image and maybe the method that called it?
something like this:
#implementation UIImage (FindImage)
+ (UIImage *)imageNamed:(NSString *)name {
UIImage *img = [self imageNamed:name];
if (img) {
return img;
} else {
NSLog(#"\n\nImage not found: %#\n\n", name);
return nil;
}
}
#end
I want to override imageNamed because:
I am using it in many different classes
I believe that when a xib load an image it call this method
on resume, my question is: Can i log all images that could not be found on bundle?
EDIT
Refining my question:
i see my question is a little bit complicated so i`m going to try elucidate it.
I want to find all images that my app tries to use, but in my project this images are not found.
I can remove all images, i can create a subclass, i can create categories, i really don't care. Maybe there is a flag on Xcode to complain about missing images...
all i want is to find all places where i`m using an image an this image is not found on bundle.
You could use https://github.com/steipete/Aspects to inject a logging instruction to the end of +imageNamed: