开发者

Optimizing drag control in Cocos2D-iphone

开发者 https://www.devze.com 2023-03-23 19:15 出处:网络
I\'m writing a simple side scrolling iOS game, the control is the same as the one in Flying Hamster, i.e. you drag your finger to move the main character to anywhere on the screen.

I'm writing a simple side scrolling iOS game, the control is the same as the one in Flying Hamster, i.e. you drag your finger to move the main character to anywhere on the screen. Now the game is almost done, but I have one serious trouble: All the enemy and background objects move smoothly, EXCEPT the main character :(

Actually the game's overall frame is always above 40fps, just the main character is moving not smoothly as if it's below 10fps. Could you please take a look into my code below and tell me what's wrong? Any help will be appreciated

Here's my code

    -(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent*)event{

    if (isGameOver == YES || isGamePause == YES) {    
        return;    
    }             
    UITouch *touch = [touches anyObject];    
    CGPoint touchLocation = [touch locationInView: [touch view]]; 
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];    
    CGPoint diff = ccpSub(touchLocation,before开发者_StackOverflowPosTouch);    
    beforePosTouch = touchLocation;      
    CGPoint positionOfPlayer = player.position;    
    CGPoint newPos = ccpAdd(positionOfPlayer, diff);    
    if (newPos.x <playerWidth + leftLimit) {    
        return;    
    } 

    else if (newPos.x > rightLimit - playerWidth)

    {    
        return;    
    }

    if (newPos.y < playerHeight) {    
        return;    
    }    
    else if (newPos.y > screenSize.height - playerHeight)    
    {    
        return;
    }    
    [player setPosition:newPos];          
    }    
}

-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    
    UITouch *touch = [touches anyObject];    
    beforePosTouch = [touch locationInView:[touch view]];    
    beforePosTouch = [[CCDirector sharedDirector] convertToGL:beforePosTouch];    
}


This code can not be the bottleneck of your program. You have to find the bottleneck. The part of the code that is really important for you game performance.

0

精彩评论

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