开发者

Cocos2d schedule method in NSObject

It's me again with a Cocos2D problem. :-D

I create the main character of my upcoming game as a sublclass of NSObject. This class has a property for the Sprite, Spritesheet and all that stuff. But now I have the following problem.

I want to schedule a 开发者_开发百科method for animating the sprite. That action which should be scheduled every second. But because I'm using a subclass of NSObject to hold all my data, I cannot use [self schedule:@selector(action:) interval:1.0]. Is there another way? I don't want to use NSTimer, because I then can't you the CCDirector anymore to break the game. It'd really be helpful if you could help me. :-D

Sandro Meier


Lots of ways to skin that cat, but here are a few:

  • Subclass CCNode instead of NSObject and call [yourClass schedule: interval:]
  • Expose a simplified selector that internally calls [self.sprite schedule: interval:]
  • Externally call [yourClass.sprite schedule: interval:]
  • Internally call [self.sprite schedule: interval:]


//SomeObject.h
@interface SomeObject : NSObject 
{
}
-(void) initObject;
@end    

//SomeObject.m
#import "cocos2d.h"
#import "SomeObject.h"
@implementation SomeObject

-(id) init
{
    return [super init];
}

-(void) initObject
{
    [[CCScheduler sharedScheduler] scheduleUpdateForTarget:self priority:0 paused:NO];
}

-(void) update:(ccTime) dt
{
}

-(void) dealloc
{
    [[CCScheduler sharedScheduler] unscheduleUpdateForTarget:self];
    [super dealloc];
}
@end

Also look at this

[[CCScheduler sharedScheduler] scheduleSelector:@selector(ping) forTarget:self interval:2.f paused:NO];

-(void) ping
{
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜