Note that there are some explanatory texts on larger screens.

plurals
  1. POCode Refactoring issue
    primarykey
    data
    text
    <p>I have game ready and now I am trying to refactor code. I have derived Spider class from CCNode and used targeted delegate method CCTargetedTouchDelegate. </p> <pre><code>@interface Spider : CCNode&lt;CCTargetedTouchDelegate&gt; { CCSprite* spiderSprite; NSString * spiderKilled; int killed; AppDelegate *del; } +(id) spiderWithParentNode:(CCNode*)parentNode; -(id) initWithParentNode:(CCNode*)parentNode; @end </code></pre> <p>On Touch spider should be killed and here goes the code:</p> <pre><code>-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint tch = [touch locationInView: [touch view]]; CGPoint touchLocation = [[CCDirector sharedDirector] convertToGL:tch]; // Check if this touch is on the Spider's sprite. BOOL isTouchHandled = CGRectContainsPoint([spiderSprite boundingBox], touchLocation); if (isTouchHandled) { j = j + 1; killed ++; [del setKilledScore:j]; [self removeChild:spiderSprite cleanup:YES]; } return isTouchHandled; } </code></pre> <p>I am adding 10 spiders in GameScene layer using: -</p> <pre><code> for(int i=0; i &lt;10 ;i++){ [Spider spiderWithParentNode:self]; } </code></pre> <p>But, unfortunately I am not able to remove spiders and giving me EXC_BAD_ACCESS error on this line: <code>[self removeChild:spiderSprite cleanup:YES];</code></p> <p>Please help me overcome this error.</p> <p>Thanks</p> <hr> <p>Update -- Spider Init code // Static autorelease initializer, mimics cocos2d's memory allocation scheme. +(id) spiderWithParentNode:(CCNode*)parentNode { return [[[self alloc] initWithParentNode:parentNode] autorelease]; }</p> <pre><code>-(id) initWithParentNode:(CCNode*)parentNode { if ((self = [super init])) { [parentNode addChild:self]; del = [[UIApplication sharedApplication] delegate]; CGSize screenSize = [[CCDirector sharedDirector] winSize]; spiderSprite = [CCSprite spriteWithFile:@"spider.png"]; spiderSprite.position = CGPointMake(CCRANDOM_0_1() * screenSize.width, CCRANDOM_0_1() * screenSize.height); [self addChild:spiderSprite]; // Manually add this class as receiver of targeted touch events. [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES]; } return self; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload