2011-04-05 40 views
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; 
} 

回答

0

兩點

1>你有沒有在你的init方法寫

`if((self = [super initWithFrame:frame]))` 

,你應該把它寫

2>線

Sprite* mySprite = [[Sprite alloc] initWithFrame:CGRectMake(150, 100, 0, 0)]; 

你已經給出x = 150 y = 100高度和寬度是0 ...我認爲你錯了它...你的形象的高度和形象的寬度是0現在...

+0

好吧,我會嘗試。感謝您的幫助 – skiboi38 2011-04-06 02:16:37

相關問題