0
votes

I've been struggling for days with that problem: I have CCNode >> StateComponent and in the StateComponent I have a CCSprite as an attribute and add it as a child in StateComponent. When I set the position of an StateComponent object and NOT of the sprite, the bounding box of the StateComponent object appears at the right place. The default values for a sprite position are set on (0,0). The bounding box of the sprite appears at (0,0) but the sprite texture is shifted from (0,0). I add the StateComponent object afterwards to a CCScene.

Could maybe someone help me with advice: how can I set the sprite position so that the texture and bounding box appears at the same position as the StateComponent object? Later I'd like to detect if there is a touch on the node(sprite) and then rotate the node with the sprite.

Any help would be really appreciated!!!

@interface StateComponent : CCNode {

}
@end

@implementation StateComponent

-(instancetype) initWithGestureStatewithSprite:(CCSprite*) sprite andPosition: (CGPoint) spritePosition RelativeAngle:(float) angle {

    self = [super init];

    if (!self) {
        return nil;
    }

    self.sprite = sprite;
    self.relativeAngle = angle;
    self.position = spritePosition;

    [self addChild:sprite];
    return self;

}

@end

@interface StateViewScene : CCScene {

}
@end

@implementation StateViewScene 

-(id) init {
    self = [super init];

    if (!self) {
        return nil;
    }

    StateComponent * body = [[StateComponent alloc] initWithGestureStatewithSprite [CCSprite spriteWithImageNamed:@"body.png"] andPosition: CGPointMake(512,384) RelativeAngle:0];

    [self addChild:body];
    return self;

}
2
Post relevant code to add context to your question. Just based on description alone this can't be debugged. I'm guessing that maybe you changed anchorPoint from its default 0.5, 0.5. Or perhaps you did not consider that child node positions are relative to their parent's position, and maybe you need to convertToWorldSpace/convertToNodeSpace. Also note that boundingBox is a property that considers all these various aspects whereas custom code to determine bounding box might not.LearnCocos2D
Thanks for your reply! I've added the code. I don't change the anchor point. All I do is to set node's (StateComponent) position. I tried also with convertToWorldCoordinates and vice versa, but the texture was set then randomly... Unfortunately I cannot add a screenshot...UnhappyStudent
If I add the sprite directly to the scene (but it remains attribute of StateComponent) then it's displayed at the correct position.UnhappyStudent
Should I do in the init method of StateComponent: self.sprite.position = [self convertToNodeSpace: CGPointMake(512,384)]; or self.sprite.position = [self.sprite convertToNodeSpace: CGPointMake(512,384)]; ?UnhappyStudent

2 Answers

1
votes

Have you tried to set the content Size of the Node to the Sprite content Size?

-(instancetype) initWithGestureStatewithSprite:(CCSprite*) sprite andPosition: (CGPoint) spritePosition RelativeAngle:(float) angle { 
...
self.contentSize = sprite.contentSize;
...
1
votes

I managed to solve the problem by converting to node space the StateComponent position couple of times as I actually have a tree like structure of StateComponents with sprites.

Thanks for the help! :)

---Edit---

This article helped me and might be interesting: http://www.koboldtouch.com/display/IDCAR/Converting+Between+Coordinate+Spaces