Objective c 拖动边界
我正在尝试使用以下代码使我的精灵停留在屏幕边框中:Objective c 拖动边界,objective-c,ipad,cocos2d-iphone,Objective C,Ipad,Cocos2d Iphone,我正在尝试使用以下代码使我的精灵停留在屏幕边框中: - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; [self selectSpriteForTouch:touchLocation]; return TRUE; } - (void)ccTouchMoved:(UITouch *)touc
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
[self selectSpriteForTouch:touchLocation];
return TRUE; }
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
if (selSprite) {
CGPoint oldLocation = [touch previousLocationInView:touch.view];
oldLocation = [[CCDirector sharedDirector] convertToGL:oldLocation];
oldLocation = [self convertToNodeSpace:oldLocation];
CGPoint translation = ccpSub(touchLocation, oldLocation);
CGPoint newPos = ccpAdd(selSprite.position, translation);
selSprite.position = newPos; } }
我知道我需要添加一些代码来指定边界。如下所示:
int width=point.x;
int height=point.y;
if (winSize.width-sprite.contentSize.width/2<width) {
width=winSize.width-sprite.contentSize.width/2;}
if (winSize.height-sprite.contentSize.height/2<height) {
height=winSize.height-sprite.contentSize.height/2;}
if (sprite.contentSize.height/2>height) {
height=sprite.contentSize.height/2;}
if (sprite.contentSize.width/2>width) {
width=sprite.contentSize.width/2;}
int width=point.x;
整数高度=点y;
if(winSize.width sprite.contentSize.width/2width){
宽度=sprite.contentSize.width/2;}
但我对目标C还不熟悉,我还不完全了解如何处理它。明白了!
只需添加if(selSprite){}
这部分代码
int width=touchLocation.x;
int height=touchLocation.y;
if (selSprite.contentSize.width!=width) {
width=selSprite.contentSize.width/2;}
if (selSprite.contentSize.height/2<height) {
height=selSprite.contentSize.height/2;}
[selSprite setPosition:ccp(width,height)];
int width=touchLocation.x;
int height=touchLocation.y;
if(selSprite.contentSize.width!=宽度){
宽度=selSprite.contentSize.width/2;}
如果(selSprite.contentSize.height/2)