Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 拖动边界_Objective C_Ipad_Cocos2d Iphone - Fatal编程技术网

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)