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.
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;
… 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??
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
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.
You must be logged in to reply to this topic.
Social Widgets powered by AB-WebLog.com.