0

I am trying to move a sprite(ball) within a circular boundary using an accelerometer. I am able to move the sprite(ball) on an empty screen with no difficulties. But now I need to set a circular boundary so that the movement is restricted within the circle. I have a background image which is a circle kind of an image. And if at all the sprite(ball) is moving then it should be only within this circular image and I don't want the ball to move out of the circle image. I want to know how to set the boundary on the circular background image. Please help me. Thanks.

Code:

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];
 }
4

1 回答 1

0

Not sure what the remaining requirements are, but have you considered using Box2D and playing around with the gravity based on the accelerometer to move the sprite around? In this case, you can simply make a circular object representing your allowed world.

If this is not suitable, I guess you could force the position of you sprite to be always within your circle (override setPosition and make sure that x and y are always with a circle given by the mathematical formula).

Something like this (note that you need to decide where to place your ball when it is about to move outside the circle):

-(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];
}

Also, the radius of the circle (r in the above code) should actually be the radius of your target circle minus the radius of your ball.

Another example using the 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);
    }
}
于 2013-03-21T12:25:41.527 回答