【发布时间】:2011-04-28 06:37:54
【问题描述】:
刚接触 Obj C 和一般编程 - 从这个网站学到了很多东西,非常感谢大家的贡献。
我的场景如下(编写一个解释有趣名字的 iPhone 游戏)
在我的主游戏循环(在我的视图控制器中)中,如果满足某个条件,我会创建一个敌人 - 樱桃炸弹
if (bounceCounterGlobal % 2 == 0 && bounceCounterGlobal > 1 && cherryBombSwitch == 0){
[self addCherryBomb];
}
addCherryBomb方法如下:
-(void) addCherryBomb{
CherryBomb *myCherryBomb = [[CherryBomb alloc] init];
[cherryBombArray insertObject:myCherryBomb atIndex:0];
[myCherryBomb release];
[[cherryBombArray objectAtIndex:0] initializeCherryBomb];
[self.view addSubview:[[cherryBombArray objectAtIndex:0] cherryBombView]];
cherryBombSwitch = 1;
}
CherryBomb 头文件很短:
#import <Foundation/Foundation.h>
#import "SimpleGameViewController.h"
@interface CherryBomb : NSObject {
UIImageView *cherryBombView;
NSTimer *cherryBombDetonateTimer;
NSTimer *cherryBombMoveTimer;
}
@property (nonatomic, retain) UIView *cherryBombView;
-(void) initializeCherryBomb;
-(void) detonateCherryBomb;
-(void) moveCherryBomb;
@end
我想要做的是当樱桃炸弹引爆时(在cherryBomb 对象中确定),我希望该对象从cherryBombArray 中移除,它是视图控制器的一个ivar。
我尝试调用视图控制器类方法来执行此操作 - 但我无法访问视图控制器的 ivars(因为它是一个类方法)。我不知道如何与视图控制器类进行通信以告诉它删除分解的对象。
@implementation CherryBomb
...
-(void) detonateCherryBomb{
NSLog(@"KABOOM!");
cherryBombDetonateTimer = nil;
[cherryBombMoveTimer invalidate];
[cherryBombView removeFromSuperview];
//I would like to remove this object from the view controller's cherryBombArray
}
@end
非常感谢您的帮助。提前谢谢!
【问题讨论】:
标签: iphone objective-c variables instance delegation