How to align a sprite?

Discussion in 'iOS Programming' started by azg442, May 17, 2009.

  1. azg442 macrumors newbie

    Joined:
    May 10, 2009
    #1
    In cocos2d does anyone know how to center a sprite? Right now, I have a sprite that moves to where you touch on the screen. The problem is that the sprite is aligned to the lower left corner. This means that instead of moving down, if you touch just a little over the bottom the sprite will move up. Thanks in advance!

    Here is my code...

    Code:
    - (BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
    	UITouch *touch = [touches anyObject];
    	CGPoint point = [touch locationInView: [touch view]];
    	
    	[mainSprite do:[MoveTo actionWithDuration:0.5 position:ccp(point.x, 480 - point.y)]];
    	
    	return YES;
    }
    
     
  2. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #2
    in cocos the bottom left pixel is (0,0) as opposed to the top left. All sprites have a centred transform anchor by default, so setting a sprite's x to 160 and its y to 240 would centre it. The reason you are having the problem you are is because you are minusing the touches y from 480 (which to cocos is the top row of pixels). If you replace your code with the following it will be fine:-

    Code:
    - (BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *) event 
    {
    	UITouch *touch = [touches anyObject];
    	CGPoint point = [touch locationInView: [touch view]];
    	
    	[mainSprite do:[MoveTo actionWithDuration:0.5 position:ccp(point.x, point.y)]];
    	
    	return YES;
    }
    
     
  3. azg442 thread starter macrumors newbie

    Joined:
    May 10, 2009
    #3
    Thanks for the help but still does not work. The sprite now moves away from the touch. Here is my full code...

    Code:
    - (BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
    	UITouch *touch = [touches anyObject];
    	CGPoint point = [touch locationInView: [touch view]];
    	
    	[mainSprite do:[MoveTo actionWithDuration:0.5 position:ccp (point.x, 480 - point.y)]];
    	
    	return YES;
    }
    
    - (BOOL) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *) event {
    	UITouch *touch = [touches anyObject];
    	CGPoint point = [touch locationInView: [touch view]];
    	
    	[mainSprite setPosition:ccp(point.x, point.y)];
    	
    	
    	return YES;
    }
    
    - (BOOL) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *) event {
    	return YES;
    }
     
  4. azg442 thread starter macrumors newbie

    Joined:
    May 10, 2009
    #4
    I got it working! For anyone that wants to know here is the code...

    Code:
    [mainSprite setTransformAnchor:ccp(24.0, 64.5)];
    24 is half of the sprites width
    64.5 is half of the sprites height
     
  5. mccannmarc macrumors 6502

    mccannmarc

    Joined:
    Aug 15, 2008
    Location:
    Manchester, UK
    #5
    Any sprites transform anchor is ALWAYS centred by default in cocos, did you set it to something different elsewhere in your code? Ive messed with cocos since version 4.x and its always been that way up til the latest 7.2 that we are using for one of our games at the minute.

    Your code may work but it looks like something isn't right overall, you really shouldn't be minusing the touches y position from 480. I suppose theres no point fixing whats not broken but if you have any issues with positioning etc later on i'd check this out again

    Either that or you are using an old version of cocos2d, its definately worth updating it if you are, you shouldn't even be callng the do: method on sprites anyway, you should be using runAction:
     

Share This Page