我已经开始做一个小的swift/spritekit项目来教我自己的游戏开发.它以等距地图开始,我设法绘制.但是我在地图的不同瓷砖上获得精确的触摸位置时遇到了麻烦.它有效,但有点不合适,似乎不一致.
这是我的功能:
class PlayScene: SKScene {
let map = SKNode()
override func didMoveToView(view: SKView) {
let origin = view.frame.origin
let mapOrigin = CGPointMake(origin.x + self.frame.width / 4, origin.y - self.frame.height / 4)
let mapConfig: Int[][] =
[[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[2, 2, 2, 2, 1, 2, 2, 2],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, …Run Code Online (Sandbox Code Playgroud) 我创建了一个非常简单的示例代码,只有一个场景,一个精灵节点20x20px,屏幕上的点0.0.当我调用scene.physicsWorld bodyAtPoint它时,即使在例如34x34点,也会返回此节点.但在35x35点它会返回null.所以基本上所有从0px到34px的点都返回此节点,从35px开始,它不再返回它.任何想法可能是什么原因,如果精灵明显以20px 20px结束?可以看到相同的行为bodyInRect.
以下是示例代码:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
self.physicsWorld.gravity = CGVectorMake(0, 0);
SKSpriteNode *node = [[SKSpriteNode alloc] initWithColor:[UIColor whiteColor] size:CGSizeMake(20, 20)];
node.position = CGPointMake(10, 10);
node.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(20, 20)];
[self addChild:node];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
if([self.physicsWorld bodyAtPoint:location])
{
NSLog(@"Detected at …Run Code Online (Sandbox Code Playgroud)