如何将 Sprite 类与 CGRect 一起使用

发布于 2024-10-29 21:11:01 字数 1095 浏览 1 评论 0原文

我对编程(尤其是 Objective-C)非常陌生,想知道如何以绘制的 CGRect 的形式移动精灵。我在绘制 CGRect 时遇到问题,并且当我尝试移动 CGRect 时陷入困境。到目前为止,这是我的代码。这段代码中有一个 sprite 类(objective-C UIView 子类)和一个视图控制器。所有这些代码显示的是我如何绘制 CGRect。任何帮助或想法将不胜感激。

//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;
}

I am very new to programming (especially objective-C) and would like to know how to move a sprite in the form of a drawn CGRect. I am having trouble drawing the CGRect, and get majorly stuck when I try to move the CGRect. Here is my code so far. There is a sprite class (objective-C UIView subclass), and a view controller in this code. All this code shows is how I drew my CGRect. Any help or ideas would be greatly appreciated.

//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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人间☆小暴躁 2024-11-05 21:11:01

两点

1>你还没有

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

在你的 init 方法中写你应该写它

2> 你给出的行

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

x = 150 y = 100 并且高度和宽度是 0...我认为你错误地交换了它...你图像的高度和图像的宽度现在是 0...

Two point

1> you haven't write

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

in your init method you should write it

2> The line

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

you have given x = 150 y = 100 and height and width is 0... I think you swap it by mistake... you image's height and image's width is 0 right now...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文