Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialrishavatreya
4,945 PointsHow to call a method each second?
I have a number-role nodes( 4 numbers nodes). I have created a method that changes the position of the nodes each time it called. I use scheduledTimerWithTimeInterval method to call the method each second. what I found when running the code is the older nodes did not remove. it keeps adding new nodes , within few minutes no of nodes becomes 100+. How to solve the problem? Is there any easy and short way to code for this? ``` -(id)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) { /* Setup your scene here */
SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"background_1.png"];
background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:background];
//adding numbernode
[NSTimer scheduledTimerWithTimeInterval: 5.0 target: self
selector: @selector(numberNodes:) userInfo: nil repeats: YES];
// feed button
RAFeedButtonNode *feedButton = [RAFeedButtonNode feedButtonAtPosition:CGPointMake(CGRectGetMinX(self.frame)+30, CGRectGetMinY(self.frame)+120)];
[self addChild:feedButton];
}
return self;
}
-(void)numberNodes:(NSTimer *)t{
NSArray *randomPositionArray = [NSArray arrayWithObjects:[NSNumber numberWithFloat:120.00],[NSNumber numberWithFloat:163.00],[NSNumber numberWithFloat:207.00],[NSNumber numberWithFloat:250.00], nil];
id randomObject = nil;
if ([randomPositionArray count] > 0){
int randomIndex = arc4random()%[randomPositionArray count];
randomObject = [randomPositionArray objectAtIndex:randomIndex];
}
//numberOne node
RANumberButtonNode *numberOne = [RANumberButtonNode spriteNodeWithImageNamed:@"number_1_button_1.png"];
numberOne.position =CGPointMake(CGRectGetMaxX(self.frame)-67, CGRectGetMinY(self.frame)+[randomObject floatValue]);
numberOne.name =@"NumberOne";
[self addChild:numberOne];
int i=0;
for (i=0; i<4; i++) {
if (randomPositionArray[i]==randomObject) {
i++;
}
//numberTwo node
RANumberButtonNode *numberTwo = [RANumberButtonNode spriteNodeWithImageNamed:@"number_2_button_1.png"];
numberTwo.position =CGPointMake(CGRectGetMaxX(self.frame)-67, CGRectGetMinY(self.frame)+[randomPositionArray[i] floatValue]);
numberTwo.name =@"NumberTwo";
[self addChild:numberTwo];
i++;
if (randomPositionArray[i]==randomObject) {
i++;
}
//numberThree node
RANumberButtonNode *numberThree = [RANumberButtonNode spriteNodeWithImageNamed:@"number_3_button_1.png"];
numberThree.position =CGPointMake(CGRectGetMaxX(self.frame)-67, CGRectGetMinY(self.frame)+[randomPositionArray[i] floatValue]);
numberThree.name =@"NumberThree";
[self addChild:numberThree];
i++;
if (randomPositionArray[i]==randomObject) {
i++;
}
//numberFour node
RANumberButtonNode *numberFour = [RANumberButtonNode spriteNodeWithImageNamed:@"number_4_button_1.png"];
numberFour.position =CGPointMake(CGRectGetMaxX(self.frame)-67, CGRectGetMinY(self.frame)+[randomPositionArray[i] floatValue]);
numberFour.name =@"NumberFour";
[self addChild:numberFour];
i++;
}
}
@end
1 Answer
Stone Preston
42,016 Pointswhats the maximum number of nodes you want? maybe you could add a mutable array to your scene that stores the nodes you add every second. each time you add a node, check and see if the count of that array is greater than the max number of nodes you want to have at once. if its greater than the max number you want, remove the first item in the array and then add the new node.
that might work. not 100% sure but you could try
rishavatreya
4,945 Pointsrishavatreya
4,945 PointsHello Amit Bijlani , Ben Jakuben ;)