开发者

Cocos2D TouchesEnded not allowing me to access sprites?

开发者 https://www.devze.com 2022-12-30 15:29 出处:网络
Thanks so much for reading! - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UITouch * touch = [touches anyObject];

Thanks so much for reading!

- (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 ++;            
    }
}

For some reason, ccTouchesEnded isn't allowing me to access my "sprite". I also tried to use CGRectMake like so :

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

But I couldn't access my sprites position or hei开发者_StackOverflow社区ght. I keep getting "sprite" undeclared when it is declared in the init method, and added to the child.

Please help!! I'm sure i'm missing something really simple here.


"sprite" is probably declared locally in init method but not a member of the class.

One solution would be to give sprite a tag:

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

Later on you can access that sprite by using:

CCSprite* sprite = [self getChildByTag:123];

It is similar to removing a child by tag: 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


Have you tried,

  • [self sprite]
  • self.sprite
  • Checked if sprite is declared as a property and have you synthesized it?
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号