我在游戏中有一个角色,它应该是射击子弹。我已经为这个角色设置了一切,并且设置了子弹穿行的路径。下面是我使用的代码: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