Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Cocos2d在其中一个';孩子们_Objective C_Delegates_Cocos2d Iphone_Protocols_Delegation - Fatal编程技术网

Objective c Cocos2d在其中一个';孩子们

Objective c Cocos2d在其中一个';孩子们,objective-c,delegates,cocos2d-iphone,protocols,delegation,Objective C,Delegates,Cocos2d Iphone,Protocols,Delegation,我正在为一个研究项目构建一个iPad游戏,我一直在尝试让我的一个对象(从CCSprite继承)调用CCLayer上的函数 情况: 我在我的CCLayer上有一个wordObject实例(继承自CCSprite)。当对象被触摸时,它会记录一些东西,并应该在其父对象CCLayer上执行一个函数,以创建一个新对象 到目前为止,我所做的是检测触摸并记录消息,但我找不到在CCLayer上执行该功能的方法,因此我提出了问题 当用另一种语言编程时,我只需将CCLayer指针作为参数传递给对象的init函数。我

我正在为一个研究项目构建一个iPad游戏,我一直在尝试让我的一个对象(从CCSprite继承)调用CCLayer上的函数

情况: 我在我的CCLayer上有一个wordObject实例(继承自CCSprite)。当对象被触摸时,它会记录一些东西,并应该在其父对象CCLayer上执行一个函数,以创建一个新对象

到目前为止,我所做的是检测触摸并记录消息,但我找不到在CCLayer上执行该功能的方法,因此我提出了问题

当用另一种语言编程时,我只需将CCLayer指针作为参数传递给对象的init函数。我尝试通过以下方式扩展initWithSprite函数来实现这一点:

//touch function
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
        CGPoint location = [touch locationInView: [touch view]];
        if (CGRectContainsPoint([self boundingBox], location)) {
            CCLOG(@"Woah");
            [parentLayer foundWord:@"test" atLocation:ccp(100.0f, 100.0f) withZValue:(int)1000 andPoints:100];
            CCLOG(@"Delegate should have executed");

            return YES;
        }
        CCLOG(@"Touch detected, I'm located at %@ and the touch position is %@.", NSStringFromCGRect([self boundingBox]), NSStringFromCGPoint(location));
        return NO;
    }

// Extended init function, I have no normal init function anymore (but I don't think I need that, tried it and it gave crashes
-(id)initWithSpriteFrame:(CCSpriteFrame*)spriteFrame andParent:(CCLayer *)layer{
        [super initWithSpriteFrame:spriteFrame];
        self = [super init];
        if(self != nil){
            parentLayer = layer;
        }
        return self;
    }
问题是,使用此选项后,对象不再响应触摸输入(这可能是由于我在初始化对象时出错造成的)

然而,还有另一种方法可以做到这一点,据我所知,这种方法在目标C中更好。我应该能够为我需要的函数创建一个协议,然后使用委托在我的对象中调用该函数。我为此编写的代码:

//CommonProtocols.h the file with the protocols. It is included in the CCLayer and in the object
@protocol GameplayScrollingLayerDelegate
-(void)foundWord:(NSString *)word atLocation:(CGPoint)location withZValue:(int)ZValue andPoints:(int)points;
@end

//Layer.h, subscribing the class to the protocol so it knows where it should delegate to
@interface Layer : CCLayer <GameplayScrollingLayerDelegate>  {
    //some stuff omitted due to not being relevant.
}

//Layer.m, nothing done here, I've just added the function which I described in the protocol file
-(void)foundWord:(NSString *)word atLocation:(CGPoint)location withZValue:(int)ZValue andPoints:(int)points{
    // Function gibberish doing what I want it to do
}

//Object.h create the delegate
@interface Object : GameObject <CCTargetedTouchDelegate> {
    id <GameplayScrollingLayerDelegate> delegate;
}

@property (nonatomic,assign) id <GameplayScrollingLayerDelegate> delegate;


//Object.m synthesize the delegate and try to execute the function
@synthesize delegate
//... code omitted ...
-(id)init{
    //default init stuff
    [delegate foundWord:@"test" atLocation:ccp(100.0f, 100.0f) withZValue:(int)1000 andPoints:100];
}
//CommonProtocols.h使用协议创建文件。它包含在CCLayer和对象中
@协议GameplayScrollingLayerDelegate
-(void)foundWord:(NSString*)单词atLocation:(CGPoint)带有ZValue:(int)ZValue和points:(int)points的位置;
@结束
//h,将类订阅到协议,以便它知道应该委托到哪里
@接口层:CCLayer{
//由于不相关而遗漏了一些内容。
}
//m,这里什么也没做,我只是添加了协议文件中描述的函数
-(void)foundWord:(NSString*)单词位置:(CGPoint)带有ZValue:(int)ZValue和points:(int)points的位置{
//函数胡言乱语做我想让它做的事
}
//创建委托
@界面对象:游戏对象{
id代表;
}
@属性(非原子,赋值)id委托;
//m合成委托并尝试执行该函数
@综合代表
//... 代码省略。。。
-(id)init{
//默认初始化内容
[学员创建单词:@“测试”位置:ccp(100.0f,100.0f),Z值:(int)1000,点:100];
}
我缺乏使用协议或接口的知识,这可能导致我在这个过程中遗漏了一些东西。虽然它没有给我任何错误或警告,但它没有像我希望的那样执行该函数。(我有一个使用它的演示应用程序,它工作得很好,但我就是找不到我丢失的代码)


关于如何解决这个问题有什么建议吗?感谢阅读,希望能回答我的问题

拥有一个你所做的CCSprite子类是完全可以的。这应该不是问题(仅当您未使用现有字段名时)。但是如果您的精灵有一个CCLayer作为父对象,您可以从精灵(甚至是CCNode)简单地访问它


PS:在通过指针调用某些方法之前,您不能仅通过将指针传递到某个位置来更改对象的状态。

感谢您的快速响应!你真棒,刚刚解决了我两天来一直头疼的问题。我不知道我能这么容易做到,因为对象的父对象是一个CCSpriteBatchNode,所以我不得不将其设置为self.parent.parent,但它仍然可以工作。非常感谢您提供此解决方案!
CCLayer *layer = (CCLayer*) self.parent;