I modified standard example "Hello World":
CCSprite *sprite = [CCSprite spriteWithFile:#"Untitled-1.png"];
CGSize winSize = [CCDirector sharedDirector].winSize;
sprite.position = ccp(winSize.width / 2, winSize.height / 2);
[label setBlendFunc:(ccBlendFunc){GL_ZERO, GL_SRC_ALPHA}];
[label setColor:ccBLACK];
[sprite addChild:label];
[self addChild:sprite];
I got:
image
How to delete/cut a background around "Hello World" label?
my current solution is to init CCLabelTTF something like this:
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Hello World" fontName:#"Marker Felt" fontSize:64 dimensions:size hAlignment:kCCTextAlignmentCenter vAlignment:kCCVerticalTextAlignmentCenter];
where size is [CCDirector sharedDirector].winSize for my examle.
any other suggestions are welcome
Related
I need to take the screenshot in my cocos2d application. I've searched a lot, even in stack overflow. Then I found the following code:
+(UIImage*) screenshotWithStartNode:(CCNode*)stNode
{
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCRenderTexture* renTxture =
[CCRenderTexture renderTextureWithWidth:winSize.width
height:winSize.height];
[renTxture begin];
[stNode visit];
[renTxture end];
return [renTxture getUIImage];
}
Now,
Problem: The above code gives me the entire screenshot. But I am in need of the screenshot of a custom are, such as, within a CGRect(50,50,100,200).
Can anyone please help me..? Thanks...
Wow... found out what I need. I've changed the above method like:
-(UIImage*) screenshotWithStartNode:(CCNode*)startNode
{
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize winSize = [CCDirector sharedDirector].winSize;
CCRenderTexture* rtx =
[CCRenderTexture renderTextureWithWidth:winSize.width
height:winSize.height];
[rtx begin];
[startNode visit];
[rtx end];
UIImage *tempImage = [rtx getUIImage];
CGRect imageBoundary = CGRectMake(100, 100, 300, 300);
UIGraphicsBeginImageContext(imageBoundary.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// translated rectangle for drawing sub image
CGRect drawRect = CGRectMake(-imageBoundary.origin.x, -imageBoundary.origin.y, tempImage.size.width, tempImage.size.height);
// clip to the bounds of the image context
CGContextClipToRect(context, CGRectMake(0, 0, imageBoundary.size.width, imageBoundary.size.height));
// draw image
[tempImage drawInRect:drawRect];
// grab image
UIImage* subImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return subImage;
}
I'm very headache about this problem, I'm using these code for capturing a part of the screen
-(UIImage *) glToUIImage {
userphotoCount++;
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize winSize = [CCDirector sharedDirector].winSize;
CCLayerColor* whitePage = [CCLayerColor layerWithColor:ccc4(255, 255, 255, 0) width:winSize.width height:winSize.height];
whitePage.position = ccp(winSize.width/2, winSize.height/2);
CCRenderTexture* rtx = [CCRenderTexture renderTextureWithWidth:815 height:532];
[rtx begin];
[whitePage visit];
[[[CCDirector sharedDirector] runningScene] visit];
[rtx end];
return [rtx getUIImageFromBuffer];
}
I can get my UIImage by
image = [self glToUIImage];
Then I try to use this code to generate a sprite by the UIImage
-(void) ImageSprite{
image = [self glToUIImage];
userAns = [CCSprite spriteWithCGImage:image.CGImage key:[NSString stringWithFormat:#"photoCap%d.png", userphotoCount]];
userAns.position=ccp(780,410);
userAns.scale=0.6;
[self addChild:userAns z:20];
}
I can get image by these code at first time, but after I changed the screen's contents and use these code to generate new sprite(new UIImage) with the new contents, I'm failed.... the image doesn't change to new image...
How can I get the new image sprite when every time I run the "imageSprite" code?
You can get the new image sprite, but you must to change key
[CCSprite spriteWithCGImage:image.CGImage key:key];
As the title, I have mentioned the code below
CCSprite *sprite = (CCSprite *)node;
CCTexture2D *texture2d = [sprite texture];
How can I get a UIImage *p from sprite?
In your case just add next methood into your class:
- (UIImage *) imageFromSprite :(CCSprite *)sprite
{
int tx = sprite.contentSize.width;
int ty = sprite.contentSize.height;
CCRenderTexture *renderer = [CCRenderTexture renderTextureWithWidth:tx height:ty];
sprite.anchorPoint = CGPointZero;
[renderer begin];
[sprite visit];
[renderer end];
return [renderer getUIImage];
}
how to use:
CCSprite *sprite = (CCSprite *)node;
UIImage *p = [self imageFromSprite:sprite]
uhh...
I think this is a better to way. Add this as a category on CCNode.
-(UIImage*)image {
CCRenderTexture* renderer = [CCRenderTexture renderTextureWithWidth:self.contentSize.width height:self.contentSize.height];
const CGPoint ANCHORBEFORE = self.anchorPoint;
self.anchorPoint = CGPointZero;
[renderer begin];
[self visit];
[renderer end];
self.anchorPoint = ANCHORBEFORE;
return [renderer getUIImage];
}
If you are using cocos2d 2.x, render sprite into CCRenderTexture and call getUIImage method of that render texture.
By looking at the source code of both CCSprite and CCTexture2D you can see that the image is used only for creating the texture data and the image object is not kept but temporarily used.
CCTexture2D source :
http://code.google.com/p/cocos2d-iphone/source/browse/trunk/cocos2d/CCTexture2D.m?r=1882
CCSprite source :
http://code.google.com/p/cocos2d-iphone/source/browse/trunk/cocos2d/CCSprite.m?r=1747
Look in the init methods that use CGImageRef
I want the video play in the background, and the text label in the front, run the following code, video is playing, but text label does not show!
-(id) init {
if(!(self=[super init])) {
return nil;
}
CGSize size = [[CCDirector sharedDirector] winSize];
// MP4
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test" ofType:#"m4v"]];
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[moviePlayer respondsToSelector:#selector(setFullscreen:animated:)];
moviePlayer.controlStyle = MPMovieControlStyleNone;
moviePlayer.shouldAutoplay = YES;
moviePlayer.repeatMode = MPMovieRepeatModeOne;
moviePlayer.view.frame = CGRectMake(0, 0, size.height, size.width);
[viewController.view addSubview:moviePlayer.view];
[viewController.view sendSubviewToBack:moviePlayer.view];
// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Hello World" fontName:#"Marker Felt" fontSize:64];
label.position = ccp( size.width /2 , size.height/2 );
[self addChild: label];
return self;
}
I found the answer:
First in AppDelegate.m replace kEAGLColorFormatRGB565 with kEAGLColorFormatRGBA8, Second, as the following code, the last 4 lines is important:
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"video" ofType:#"mp4"]];
_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(moviePlayBackDidFinish)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayer];
[_moviePlayer respondsToSelector:#selector(setFullscreen:animated:)];
_moviePlayer.controlStyle = MPMovieControlStyleNone;
_moviePlayer.shouldAutoplay = YES;
_moviePlayer.repeatMode = MPMovieRepeatModeOne;
_moviePlayer.view.frame = CGRectMake(0, 0, 300, 300);
UIView* glView = [CCDirector sharedDirector].openGLView; // attention
[glView.superview insertSubview:_moviePlayer.view atIndex:0]; // attention
glView.opaque = NO; // attention
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // attention
CCVideo player in one of the best options to play video in cocos2D project
[CCVideoPlayer setDelegate: self];
[CCVideoPlayer playMovieWithFile: #"bait.m4v"];
I am working on a Pause screen menu for a game. After displaying the pause screen, I have the user touch the screen to hide the pause screen and resume the game. This works perfect on the simulator but doesn't work when I test on an actual device. It seems that the device doesn't respond to touches specifically for the pause menu. Every other part of the game works fine on both the simulator and device. It's weird how it does work on the simulator but not on the device. This is the code I have for the pause screen:
- (id)init {
if ((self = [super init])) {
CGSize windowSize = [[CCDirector sharedDirector] winSize];
//windowSize.height = 768.0;
//windowSize.width = 1024.0;
CCSprite *whiteScreen = [CCSprite spriteWithFile:#"OutOfTime.png"];
whiteScreen.position = ccp(windowSize.width / 2, windowSize.height / 2);
[self addChild:whiteScreen];
CCLabelTTF *touchToDismiss = [CCLabelTTF labelWithString:#"Touch screen to continue" fontName:#"Marker Felt" fontSize:30];
touchToDismiss.color = ccBLACK;
touchToDismiss.position = ccp(windowSize.width / 2, 20);
[self addChild:touchToDismiss];
}
return self;
}
- (void)onEnter {
[super onEnter];
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];
}
- (void)gameOverWithScore:(NSInteger)score {
CGSize windowSize = [[CCDirector sharedDirector] winSize];
//windowSize.height = 768.0;
//windowSize.width = 1024.0;
CCLabelTTF *touchToDismiss = [CCLabelTTF labelWithString:#"Game Over" fontName:#"Marker Felt" fontSize:75];
touchToDismiss.color = ccBLACK;
touchToDismiss.position = ccp(windowSize.width / 2, windowSize.height / 2 + 40);
[self addChild:touchToDismiss];
NSString *scoreString = [NSString stringWithFormat:#"Final Score: %d", score];
CCLabelTTF *scoreLabel = [CCLabelTTF labelWithString:scoreString fontName:#"Marker Felt" fontSize:60];
scoreLabel.color = ccBLACK;
scoreLabel.position = ccp(windowSize.width / 2, (windowSize.height / 2) - 40);
[self addChild:scoreLabel];
}
- (void)setMessage:(NSString *)message {
}
- (void)dealloc {
[super dealloc];
}
- (BOOL)ccTouchBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"haha");
return YES;
}
You probably should add the following line on your init
self.isTouchEnabled = YES;
You have to do this for every CCLayer you have that has touch events.
EDIT: just noticed that your ccTouchBegan only prints a Log.
This log is easily viewed in the debug window of XCode, but in the device it is hidden from the user.
Try doing something else.