2014-05-19 24 views
1

作爲新的OBJ-C語言,我有一個網格定位矩形困難雪碧套件...不能在網格位置SKShapeNode(矩形)正是

有矩形中的偏移我創建 - 如果我設法得到的代碼定位一個矩形在一個選定的領域另一個矩形將被抵消...

我已經嘗試了幾種不同的認可,我可以得到它使用JavaFX的工作。我究竟做錯了什麼?

下面的照片清楚地顯示了我的問題。

enter image description here

我的代碼是相當簡單,並且可以看這裏:

#import "MyScene.h" 

@implementation MyScene 

const int ROWS = 10; 

-(id)initWithSize:(CGSize)size {  
if (self = [super initWithSize:size]) { 
    /* Setup your scene here */ 

    self.backgroundColor = [SKColor darkGrayColor]; 

    [self createBoardWithRows:ROWS]; 

    [self createBoxPositionX:1 positionY:1]; 
    [self createBoxPositionX:3 positionY:3]; 
    [self createBoxPositionX:5 positionY:5]; 

} 
return self; 
} 

-(void) createBoardWithRows: (int) rows{ 

for (int i = 1; i < rows; i++){ 

    //Horisontal lines 
    int yPos = self.size.height/rows * i; 

    SKShapeNode *lineH = [SKShapeNode node]; 
    CGMutablePathRef pathToDraw = CGPathCreateMutable(); 

    CGPathMoveToPoint(pathToDraw, NULL, 0, yPos); 
    CGPathAddLineToPoint(pathToDraw, NULL, self.size.width, yPos); 

    lineH.path = pathToDraw; 
    lineH.lineWidth = 1.0; 
    [lineH setStrokeColor:[UIColor blackColor]]; 


    //Vertical Lines 
    int xPos = self.size.width/rows * i; 

    SKShapeNode *lineV = [SKShapeNode node]; 

    CGPathMoveToPoint(pathToDraw, NULL, xPos, 0); 
    CGPathAddLineToPoint(pathToDraw, NULL, xPos, self.size.height); 

    lineV.path = pathToDraw; 
    lineV.lineWidth = 1.0; 
    [lineV setStrokeColor:[UIColor blackColor]]; 

    //Add lines 
    [self addChild:lineH]; 
    [self addChild:lineV]; 
} 
} 

-(void) createBoxPositionX:(int) fieldIndexX positionY:(int) fieldIndexY{ 

int width = self.size.width/ROWS; 
int height = self.size.height/ROWS; 

int x = (width * fieldIndexX); 
int y = (height * fieldIndexY); 

CGRect box = CGRectMake(x, y, width, height); 

SKShapeNode *shapeNode = [[SKShapeNode alloc] init]; 
shapeNode.path = [UIBezierPath bezierPathWithRect:box].CGPath; 
shapeNode.fillColor = SKColor.yellowColor; 
//Stroke settings 
shapeNode.strokeColor = [SKColor clearColor]; 
shapeNode.lineWidth = 0; 
[self addChild:shapeNode]; 

//Alternative rectangle 
//SKSpriteNode spriteNodeWithColor:CGSize: 
} 

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
/* Called when a touch begins */ 
} 

-(void)update:(CFTimeInterval)currentTime { 
/* Called before each frame is rendered */ 
} 

@end

+3

它看起來好像沒有考慮到每個框中邊框的線條寬度 - 所以當您創建黃色框時,它們會被相當於「邊框」數量的點數所代替。 – KerrM

回答

3

它幾乎好像你不考慮邊框的線寬在每個框中 - 因此,當您創建黃色框時,它們將被相當於「邊框」數量的點數所取代。

爲了解決這個問題,更改這兩個行:

int x = (width * fieldIndexX); 
int y = (height * fieldIndexY); 

到:

int x = (width * fieldIndexX) + fieldIndexX; 
int y = (height * fieldIndexY) + fieldIndexY; 
+0

這也是我最初的想法,但我仍然遇到這個解決方案的問題。似乎只有我是Y軸。你可以在這裏看到結果:https://www.dropbox.com/s/47e5edyk4osrr5b/Screenshot%202014-05-19%2012.53.32.png – Zahrec

0

如果您收到1個像素移位到它可能是錨點問題的任何方向。 默認情況下它是0.5,0.5 - 在精靈的中心。 因此,如果你的精靈甚至有長度爲例如4,中間可以落在2或3,你會得到1像素關閉定位。

這種情況的解決方法是明確的,節點的錨點設置爲0,0,就像這樣:

node.anchorPoint = CGPointMake(0,0); 

這裏做的事情是,精靈被固定到它的父與左下角,而不是中央。由於左下角總是從0,0像素開始,所以無論節點的像素數量如何,您都可以擁有正確的位置。

但是你將不得不調整節點的位置來適應這種變化。

希望這會有所幫助。