【问题标题】:SpriteKit: Making a character move in a worldSpriteKit:让角色在世界中移动
【发布时间】:2013-12-02 08:13:38
【问题描述】:

我正在尝试了解如何创建一个巨大的世界,让角色在世界中移动,然后通过移动的角色移动世界的可见部分。就像马里奥游戏一样。

我在这里阅读苹果指南:https://developer.apple.com/library/IOs/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Actions/Actions.html#//apple_ref/doc/uid/TP40013043-CH4-SW32

关于“将场景集中在节点上”,但我真的完全无法理解。我尝试实现它,但它根本不起作用。

那么,有人可以在这里帮助我吗?一个带有一些好的 cmets 的完整示例会很棒。

希望我的问题有意义,在此先感谢大家!

【问题讨论】:

    标签: ios objective-c xcode sprite sprite-kit


    【解决方案1】:

    他们试图与你交谈“你的游戏世界是一个节点,你需要做的只是用你的角色移动世界节点”。 游戏“冒险”——Sprite Kit 的示例游戏将极大地帮助您创建这样一个巨大的世界。

    https://developer.apple.com/LIBRARY/IOS/documentation/GraphicsAnimation/Conceptual/CodeExplainedAdventure/AdventureArchitecture/AdventureArchitecture.html

    => 纪录片中有这个游戏的示例代码。

    此外,您可以创建“平铺地图”来做同样的事情。

    【讨论】:

      【解决方案2】:

      我使用一个简单的类AGMovingNode,继承自SKNode作为背景层。

      它并不完美,但至少你可以从哪里开始。

      这是该类的代码:

      AGMovingNode.h

      #import <SpriteKit/SpriteKit.h>
      
      @interface AGMovingNode : SKNode
      
      @property float pointsPerSecondSpeed;
      
      - (instancetype)initWithPointsPerSecondSpeed:(float)pointsPerSecondSpeed;
      - (void)update:(NSTimeInterval)currentTime paused:(BOOL)paused;
      
      @end
      

      AGMovingNode.m

      #import "AGMovingNode.h"
      
      @implementation AGMovingNode
      {
      
          NSTimeInterval _lastUpdateTime;
          NSTimeInterval _deltaTime;
      
      }
      
      - (instancetype)initWithPointsPerSecondSpeed:(float)pointsPerSecondSpeed {
          if (self = [super init]) {
              self.pointsPerSecondSpeed = pointsPerSecondSpeed;
          }
          return self;
      }
      
      - (void)update:(NSTimeInterval)currentTime paused:(BOOL)paused {
          if (paused) {
              _lastUpdateTime = 0;
              return;
          }
          //To compute velocity we need delta time to multiply by points per second
          if (_lastUpdateTime) {
              _deltaTime = currentTime - _lastUpdateTime;
          } else {
              _deltaTime = 0;
          }
          _lastUpdateTime = currentTime;
      
          CGPoint bgVelocity = CGPointMake(-self.pointsPerSecondSpeed, 0.0);
          CGPoint amtToMove = CGPointMake(bgVelocity.x * _deltaTime, bgVelocity.y * _deltaTime);
          self.position = CGPointMake(self.position.x+amtToMove.x, self.position.y+amtToMove.y);
      }
      @end
      

      我在其中一个教程中找到了它(现在不记得是哪一个了)并稍微修改了它。

      【讨论】:

        【解决方案3】:

        我发现最好使用 SKActions 来移动对象。

        我所做的是有一种方法可以用精灵动画重新定位我的角色(当他站立和行走时):

        - (void)moveUser:(CGPoint)position {
          SKSpriteNode *user = (SKSpriteNode*)[self childNodeWithName:@"userSprite"];
        
          // ... some logic to load correct atlas
          NSArray *animationFrames = ...
        
          SKAction *animation = [SKAction animateWithTextures:animationFrames timePerFrame:0.05f numberOfFrames:animationFrames.size];
          SKAction *moveToPositionAction = [SKAction moveTo:position duration:0.4f];
        
          [userNode runAction:[SKAction group:@[animation,moveToPositionAction]]];
        }
        

        【讨论】:

          猜你喜欢
          • 2022-11-10
          • 1970-01-01
          • 2021-11-29
          • 2020-07-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-02
          • 1970-01-01
          相关资源
          最近更新 更多