0

我从 CoCos2d 开发开始,我认为我在节点空间和操作方面存在一些问题,这是我的代码,虽然我在运行代码时似乎没有任何错误,但任何评论都表示赞赏:

    +(id) create {
    return [[self alloc] init];}

    -(id) init {
        if ((self = [super init]))
        {
        CGSize scSize = [[CCDirector sharedDirector] winSize];

    self.position = ccp(0,scSize.height);;


    touchArea = [CCSprite spriteWithFile:@"touchArea.png"]; 
    [touchArea setAnchorPoint:ccp(0, 0.5)];
    [self addChild:touchArea];


    obj_1 = [CCSprite spriteWithFile:@"obj1.png"];
    obj_2 = [CCSprite spriteWithFile:@"obj2.png"];
    obj_3 = [CCSprite spriteWithFile:@"obj3.png"];

    obj_tab = [NSMutableArray arrayWithObjects:obj_1, obj_2, obj_3, nil];


        for (int i=0; i < obj_tab.count; i++)
        {
            CCSprite *obj = [obj_tab objectAtIndex:i];
            float offSetPos = ((float)(i+1)) / (obj_tab.count +1);
            CGPoint pos = ccp(scSize.width*offSetPos, 0);
            obj.position = pos;
            [obj setAnchorPoint:ccp(0.5, -0.25)];
            [touchArea addChild:obj];
        }

    velocidad = 3;
    destino = ccp(0,-100);
    //[self scheduleUpdate];
    [self moveIt] // <-- Edited to call another method
 }   
    return self;

}

-(void) moveit{
    CCMoveTo *moverAction = [CCMoveTo actionWithDuration:5 position:ccp(0,0)];
    CCCallFunc *onEndAction = [CCCallFunc actionWithTarget:self selector:@selector(onEndMove:)];
    CCSequence *moveSequence = [CCSequence actions: moverAction, onEndAction, nil];
    [touchArea runAction:moveSequence];
{

-(void) update:(ccTime)delta {

}

-(void)onEndMove:(id)sender{
    NSLog(@"OnEndMove fired");
}

-(void) onEnter{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES];
}

-(void) onExit {
    [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
}


-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{

    CGRect boundingBox = touchArea.boundingBox;
    boundingBox.origin = CGPointZero;

    BOOL isTouchHandled = CGRectContainsPoint(boundingBox, [touchArea convertTouchToNodeSpace: touch]);

    //NSLog(@"touchLocation y = %f", touchLocation.y);
    //NSLog(@"touchArea y = %f", touchArea.position.y);

    if (isTouchHandled){
        NSLog(@"Touch Handled");
    }

    return isTouchHandled;

}
@end
4

2 回答 2

0

-(void) update:(ccTime)delta被叫的频率是多少?(可能是从您的scheduleUpdate方法开始的。)您可能每秒调用[touchArea runAction:moveSequence];几次,这会使大量的 CCMoveTo 排队。

您可能应该将您runAction:的 s 完全移出那里,或者至少在您的update:方法中添加一些逻辑来检查是否touchArea应该启动另一个runAction:.

于 2012-06-18T22:59:46.227 回答
0

我假设上面显示的代码类 'self' 派生自 CCNode。确保 'self' 正在运行,它必须是 CCNode 的后代,并且将在添加到正在运行的节点时运行。这是 CCNode 的 onEnter(取自版本 1.0.1)中发生的情况:

-(void) onEnter
{
    [children_ makeObjectsPerformSelector:@selector(onEnter)];  
    [self resumeSchedulerAndActions];

    isRunning_ = YES;
}

这是为节点启用调度程序和操作的地方。如果它从未被调用,您的操作将不会运行。

您可以将 moveIt 代码移动到 onEnter 方法,该方法将在对象运行时调用。

-(void) onEnter{
    CCLOG(@"%@<onEnter> : invoked",self.class);
    [self moveIt];
    [super onEnter];   
}
于 2012-06-19T13:50:33.240 回答