Cocos2D TouchesEnded不允许我访问sprite?

时间:2010-05-15 17:35:01

标签: objective-c cocos2d-iphone sprite touchesbegan touches

非常感谢您的阅读!

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];

    CGPoint location = [[CCDirector sharedDirector] convertToGL: [touch locationInView:touch.view]];
    CGRect myRect = CGRectMake(100, 120, 75, 113);

    int tjx = sprite.position.x;

    if(CGRectContainsPoint(myRect, location)) {
       tjx ++;            
    }
}

出于某种原因,ccTouchesEnded不允许我访问我的“精灵”。我也试过用 CGRectMake喜欢这样:

CGRectMake( sprite.position.x, sprite.position.y, sprite.contentSize.Width, sprite.contentSize.Height) 

但我无法访问我的精灵位置或身高。当它在init方法中声明并且添加到子进程中时,我一直得到“sprite”未声明的声明。

请帮忙!!我确定我在这里遗漏了一些非常简单的东西。

2 个答案:

答案 0 :(得分:0)

你试过吗,

  • [self sprite]
  • self.sprite
  • 检查精灵是否被声明为属性并且是否合成了它?

答案 1 :(得分:0)

“sprite”可能在init方法中声明为本地,但不是该类的成员。

一种解决方案是给sprite一个标签:

sprite.tag = 123; // any arbitrary number to identify this sprite

稍后您可以使用以下方法访问该精灵:

CCSprite* sprite = [self getChildByTag:123];

类似于通过标记删除子项: http://www.learn-cocos2d.com/knowledge-base/cocos2d-iphone-faq/learn-cocos2d-public-content/manual/cocos2d-general/14824-how-to-remove-a-child-from-the-nodescenelayer