如何正确定位矩形?

时间:2015-02-11 22:59:29

标签: ios objective-c sprite-kit

我非常擅长使用XCode和Objective-C开发应用程序。我正在尝试创建一个简单的iOS应用程序,当您点击屏幕时,在手指的位置创建一个矩形。我有一个问题,矩形(类称为Ball)未正确定位。点击时,它位于屏幕左侧,略高于您的手指。我不知道如何解决这个问题,但我觉得我的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

2 个答案:

答案 0 :(得分:0)

如果您只是想调整位置,可以这样做:

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

答案 1 :(得分:0)

touchesBegan内使用locationInNode代替locationInView来获取您的接触点。
应使用touch location而不是SKScene来计算locationInNode中的locationInView。这是因为origin的{​​{1}}位于左下角,SKScene的原点位于左上角。

UIView