Sprites having a local position?

Sprites having a local position?
0.0 0


Is there any way for objects and/or CCSprites to have a local position? For instance, I only want part of the CCSprite to be touchable (where I hold to move it), but I don’t want to have to continually update its position, I’d rather it just always be local to the image itself.

Any way?




Sorry, I can not understand you clearly, what’s meaning of ‘a local position’? And what’s your game logic of touching sprites. Can you paste some demo code?


It means that it’s local to the object, not to the world. The Anchor Point of a sprite is similar to a local position (say mine is in the middle and on the bottom of the sprite (.5,0)). For instance, say the world is 320x480 and my object’s sprite is 32x100. Going by what I said my anchor point is earlier, but I want a circle collision area (for where I touch) at the top of the sprite, the local position would be something like (16, 75). I want that local position (for the center of the circle on the sprite) to always be at the top of the sprite.

Or maybe I’m just going about this the wrong way: Considering there is 3 different functions for input (began, moved, ended), I’d only need to know the collision’s center (and radius) at the began portion of the input. Once I detect that there’s a collision (my finger’s point inside the circle), make there be a variable for “ChosenObject” and then update the object’s position when I am moving my input.


If I understand you correctly you only want to have the upper part of a particular sprite to be touchable?


To an extent, but it may not always be the upper part that I want touched. Maybe it’s only the right corner, or something of that nature.


Your question isn’t stated very clearly.

Is there any way for objects and/or CCSprites to have a local position?
I haven’t seen a functionality that converts world coordinates to local sprite coordinates in cocos2d. It wouldn’t be very hard to achieve though. You will need to store an offset along with the sprite if you want to move the “local position” anchor point. Maybe something like this:

CCPoint getLocalPosition(CCSprite* sprite, CCPoint& touchpos)
    CCPoint* offset = (CCPoint*)sprite->getUserData();
    return //do maths here

If you are already using the user data of the sprite then you may want to consider creating a class to wrap multiple objects.


Ok just an example, let’s say you have a 40 x 40 sprite and you only want the lower part of it as touchable. On top of my head this is what I’ve got.
On your HelloWorld.h file declare a new sprite like so

CCSprite* yourSprite;

next on your .ccp file under init()

yourSprite = CCSprite::spriteWithFile("something.png");
yourSprite->setPosition(anywhere you want);

next on your ccTouchBegan method we need to create a bounding box for you sprite’s lower part to indicate that bounding box that we are creating is touchable.

On your ccTouchBegan(CCTouch* touch, CCEvent* event)
  CCPoint location;
  location = this->convertTouchToNodeSpace(touch);
  //A little math here will get your sprite's lower bounding box
  CCRect yourSpriteLowerBoundingBox = CCRectMake(yourSprite->getPosition().x - (yourSprite->getContentSize().width), yourSprite->getPosition().y - (yourSprite->getContentSize().height), yourSprite->getContentSize().width, yourSprite->getContentSize().height /2);

  if (CCRect::CCRectContainsPoint(yourSpriteLowerBoundingBox, location)
    this->removeChild(yourSprite, true);

now if you touch the lower part of yourSprite it should delete your yourSprite otherwise nothing will happen.