cocos2dを使ってiPhoneの2Dゲームを開発しています。ピッカーが必要です。cocos2d でピッカーを使用することは可能ですか ! 可能であれば、 cocos2d でピッカーを使用するにはどうすればよいですか?
4512 次
1 に答える
5
はい、標準の UIView ベースのクラスと Cocos2D クラスを組み合わせることができます。
アプリケーション デリゲートで、Director を起動したときに UIWindow を作成し、それに Director をアタッチしました。ウィンドウへの参照を appdelegate に保存することもできます。UIView を作成して Window に追加し、director を介して通常どおり cocos2d ノードを操作できるようになりました。
ここからは、UIPickerView を作成してウィンドウに追加するだけです。UIPickerView の構成は、それ自体がすべてのタスクです... Nitrex88 には、このテーマに関する優れたビデオがあります 。また、 UIPickerViewだけでなく、さらに多くの UIView サブクラスの確かな例については、UICatalogを確認してください。
簡単な UIPicker を cocos2d アプリに追加する例を次に示します。
#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 に答える