2013-03-20 41 views
0

我正尝试使用加速度计在圆形边界内移动精灵(球)。我可以毫无困难地将精灵​​(球)移动到空白屏幕上。但是现在我需要设置一个圆形边界,以便在圆内限制运动。我有一个背景图像,这是一种图像的圆形类型。如果所有精灵(球)正在移动,那么它应该只在这个圆形图像内,我不希望球移出圆形图像。我想知道如何在圆形背景图像上设置边界。请帮帮我。谢谢。如何使用cocos2d中的加速度计在圆形边界内移动精灵

代码:

if((self=[super init])) 
{ 
    bg=[CCSprite spriteWithFile:@"challenge-game.png"]; 
     bg.position=ccp(240,160); 
     bg.opacity=180; 
     [self addChild:bg]; 

    ball1=[CCSprite spriteWithFile:@"ball.png"]; 
    ball1.position=ccp(390,180); 
    [self addChild:ball1]; 

    ball2=[CCSprite spriteWithFile:@"ball1.png"]; 
    ball2.position=ccp(240,20); 
    [self addChild:ball2]; 

    ball3=[CCSprite spriteWithFile:@"ball2.png"]; 
    ball3.position=ccp(100,180); 
    [self addChild:ball3]; 

    size = [[CCDirector sharedDirector] winSize]; 
    self.isAccelerometerEnabled=YES; 
    [self scheduleUpdate]; 
    } 

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
    { 
    ballSpeedY = acceleration.x*20; 
    ballSpeedX = -acceleration.y*10;  
    } 

-(void)updateBall1 { 
float maxY1 = size.height - ball1.contentSize.height/2; 
float minY1 = ball1.contentSize.height/2; 
float newY1 = ball1.position.y + ballSpeedY; 
newY1 = MIN(MAX(newY1, minY1), maxY1); 

float maxX1 = size.width - ball1.contentSize.width/2; 
float minX1 = ball1.contentSize.width/2; 
float newX1 = ball1.position.x + ballSpeedX; 
newX1 = MIN(MAX(newX1, minX1), maxX1); 
NSLog(@"MAXY: %f MINY: %f NEWY: %f",maxY1,minY1,newY1); 
ball1.position = ccp(newX1, newY1);  
} 

-(void)setPosition:(CGPoint)position 
{ 
float dx=position.x-240; 
float dy=position.y-160; 
float r=bg.contentSize.width/2-ball1.contentSize.width/2; 
if((dx*dx+dy*dy)>(r*r)) 
{ 
    position.x=100; 
    position.y=100; 
} 
[super setPosition:position]; 
} 

回答

0

不知道什么剩余的要求,但你有没有考虑使用Box2D的和基于加速度计重力玩弄围绕移动精灵?在这种情况下,您可以简单地创建一个代表允许的世界的圆形对象。

如果这不合适,我猜你可以强制你的精灵位置始终在你的圆圈内(重写setPosition并确保x和y总是带有由数学公式给出的圆)。

像这样的东西(请注意,你需要决定在何处放置你的球,当它是关于圆外移动):在上面的代码

-(void) setPosition:(CGPoint)pos 
{ 
    float dx = pos.x - centerX; 
    float dy = pos.y - centerY; 

    if ((dx * dx + dy * dy) > (r * r)) 
    { 
     // Change pos.x and pos.y to stay within the circle. 
    } 

    [super setPosition:pos]; 
} 

而且,圆的半径(r )实际上应该是你的目标圆的半径减去你的球的半径。

又如使用update

// In your scene/layer initialization register update callback, like this 
[self scheduleUpdate]; 


// Then modify your update method 
- (void) update:(ccTime)delta 
{ 
    // Your update logic 

    // Here you take care of your sprite. 
    float dx = ballSprite.position.x - centerX; 
    float dy = ballSprite.position.y - centerY; 

    if ((dx * dx + dy * dy) > (r * r)) 
    { 
     // Change the position of ballSprite to stay within the circle. 
     ballSprite.position = ccp(newX, newY); 
    } 
} 
+0

感谢您的答复。没有它的正义COCOS2D。我如何覆盖setPosition方法?它属于哪一类?帮我。提前致谢。 – BudHawk 2013-03-22 04:40:02

+0

'CCNode'有一个属性'position',它由内部'setPosition:(CGPoint)'方法处理。如果您要将CCSprite扩展到您想要限制的对象,只需添加一个方法' - (void)setPosition:(CGPoint)position' implementation file(.m)。 'position'参数是精灵想要移动的目标位置。现在,确保'position.x'和'position.y'在一个圆圈内。如果不是,则更正x和y并调用'[super setPosition:position]'。 – gberginc 2013-03-22 06:00:27

+0

我正在使用CCSprite添加图像。 – BudHawk 2013-03-22 06:39:05