iOS Sprite Kit平台跳线问题

时间:2014-06-18 14:25:49

标签: ios objective-c sprite-kit game-engine

我目前正在开发平台跳线应用程序,但我是一个完整的冲洗器。 无论如何,我在网上找到了一个教程,并开始调整该代码。

我现在的问题是:我创建了一个静态图像的SpriteKitNode。我想用跑步(跳跃/攻击)动画覆盖它。所以我想我可以将Atlastexture坐标设置为SpriteNode坐标。 它甚至可以工作(现在没有动画)。但是当我开始移动MapchildNode(静态图像)时,我自己的纹理移出屏幕,因为地图开始以静态节点为中心。我尝试将纹理设置为与每个帧的节点相同的坐标,甚至控制台显示坐标是相同的。但是,纹理远离节点。

正如我所说,我是一个总菜鸟。如果我现在显示错误的代码行,请耐心等待。

@implementation GameLevelScene

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
[[SKTAudio sharedInstance] playBackgroundMusic:@"sim.mp3"];

self.backgroundColor = [SKColor colorWithRed:.4 green:.4 blue:.95 alpha:1.0];


self.map = [JSTileMap mapNamed:@"nun.tmx"];
[self addChild:self.map];
self.walls = [self.map layerNamed:@"walls"];
self.hazards = [self.map layerNamed:@"hazards"];

self.player = [[Player alloc] initWithImageNamed:@"koalio_stand"];
self.player.position = CGPointMake(100, 80);
self.player.zPosition = 15;
[self.map addChild:self.player];


// NOW THE ANIMATION
SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"noel"];
SKTexture *f1 = [atlas textureNamed:@"noel1.png"];
SKTexture *f2 = [atlas textureNamed:@"noel2.png"];
SKTexture *f3 = [atlas textureNamed:@"noel3.png"];
SKTexture *f4 = [atlas textureNamed:@"noel4.png"];
SKTexture *f5 = [atlas textureNamed:@"noel5.png"];
SKTexture *f6 = [atlas textureNamed:@"noel6.png"];
SKTexture *f7 = [atlas textureNamed:@"noel7.png"];
self.noel = [SKSpriteNode spriteNodeWithTexture:f1];

[self addChild:self.noel];


NSArray *noelRunning = @[f1, f2, f3, f4, f5, f6, f7, f6, f5, f4, f3, f2, f1, f2, f3, f4, f5, f6, f7, f6, f5, f4, f3, f2, f1, f2, f3, f4, f5, f6, f7, f6, f5, f4, f3, f2, f1];
// LEARN TO LOOP!?

self.running = [SKAction animateWithTextures:noelRunning timePerFrame:0.03];

// END OF ANIMATION




self.userInteractionEnabled = YES;
  }
  return self;
}

静态图像为“koalio-stand.png”

那么有更新方法:

- (void)update:(NSTimeInterval)currentTime
{
  if (self.gameOver) return;

  NSTimeInterval delta = currentTime - self.previousUpdateTime;

if (delta > 0.02) {
delta = 0.02;
}

self.previousUpdateTime = currentTime;

[self.player update:delta];

//[self.noel runAction:self.running];


  [self checkForAndResolveCollisionsForPlayer:self.player forLayer:self.walls];
  [self handleHazardCollisions:self.player]; 
  [self checkForWin];


  [self setViewpointCenter:self.player.position];


}

然后有一个大麻烦制造者......?这个方法使视图居中,正是视图居中时,静态图像和纹理都在分开(静态图像停留在中间,纹理移出屏幕)。但控制台显示两者具有相同的坐标。 我使用了>> self.player.position = self.noel.position<<

- (void)setViewpointCenter:(CGPoint)position {
  NSInteger x = MAX(position.x, self.size.width / 2);
  NSInteger y = MAX(position.y, self.size.height / 2);
  x = MIN(x, (self.map.mapSize.width * self.map.tileSize.width) - self.size.width / 2);
  y = MIN(y, (self.map.mapSize.height * self.map.tileSize.height) - self.size.height / 2);



  CGPoint actualPosition = CGPointMake(x, y);
  CGPoint centerOfView = CGPointMake(self.size.width/2, self.size.height/2);
  CGPoint viewPoint = CGPointSubtract(centerOfView, actualPosition);

  self.map.position = viewPoint;
  self.noel.position = self.player.position;

}

我求你的帮助=((

1 个答案:

答案 0 :(得分:1)

我不清楚你想要实现什么,但如果你的问题是为什么playernoel对象在分配相同的位置时会移动到不同的地方?答案是,您的变量playernoel是不同父母的子项,playerself.map的子项,noelself的子项。

节点的位置与父母的帧相关。

相关问题