저는 cocos2d를 사용하여 iPhone 2d 게임을 개발 중입니다. 피커가 필요해. Picker를 cocos2d에서 사용할 수 있습니까? 가능한 경우 cocos2d에서 Picker를 어떻게 사용할 수 있습니까?cocos2d에서 UIPickerView를 사용할 수 있습니까?
2
A
답변
5
예, 표준 UIView 기반 클래스를 Cocos2D 클래스와 혼합하여 사용할 수 있습니다.
응용 프로그램 위임자에서 디렉터를 시작할 때 UIWindow를 만들고 디렉터를 첨부했습니다. appdelegate의 창에 대한 참조를 저장할 수도 있습니다. 이제 창에 UIView를 생성하고 추가 할 수있을뿐 아니라 Director를 통해 평소처럼 cocos2d 노드를 조작 할 수 있습니다.
여기에서 UIPickerView를 만들어 창에 추가하는 것입니다. UIPickerView 구성은 자체적으로 전체 작업입니다 ... Nitrex88은 good video on the subject입니다. 또한 UICatalog에서 UIPickerView뿐만 아니라 많은 UIView 하위 클래스의 견고한 예제를 확인하십시오.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "cocos2d.h"
@interface AppDelegate {
UIWindow *window;
NSArray *pickerValues;
}
@property (nonatomic, retain) UIWindow window;
@property (nonatomic, retain) NSArray *pickerValues;
@end
@implementation AppDelegate
@synthesize window, pickerValues;
-(void)applicationDidFinishLaunching:(UIApplication *)application {
// Create Window
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[window setUserInteractionEnabled:YES];
[window setMultipleTouchEnabled:YES];
// Set up Director and attach to window
[[Director sharedDirector] attachInWindow:window];
[[Director sharedDirector] setLandscape:YES];
[[Director sharedDirector] runWithScene:[MyScene node]];
// Create one large view and rotate the coordinates to landscape
UIView *parentView = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f,480.0f, 320.0f)];
parentView.transform = CGAffineTransformIdentity;
parentView.transform = CGAffineTransformMakeRotation(degreesToRadian(90));
parentView.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);
// Initialize picker and its data source
pickerValues = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",nil];
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0f, 195.0f, 320.0f, 125.0f)];
[pickerView setDelegate:self];
// Attach picker to parent view and parent view to window
[parentView addSubview:pickerView];
[window addSubview:parentView];
[window makeKeyAndVisible];
}
- (void) dealloc {
[window release];
[pickerValues release];
[super dealloc];
}
// ====================
// UIPicker Callbacks
// ====================
// Fire when new picker values are selected
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *numberSequence = [NSString stringWithFormat:@"Sequence: %@%@%@",
[pickerValues objectAtIndex:[thePickerView selectedRowInComponent:0]],
[pickerValues objectAtIndex:[thePickerView selectedRowInComponent:1]],
[pickerValues objectAtIndex:[thePickerView selectedRowInComponent:2]]];
NSLog(numberSequence);
}
// Number of picker wheels in the picker
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
// Number of items in each picker wheel
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [pickerValues count];
}
// Title for Row #
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [pickerValues objectAtIndex:row];
}
// Row height in pixels
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return 40.0;
}
// Column width in pixels
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return 90.0f;
}
// ====================
@end
: 여기
는적인 Cocos2D 응용 프로그램에 사소한 UIPicker 추가의 예