2010-05-15 99 views
1

非常感谢您的阅读!Cocos2D TouchesEnded不允许我访问精灵?

- (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方法中声明的时候,我一直不知道“精灵”,并将其添加到子节点中。

请帮忙!!我确定我在这里错过了一些非常简单的事情。

回答

0

您是否尝试过,

  • [self sprite]
  • self.sprite
  • 经过,如果精灵被声明为一个属性,你合成的呢?
+0

我使用了Ktag方法,并解决了这个问题。非常感谢你Rob! – maiko 2010-06-03 01:06:21

0

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

一个解决办法是给精灵标签:

CCSprite* sprite = [self getChildByTag:123]; 

它类似于标签移除一个孩子:

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

以后你可以通过访问精灵 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

+0

谢谢GH! tag'n解决了我的问题 – maiko 2010-06-03 01:06:56