2015-02-11 64 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]; 
    } 
}