So within a CCLayer I am adding an ImagePicker / Camera to the openGLView and then a UIButton - all good, but now I want to add a CCLabel (and in the future CCSprites) on top of those elements.
uip = [[UIImagePickerController alloc] init];
uip.sourceType = UIImagePickerControllerSourceTypeCamera;
uip.showsCameraControls = NO;
uip.toolbarHidden = YES;
uip.navigationBarHidden = YES;
uip.wantsFullScreenLayout = YES;
uip.cameraViewTransform = CGAffineTransformScale(uip.cameraViewTransform, CAMERA_TRANSFORM, CAMERA_TRANSFORM);
[[[CCDirector sharedDirector] openGLView] addSubview:uip.view];
arrowButton = [UIButton buttonWithType:UIButtonTypeCustom];
[arrowButton addTarget:self
action:#selector(arrowButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIImage *imgNormal = [UIImage imageNamed:#"btn_next_norm.png"];
[arrowButton setImage:imgNormal forState:UIControlStateNormal];
UIImage *imgPressed = [UIImage imageNamed:#"btn_next_pressed.png"];
[arrowButton setImage:imgPressed forState:UIControlStateHighlighted];
arrowButton.frame = CGRectMake(screenSize.width - 48.0, screenSize.height - 37.0, 48.0, 37.0);
[[[CCDirector sharedDirector] openGLView] addSubview:arrowButton];
CCLabelTTF* label = [CCLabelTTF labelWithString:#"Experience 1" fontName:#"Arial" fontSize:32];
label.color = ccc3(0, 0, 0);
CGSize size = [[CCDirector sharedDirector] winSize];
label.position = CGPointMake(size.width / 2, size.height / 2);
// [[[CCDirector sharedDirector] openGLView] addSubview:labe]; cant add to openGLView
You need to addSubview UIView components under the openGLView as the following,
[[[CCDirector sharedDirector] openGLView].superview addSubview:arrowButton];
And then, openGLView should be transparent. "Displaying an EAGLView with transparent background on a UIImageView"
EDIT:
moving the Cocos on top of the UIImagePickerController
Ok, how about the following? addSubview the cocos2d view (openGLView) on cameraOverlayView of UIImagePickerController.
[[[CCDirector sharedDirector] openGLView].superview addSubview:uip.view];
[[[[CCDirector sharedDirector] openGLView] removeFromSuperview];
uip.cameraOverlayView = [[CCDirector sharedDirector] openGLView];
Also, you need Making Cocos2d Transparent.
Related
I'm using kobold2d v2.1.0 and added AdMob Mediation SDK to my project according to Google's instructions.
I created AdViewController and added its view to rootViewController.view.
AdViewController.m:
-(void)viewDidLoad
{
[super viewDidLoad];
// Initialize the banner at the bottom of the screen.
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height -
CGSizeFromGADAdSize(kGADAdSizeBanner).height);
// Use predefined GADAdSize constants to define the GADBannerView.
self.adBanner = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
// before compiling.
self.adBanner.adUnitID = AD_UNIT_ID;
self.adBanner.delegate = self;
[self.adBanner setRootViewController:self];
[self.view addSubview:self.adBanner];
self.adBanner.center =
CGPointMake(self.view.center.x, self.adBanner.center.y);
[self.adBanner loadRequest:[self createRequest]];
}
AppDelegete.m
-(void) initializationComplete
{
adViewController = [[AdViewController alloc] init];
[self.rootViewController.view addSubview:adViewController.view];
}
-(id) alternateView
{
// we want to be a dummy view the self.view to which we add the glView plus all other UIKit views
KKAppDelegate* appDelegate = (KKAppDelegate*)[UIApplication sharedApplication].delegate;
// add a dummy UIView to the view controller, which in turn will have the glView and later other UIKit views added to it
CGRect bounds = [appDelegate.window bounds];
UIView* dummyView = [[UIView alloc] initWithFrame:bounds];
[dummyView addSubview:[CCDirector sharedDirector].view];
return dummyView;
}
When I tap Ad area before Ad is displayed, remotely located CCMenuItem is pressed as if I tap the CCMenuItem.
Please look at this
I think this is related to this question, but I do not know how to resolve it.
Please let me know how to prevent CCMenuItem to respond to the tap of the remote location.
I'm sorry I'm not good at English.
I solved the problem by changing -(CCMenuItem *) itemForTouch: (UITouch *) touch Method in CCMenu.m.
-(CCMenuItem *) itemForTouch: (UITouch *) touch
{
//CGPoint touchLocation = [touch locationInView: [touch view]];
/*changed*/ CGPoint touchLocation = [touch locationInView: [CCDirector sharedDirector].view];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
.......
}
Before change [touch view] is AdView, so CCMenu got wrong coordinates.
I was referring to this question.
I want to use UITextField in Cocos2d, so I wrote the following code:
// In HelloWorldLayer...
-(id) init
{
if( self=[super init] )
{
CGSize winSize = [[CCDirector sharedDirector] winSize];
UIView *view = [[CCDirector sharedDirector] openGLView];
// CGRect rect = CGRectMake(60.0, 45.0, 360.0, 25.0);
CGPoint pos1 = ccp( 60.0, winSize.height - 45.0 );
CGPoint pos2 = ccp( 360.0, 25.0 );
pos1 = [[CCDirector sharedDirector] convertToUI:pos1];
pos2 = [[CCDirector sharedDirector] convertToUI:pos2];
// UITextField *_tField; it is declared in field
_tField = [[UITextField alloc] initWithFrame:CGRectMake(pos1.x, pos1.y, pos2.x, pos2.y)];
_tField.backgroundColor = [UIColor whiteColor];
_tField.borderStyle = UITextBorderStyleRoundedRect;
_tField.returnKeyType = UIReturnKeyDone;
_tField.delegate = self;
[view addSubview:_tField];
}
return self;
}
Now I have problem like this:
http://serviceapi.nmv.naver.com/flash/convertIframeTag.nhn?vid=F25D1799886DD32FDA54463C2458197E492A&outKey=V1269fde60a3180c7d50216a83d22b478817df9b6060a511adffa16a83d22b478817d&width=720&height=438
How can I solve this problem...?
By the problem, do you mean why the text is not being written horizontally?
Because the text is actually written horizontally, maybe you have something else that is affecting the code.
I want to create a toolbar (initially hidden) with items that can be dragged. If a button is tapped, the toolbar will appear buttom-up (just like the animation of keyboard). I just like to ask how to do it in cocos2d.
Thanks for the response!
I used this code for drawer open and close.
-(void)showMyCocos2DDrawer
{
CGSize s = [[CCDirector sharedDirector] winSize];
self.position = ccp(-s.width,0.0f); //do this in ur init method :)
CGPoint pos =ccp(0.0f, 0.0f );
id moveTo = [CCMoveTo actionWithDuration:0.5f position:pos];
id calFun = [CCCallFunc actionWithTarget:self selector:#selector(animDone)];
id seq = [CCSequence actions:moveTo, calFun, nil];
[self runAction:seq];
}
-(void)hideCocos2DDrawer
{
CGSize s = [[CCDirector sharedDirector] winSize];
CGPoint pos =ccp(-s.width, 0.0f);
id moveTo = [CCMoveTo actionWithDuration:0.3f position:pos];
id calFun = [CCCallFunc actionWithTarget:self selector:#selector(goBack)];
id seq = [CCSequence actions:moveTo, calFun, nil];
[self runAction:seq];
}
-(void) animDone
{
//write in code here..
}
-(void)goBack
{
//write out code here..
}
I need help figuring out the rotation of a sprite with my finger. The sprite is rotating fine but on the initial touch of my finger it somehow rotates by a few degrees by itself.
Also the rotations only work when the finger is rotated around the center of the sprite.
I am trying to simulate the bicycle wheel and have a gear sprite and a pedal sprite as a child of the gear sprite. I want the bicycle wheel to rotate as I touch the pedal and rotate it. I haven't got that far yet. Right now I am trying to figure out how to get rid of the initial shift the gear makes.
Here is the complete code
#import "BicycleScene.h"
#implementation BicycleScene
+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
BicycleScene *layer = [BicycleScene node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
-(id) init
{
if ((self=[super init])) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
//place the bicycle sprite
bicycleSprite = [CCSprite spriteWithFile:#"bike_gear.png"];
bicycleSprite.position = ccp(bicycleSprite.contentSize.width/2 + 100, winSize.height/2);
[self addChild:bicycleSprite z:1];
//place the pedal sprite
pedalSprite = [CCSprite spriteWithFile:#"bike_pedal.png"];
[bicycleSprite addChild:pedalSprite z:1];
pedalSprite.position = ccp(150, 15);
//enable touch
self.isTouchEnabled = YES;
[self schedule:#selector(gameLoop:) interval:.1/100];
}
return self;
}
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"Touch begun");
}
-(void)gameLoop:(ccTime) dt{
bicycleSprite.rotation = cocosAngle;
}
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"Touch moved");
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
CGPoint previousLocation = [touch previousLocationInView:[touch view]];
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint previousTouchingPoint = [[CCDirector sharedDirector] convertToGL:previousLocation];
CGPoint vector = ccpSub(touchingPoint, bicycleSprite.position);
CGFloat rotateAngle = -ccpToAngle(vector);
previousAngle = cocosAngle;
cocosAngle = CC_RADIANS_TO_DEGREES( rotateAngle);
//bicycleSprite.rotation = cocosAngle;
}
#end
I am slightly confused if the line:
CGPoint vector = ccpSub(touchingPoint, bicycleSprite.position);
should actually be:
CGPoint vector = ccpSub(touchingPoint, previousTouchingPoint );
I tried that as well but it didn't work.
I have also uploaded my complete xcodeproj to 4shared for anyone who wants to take a look here: http://www.4shared.com/file/5BaeW4oe/Healthy.html
#interface MainScene : CCLayer {
CCSprite *dial;
CGFloat dialRotation;
}
+ (id)scene;
#end
---------------------------------
#implementation MainScene
+ (id)scene
{
CCScene *scene = [CCScene node];
CCLayer *layer = [MainScene node];
[scene addChild:layer];
return scene;
}
- (id)init
{
if((self = [super init])) {
CCLOG(#"MainScene init");
CGSize size = [[CCDirector sharedDirector]winSize];
dial = [CCSprite spriteWithFile:#"dial.png"];
dial.position = ccp(size.width/2,dial.contentSize.height/2);
[self addChild:dial];
self.isTouchEnabled = YES;
[self scheduleUpdate];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)update:(ccTime)delta
{
dial.rotation = dialRotation;
}
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
//acquire the previous touch location
CGPoint firstLocation = [touch previousLocationInView:[touch view]];
CGPoint location = [touch locationInView:[touch view]];
//preform all the same basic rig on both the current touch and previous touch
CGPoint touchingPoint = [[CCDirector sharedDirector] convertToGL:location];
CGPoint firstTouchingPoint = [[CCDirector sharedDirector] convertToGL:firstLocation];
CGPoint firstVector = ccpSub(firstTouchingPoint, dial.position);
CGFloat firstRotateAngle = -ccpToAngle(firstVector);
CGFloat previousTouch = CC_RADIANS_TO_DEGREES(firstRotateAngle);
CGPoint vector = ccpSub(touchingPoint, dial.position);
CGFloat rotateAngle = -ccpToAngle(vector);
CGFloat currentTouch = CC_RADIANS_TO_DEGREES(rotateAngle);
//keep adding the difference of the two angles to the dial rotation
dialRotation += currentTouch - previousTouch;
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
EDIT: I am sorry I can't get this to stay in code mode. But this is the solution and it should be easy to implement in your code. cheers!
In ccTouchesBegan, use the same code as what you've got in ccTouchesMoved to determine the angle, then move to that angle using CCRotateTo. You will need some handling of the user moving his finger while CCRotateTo is active, perhaps stopping the current action and kicking off another one to move to the new angle.
I am a newbie at Cocos2D and i'm just getting started. I have set up a scene, and I have to change the color of a line in Cocos2D. Please can anyone help me out with this? Any help will be appreciated.
This should work:
-(id) init{
if((self = [super init])){
//CGSize winSize = [[CCDirector sharedDirector] winSize];
naughtytoucharray = [[NSMutableArray alloc] init];
self.isTouchEnabled = YES;
CCMenuItem *starMenuItem = [CCMenuItemImage itemFromNormalImage:#"ButtonStar.png" selectedImage:#"ButtonStarSel.png"
target:self selector:#selector(starButtonTapped:)];
CCMenuItem *starMenuItem1 = [CCMenuItemImage itemFromNormalImage:#"ButtonPlus.png" selectedImage:#"ButtonPlusSel.png"
target:self selector:#selector(starButtonTapped1:)];
CCMenuItem *menuItem1 = [CCMenuItemImage itemFromNormalImage:#"Button1.png" selectedImage:#"Button1Sel.png" target:self selector:#selector(button1Tapped:)];
CCMenuItem *menuItem2 = [CCMenuItemImage itemFromNormalImage:#"Button2.png" selectedImage:#"Button2Sel.png" target:self selector:#selector(button2Tapped:)];
CCMenuItem *menuItem3 = [CCMenuItemImage itemFromNormalImage:#"Button3.png" selectedImage:#"Button3Sel.png" target:self selector:#selector(button3Tapped:)];
_scoreLabel = [CCLabelTTF labelWithString:#" MyScore" dimensions:CGSizeMake(150, 30) alignment:UITextAlignmentLeft fontName:#"Arial" fontSize:15];
_scoreLabel.color = ccc3(255, 0, 0);
[_scoreLabel setPosition:ccp(80, 300)];
CCMenu *starMenu = [CCMenu menuWithItems:starMenuItem, nil];
CCMenu *starMenu1 = [CCMenu menuWithItems:starMenuItem1, nil];
CCRadioMenu *radioMenu = [CCRadioMenu menuWithItems:menuItem1, menuItem2, menuItem3, nil];
starMenuItem.position = ccp(60, 60);
starMenuItem1.position = ccp(60, 120);
radioMenu.position = ccp(120, 120);
[radioMenu alignItemsVertically];
radioMenu.selectedItem_ = menuItem1;
[menuItem1 selected];
starMenu.position = CGPointZero;
starMenu1.position = CGPointZero;
[self addChild:starMenu];
[self addChild:starMenu1];
[self addChild:radioMenu];
[self addChild:_scoreLabel];
}
[_scoreLabel setString:[NSString stringWithFormat:#"MyScore: %d", score]];
return self;
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint new_location = [touch locationInView:[touch view]];
new_location = [[CCDirector sharedDirector] convertToGL:new_location];
CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
[naughtytoucharray addObject:NSStringFromCGPoint(new_location)];
[naughtytoucharray addObject:NSStringFromCGPoint(oldTouchLocation)];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
exit(0);
NSLog(#"exit");
}
else if(buttonIndex == 1)
{
//CCScene *scene = [CCScene node];
[[CCDirector sharedDirector] replaceScene:[GameScene scene1]];
NSLog(#"Play again");
}
}
-(void)draw
{
glEnable(GL_LINE_SMOOTH);
for (int i = 0; i < [naughtytoucharray count]; i+=2)
{
CGPoint start = CGPointFromString([naughtytoucharray objectAtIndex:i]);
CGPoint end = CGPointFromString([naughtytoucharray objectAtIndex:i+1]);
ccDrawLine(start, end);
glColor4ub(200, 120, 120, 255);