我已经在这段代码上工作了很长一段时间,但它只是感觉向前迈出了一步,后退了两步.我希望有人可以帮助我.
我正在使用Sprite Kit,所以我有一个Scene文件来管理渲染,UI和触摸控件.我有一个像这样的相机功能的SKNode:
_world = [[SKNode alloc] init];
[_world setName:@"world"];
[self addChild:_world];
Run Code Online (Sandbox Code Playgroud)
我正在使用UIGestureRecognizer,所以我添加了我需要的那些:
_panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePanFrom:)];
[[self view] addGestureRecognizer:_panRecognizer];
_pinchRecognizer = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
[[self view] addGestureRecognizer:_pinchRecognizer];
Run Code Online (Sandbox Code Playgroud)
平移工作正常,但不是很好.捏是真正的问题.捏的想法是抓住屏幕中心的一个点,将该点转换为世界节点,然后在放大时移动到它.这是捏的方法:
-(void) handlePinch:(UIPinchGestureRecognizer *)sender {
if (sender.state == UIGestureRecognizerStateBegan) {
_tempScale = [sender scale];
}
if (sender.state == UIGestureRecognizerStateChanged) {
if([sender scale] > _tempScale) {
if (_world.xScale < 6) {
//_world.xScale += 0.05;
//_world.yScale += 0.05;
//[_world setScale:[sender scale]];
[_world setScale:_world.xScale += 0.05];
CGPoint screenCenter = CGPointMake(_initialScreenSize.width/2, _initialScreenSize.height/2);
CGPoint newWorldPoint = …
Run Code Online (Sandbox Code Playgroud) 我最近意识到使用self.whatever = thing从init内部访问类的属性不是正确的形式,而是应该使用_whatever = thing直接访问属性.当我为基类设置值时,这很好用.但是,当我尝试在子类的init内设置值时,我收到错误"使用未声明的标识符_whatever".使用self.w从子类的init内部工作正常.
为了清楚起见,对于@property int在基础对象的接口中声明的任何内容,这将编译:
-(id) init {
self = [super init];
if (self) {
[self createName];
self.whatever = 100;
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
而这不是:
-(id) init {
self = [super init];
if (self) {
[self createName];
_whatever = 100;
}
return self;
}
Run Code Online (Sandbox Code Playgroud)
我想我在这里误解了一些东西.我试着搜索我做错了什么,但我不知道要搜索的正确单词.
根据要求,基类的标头:
#import <Foundation/Foundation.h>
@interface DTCharacter : NSObject
@property BOOL isIdle;
@end
Run Code Online (Sandbox Code Playgroud)
我正在使用自动合成,因此在基类的实现中没有任何关于此变量的信息.
此外,子类的头文件未提及或引用该变量.
我正在尝试在子类的实现中分配它:
-(id) init {
self = [super init];
if (self) {
[self createName];
_isIdle = YES; //says use of undeclared …
Run Code Online (Sandbox Code Playgroud) 这就是我想要做的.我有一个简单的NS_OPTIONS设置
typedef NS_OPTIONS(NSUInteger, BuildingsPresent) {
NoBuildings = 0,
LadderPresent = 1 << 0,
WallPresent = 1 << 1,
RoomPresent = 1 << 2,
RoomUpgradePresent = 1 << 3
};
Run Code Online (Sandbox Code Playgroud)
我正在设置这样的值:
self.buildingPresent |= LadderPresent | WallPresent;
Run Code Online (Sandbox Code Playgroud)
这似乎工作正常,但现在我想一次性测试if语句中的标志,如下所示:
if (self.buildingPresent & LadderPresent & WallPresent) {
Run Code Online (Sandbox Code Playgroud)
这不能正常工作.如果我将它分解为多个if语句,它似乎确实有效:
if (self.buildingPresent & LadderPresent) {
if (self.buildingPresent & WallPresent) {
//do something
}
}
Run Code Online (Sandbox Code Playgroud)
我也想正确测试缺少某些东西,所以写这个的理想方式是:
if (!(self.buildingPresent & RoomPresent) && (self.buildingPresent & LadderPresent & WallPresent) {
Run Code Online (Sandbox Code Playgroud)
这在Objective-C中是否可行?难道我做错了什么?
objective-c ×3
enums ×1
if-statement ×1
inheritance ×1
ios ×1
pinch ×1
sprite-kit ×1
subclass ×1
zoom ×1