Drag CCSprite inside a box

Forums Programming cocos2d support (graphics engine) Drag CCSprite inside a box

This topic contains 2 replies, has 2 voices, and was last updated by  michaelm 1 year, 5 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
Author Posts
Author Posts
November 10, 2012 at 2:06 pm #245236

tiggybouncer
Participant
@tiggybouncer

Hello all,

I’m trying to drag a sprite (that’s the child of a ‘canvas’ sprite) around, however it won’t move :(

I have the following code:

-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

if(CGRectContainsPoint ([activePart boundingBox], touchLocation))
{
CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];

CGPoint translation = ccpSub(touchLocation, oldTouchLocation);
CGPoint newPos = ccpAdd(activePart.position, translation);
activePart.position = newPos;

NSLog(@"Reached function");
}
}

… but I haven’t added the code to stop it from leaving the boundingBox of the canvas, because I don’t know how to do that :( Can I have some help please?? :)

Thanks :)

November 10, 2012 at 9:05 pm #391869

tiggybouncer
Participant
@tiggybouncer

But hey, I don’t mind if I don’t have the sprite selection bit yet… I’ll add that once I’ve got the other part working ;)

November 10, 2012 at 11:45 pm #391870

michaelm
Participant
@michaelm

I was just about to start working on this, possibly for the same reason a crop tool?

Thinking about it limiting to a bounding box the code would be something like this pseudocode

assuming anchorPoint (.5, .5)

int leftEdge = position.x – (width * .5)

rightEdge = position.x + (width * .5)

topEdge = position.y – (height * .5)

bottomEdge = position.y + (height * .5)

then just check bounds and reset

if (leftEdge < 0) position.x = width * .5;

if (rightEdge > boundaryRight) position.x = boundaryRight – width * .5;

if (topEdge < 0) position.y = height * .5;

if (bottomEdge > boundaryBottom) position.y = boundaryBottom – height * .5;

Something like that anyway, I’ll probably start writing it tonight so if I run across anything wrong with the idea I’ll repost.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.