Jon Wei
Jon Wei

Reputation: 1481

CoCos2d Pixels and Positioning

I've been Googling and searching stack overflow all over, but is there any way at all to figure out what pixel is what point? Like, is there some app that can determine that your pointing at say, (321, 199)?

And while I'm here, in CoCos2d, I used the iPhone 5.0 simulator, so I'm assuming that it has retina display. The thing however is, when I told CoCos2d to place a sprite at like, (516, 724) , I had to cut it down to 320x480 measurements. I thought retina was 640x960.

Upvotes: 0

Views: 1628

Answers (2)

mattblessed
mattblessed

Reputation: 801

yes i just made a sample app that did exactly this

HelloWorld.m

//
//  HelloWorldLayer.m
//  FindCocosCoord
//

#import "HelloWorldLayer.h"

CCLabelTTF *touchLabelX;
CCLabelTTF *touchLabelY;
CCSprite *touchSprite;

@implementation HelloWorldLayer

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

-(id) init
{
    if( (self=[super init])) {
        self.isTouchEnabled = YES;
        touchLabelX = [CCLabelTTF labelWithString:@"X = " fontName:@"Marker Felt" fontSize:20];
        touchLabelX.position = ccp(100,50);
        [self addChild:touchLabelX];

        touchLabelY = [CCLabelTTF labelWithString:@"Y = " fontName:@"Marker Felt" fontSize:20];
        touchLabelY.position = ccp(170,50);
        [self addChild:touchLabelY];

        touchSprite = [CCSprite spriteWithFile:@"Icon-Small.png"];
        touchSprite.position = ccp(0,0);
        [self addChild:touchSprite];
    }
    return self;
}

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch* myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView: [myTouch view]];
    location = [[CCDirector sharedDirector]convertToUI:location];

    touchSprite.position = location;

    touchX = touchSprite.position.x;
    touchY = touchSprite.position.y;

    NSLog(@"Location X = %i", (int)touchX);
    NSLog(@"Location Y = %i", (int)touchY);

    NSString *touchXstring = [NSString stringWithFormat:@"X = %i", (int)touchX];
    NSString *touchYstring = [NSString stringWithFormat:@"Y = %i", (int)touchY];

    [touchLabelX setString:touchXstring];
    [touchLabelY setString:touchYstring];

}

- (void) dealloc
{
    [super dealloc];
}
@end

HelloWorld.h

#import "cocos2d.h"

@interface HelloWorldLayer : CCLayer
{
    int touchX,touchY;
}

+(CCScene *) scene;

@end

Upvotes: 0

EmilioPelaez
EmilioPelaez

Reputation: 19884

Just like UIKit, cocos makes it easy for you to work on both resolutions by working with "points".

A point in non-retina display is one pixel, but on retina display it's two pixels wide and two pixels high.

So, even when working on a retina device, you work on a grid of 320x480 points.

Upvotes: 2

Related Questions