2015-02-11 62 views
0

我对使用XCode和Objective-C开发应用程序完全陌生。我正在尝试创建一个简单的iOS应用程序,在您点击屏幕时在手指位置创建一个矩形。我有一个问题,矩形(该类称为球)没有正确定位。点击时,它位于屏幕左侧,略高于手指。我不知道如何解决这个问题,但我觉得这是我的init函数的问题。谁能帮忙?如何正确定位矩形?

谢谢。

GameScene.m

#import "GameScene.h" 
#import "Ball.h" 

@implementation GameScene 

double SCREEN_WIDTH, SCREEN_HEIGHT; 

NSMutableArray *screenObjects; 
SKView *mainView; 

- (void) didMoveToView:(SKView*)view { 
    mainView = view; 
    SCREEN_WIDTH = self.view.frame.size.width; 
    SCREEN_HEIGHT = self.view.frame.size.height; 
    screenObjects = [[NSMutableArray alloc] init]; 
    [Ball setDefaultView: self]; 
    [Ball setRightBounds: SCREEN_WIDTH]; 
    [Ball setLeftBounds: 0]; 
    [Ball setBottomBounds: SCREEN_HEIGHT]; 
    [Ball setTopBounds: 0]; 
} 

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    for (UITouch *touch in touches) { 
     double x = [touch locationInView: mainView].x; 
     double y = [touch locationInView: mainView].y; 
     Ball *object = [[Ball alloc] init]; 
     [object setPosition: CGPointMake(x, y)]; 
     [screenObjects addObject: object]; 
     NSLog(@"%f %f", x, y); 
    } 
} 

- (void) update:(CFTimeInterval)currentTime { 
    for (int i = 0; i < screenObjects.count; i++) { 
     [screenObjects[i] applyVelocity]; 
     [screenObjects[i] dealWithWallCollisions]; 
    } 
} 

@end 

Ball.h

#ifndef IndependentStudyPrototype_Ball_h 
#define IndependentStudyPrototype_Ball_h 
#import "GameScene.h" 

@interface Ball : NSObject 

@property double width; 
@property double height; 
@property double x; 
@property double y; 
@property double velocity; 

+ (void) setDefaultView:(GameScene*)view; 
+ (void) setTopBounds:(double)bounds; 
+ (void) setBottomBounds:(double)bounds; 
+ (void) setLeftBounds:(double)bounds; 
+ (void) setRightBounds:(double)bounds; 
- (void) setPosition:(CGPoint)point; 
- (void) updatePosition; 
- (void) dealWithWallCollisions; 
- (void) applyVelocity; 


@end 

#endif 

Ball.m

#import <Foundation/Foundation.h> 
#import "Ball.h" 
#import "GameScene.h" 

static GameScene *defaultView; 
static double leftBound, rightBound, topBound, bottomBound; 

@implementation Ball { 
    SKSpriteNode *rectangle; 
    double stepX, stepY; 
} 

+ (void) setDefaultView:(GameScene*)view { 
    defaultView = view; 
} 

+ (void) setBottomBounds:(double)bounds { 
    bottomBound = bounds; 
} 

+ (void) setTopBounds:(double)bounds { 
    topBound = bounds; 
} 

+ (void) setLeftBounds:(double)bounds { 
    leftBound = bounds; 
} 

+ (void) setRightBounds:(double)bounds { 
    rightBound = bounds; 
} 

- (void) updatePosition { 
    self->rectangle.position = CGPointMake(self.x, self.y); 
} 

- (void) setPosition:(CGPoint)point { 
    self.x = point.x; 
    self.y = point.y; 
    [self updatePosition]; 
} 

- (void) applyVelocity { 
    self.x += self->stepX; 
    self.y += self->stepY; 
    [self updatePosition]; 
} 

- (void) dealWithWallCollisions { 
    if (self.x > leftBound) { 
     self->stepX = -self.velocity; 
    } else if (self.x < rightBound) { 
     self->stepX = self.velocity; 
    } 
    if (self.y > bottomBound) { 
     self->stepY = -self.velocity; 
    } else if (self.y < topBound) { 
     self->stepY = self.velocity; 
    } 
} 

- (id)init { 
    self = [super init]; 
    self->rectangle = [[SKSpriteNode alloc] initWithColor: [UIColor blueColor] size: CGSizeMake(50, 50)]; 
    self.velocity = 1; 
    self->stepX = self.velocity; 
    self->stepY = self.velocity; 
    [defaultView addChild: self->rectangle]; 
    return self; 
} 

@end 

回答

0

如果你只是微调位置,y OU可以做这样的事情:

[object setPosition: CGPointMake(x+20, y-10)]; 
0

touchesBegan使用locationInNode,而不是locationInView,让您的触摸点。
SKScene中的touch location应该使用locationInNode而不是locationInView来计算。这是因为SKSceneorigin位于左下角,UIView的原点位于左上角。

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { 
    for (UITouch *touch in touches) { 
     CGPoint point = [touch locationInNode:self]; // Changed 
     Ball *object = [[Ball alloc] init]; 
     [object setPosition: CGPointMake(point.x, point.y)]; // Changed 
     [screenObjects addObject: object]; 
    } 
}