0
我對編程(特別是Objective-C)非常陌生,並且想知道如何以CGRect的形式移動一個Sprite。我在繪製CGRect時遇到問題,並且在嘗試移動CGRect時受到嚴重卡住。這是我的代碼到目前爲止。在這個代碼中有一個精靈類(objective-C UIView子類)和一個視圖控制器。所有這些代碼顯示的是我如何繪製我的CGRect。任何幫助或想法將不勝感激。如何在CGRect中使用Sprite類
//the Sprite.h file
@interface Sprite : UIView {
UIImage* image;
}
//the Sprite.m file
-(id)initWithFrame:(CGRect)frame {
UIImage* loadedImage = [UIImage imageNamed:@"image1.png"];
CGRect rect = CGRectMake(frame.origin.x, frame.origin.y, loadedImage.size.width, loadedImage.size.height);
self = [super initWithFrame:rect];
image = [loadedImage retain];
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
return self;
}
-(void)drawRect:(CGRect)rect {
[image drawInRect:rect];
}
//the ViewController.m file
-(void) viewWillAppear:(BOOL)animated {
// add a sprite
Sprite* mySprite = [[Sprite alloc] initWithFrame:CGRectMake(150, 100, 0, 0)];
[self.view addSubview:mySprite];
}
//the ViewController.h file
@class Sprite;
@interface Sprite2TestViewController : UIViewController {
Sprite* Sprite;
}
好吧,我會嘗試。感謝您的幫助 – skiboi38 2011-04-06 02:16:37