我在遊戲中有一個角色,它應該是射擊子彈。我已經爲這個角色設置了一切,並且設置了子彈穿行的路徑。下面是我使用的代碼:SKSprite沒有定位它應該在哪裏
//The destination of the bullet
int x = myCharacter.position.x - 1000 * sin(myCharacter.zRotation);
int y = myCharacter.position.y + 1000 * cos(myCharacter.zRotation);
//The line to test the path
SKShapeNode* beam1 = [SKShapeNode node];
//The path
CGMutablePathRef pathToDraw = CGPathCreateMutable();
//The starting position for the path (i.e. the bullet)
//The NozzleLocation is the location of the nozzle on my character Sprite
CGPoint nozzleLoc=[self convertPoint:myCharacter.nozzleLocation fromNode:myCharacter];
CGPathMoveToPoint(pathToDraw, NULL, nozzleLoc.x, nozzleLoc.y);
CGPathAddLineToPoint(pathToDraw, NULL, x, y);
//The bullet
SKSpriteNode *bullet = [SKSpriteNode spriteNodeWithTexture:bulletTexture size:CGSizeMake(6.f, 6.f)];
bullet.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:3 center:bullet.position ];
[bullet.physicsBody setAffectedByGravity:NO];
[bullet.physicsBody setAllowsRotation:YES];
[bullet.physicsBody setDynamic:YES];
bullet.physicsBody.categoryBitMask = bulletCategory;
bullet.physicsBody.contactTestBitMask = boundsCategory;
//These log the correct locations for the character
//and the nozzle Location
NSLog(@"myposition: %@",NSStringFromCGPoint(myCharacter.position));
NSLog(@"nozloc: %@",NSStringFromCGPoint(nozzleLoc));
bullet.position = [bullet convertPoint:nozzleLoc fromNode:self];
[self addChild:bullet];
NSLog(@"Bullet Position: %@",NSStringFromCGPoint(bullet.position));
[bullet runAction:[SKAction followPath:pathToDraw duration:6.f]];
//I'm using this to test the path
beam1.path = pathToDraw;
[beam1 setStrokeColor:[UIColor redColor]];
[beam1 setName:@"RayBeam"];
[self addChild:beam1];
這是我從NSLogs得到我在上面使用:
myposition:{122.58448028564453,109.20420074462891}
nozloc:{145.24272155761719 ,77.654090881347656}
子彈的位置:{145.24272155761719,77.654090881347656}
所以一切都應該工作,對吧?但是我遇到的問題是子彈是從一個稍微不同的位置拍攝的。您可以從下面的圖片看到:
我對齊字符,從而使子彈在中間的那個小廣場開始。通過這種方式,你可以看到子彈應該開始的距離(在我的角色持有的槍的前面)以及屏幕中間的正方形。
子彈在一條直線上正確移動,線的角度與路徑的角度相同(路徑和線條子彈形狀平行,如圖所示)。當我移動我的線時,子彈也以相同的方式移動。我認爲問題是節點之間的點轉換,但我已經嘗試了兩種方法,但我已經嘗試了兩種方法,但我已經嘗試了兩種方法,但它們都導致子彈的起點完全相同。你知道我爲什麼會遇到這個問題嗎?是因爲我使用setScale
(我將它設置爲0.3)縮小了我的角色精靈?
非常感謝您的幫助。
又一個很棒的答案Dion!我最終把這個位置的代碼行取出來了,它工作了!非常感謝! – Septronic