我正在使用 cocos2d 开发 iPhone 2d 游戏。我需要一个选择器。是否可以在 cocos2d 中使用 Picker !如果可能的话,请告诉我,如何在 cocos2d 中使用 Picker?
Nahid
问问题
4512 次
1 回答
5
是的,您可以将基于标准 UIView 的类与 Cocos2D 类混合和匹配。
在您的应用程序委托中,当您启动 Director 时,您创建了一个 UIWindow 并将 Director 附加到它。您还可以在 appdelegate 中保存对窗口的引用。现在您可以创建 UIViews 并将其添加到 Window,以及像往常一样通过 director 操作 cocos2d 节点。
从这里开始,只需创建一个 UIPickerView 并将其添加到窗口中。配置 UIPickerView 本身就是一项完整的任务...... Nitrex88 有一个关于这个主题的好视频 。此外,查看UICatalog以获得一个可靠的示例,不仅是 UIPickerView,还有更多 UIView 子类。
这是一个向 cocos2d 应用程序添加一个简单的 UIPicker 的示例:
#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
于 2009-05-21T06:28:12.967 回答