我目前正在SpriteKit
开发一个大型游戏,它在游戏中使用了许多不同的角色和敌人——所有这些都按计划运行得很好。
然而,随着关卡和游戏玩法变得更加复杂,我希望利用 AppleGameplayKit
的功能来改进游戏“思考”的逻辑和易用性以及它的运行效率。
然而,我面临的问题是我似乎根本无法让我的agentWillUpdate:(GKAgent *)agent
或agentDidUpdate:(GKAgent *)agent
委托的方法运行。我对我的角色的移动控制没有任何问题,我对如何GKAgent
控制敌人的移动也没有任何问题,但我无法让这些方法开火。
这是我的头文件OLevel.h:
#import "OEnemy.h"
@import SpriteKit;
@import GameplayKit;
@interface OLevel : SKScene <SKPhysicsContactDelegate>
// A component system to manage per-frame updates for all agents
@property (nonatomic, readwrite) GKComponentSystem *agentSystem;
@property (nonatomic, readwrite) OEnemy *testEnemy;
@property (nonatomic, readwrite) GKAgent2D *characterAgent;
@property (nonatomic, readwrite) GKAgent2D *enemyAgent;
@property (nonatomic, readwrite) GKGoal *seekGoal;
这是我设置SKScene
OLevel.m的方式:
- (void)initSceneWithLevel:(NSInteger)level andChapter:(NSInteger)chapter {
currentLevel = level;
currentChapter = chapter;
[self setUpScene];
[self setUpCharacter];
[self setUpEnemyWithLocation:[enemyArray firstObject]];
}
- (void)didMoveToView:(SKView *)view {
// Set up the agent system
self.agentSystem = [[GKComponentSystem alloc] initWithComponentClass:[GKAgent2D class]];
self.characterAgent = [[GKAgent2D alloc] init];
self.characterAgent.position = (vector_float2){character.position.x, character.position.y};
[self.agentSystem addComponent:self.testEnemy.agent];
self.seekGoal = [GKGoal goalToSeekAgent:self.characterAgent];
[self.testEnemy.agent.behavior setWeight:1 forGoal:self.seekGoal];
/* >>> More of scene setup below - not relevant to GameplayKit issues <<< */
}
注意当我将我的敌人添加到场景中时,我将 分配给self.testEnemy
我创建的敌人:
- (void)setUpEnemyWithLocation:(NSString *)location {
OEnemy *enemy = [OEnemy node];
[enemy createEnemyWithLocation:location];
[chapterScene addChild:enemy];
// Add enemy to enemyArray
[enemyArray addObject:enemy];
self.testEnemy = enemy;
self.testEnemy.agent = [[GKAgent2D alloc] init];
self.testEnemy.agent.behavior = [[GKBehavior alloc] init];
}
这是我的SKNode
子类OEnemy.h:
@import SpriteKit;
@import GameplayKit;
@interface OEnemy : SKNode <GKAgentDelegate>
@property (readwrite) GKAgent2D *agent;
这是我的SKNode
OEnemy.m:
#import "OEnemy.h"
#import "SSOC.h"
@interface OEnemy () {
SKSpriteNode *enemy;
}
@end
@implementation OEnemy
- (id)init {
if (self = [super init]) {
// Initialization done here
NSLog(@"Enemy is in the scene");
// An agent to manage the movement of this node in a scene.
_agent = [[GKAgent2D alloc] init];
_agent.radius = 40;
_agent.position = (vector_float2){self.position.x, self.position.y};
_agent.delegate = self;
_agent.maxSpeed = 100;
_agent.maxAcceleration = 50;
}
return self;
}
/* Methods that are independent of GameplayKit
.
.
.
*/
#pragma mark - GameplayKit Delegate
- (void)agentWillUpdate:(nonnull GKAgent *)agent {
NSLog(@"will update");
}
- (void)agentDidUpdate:(nonnull GKAgent2D *)agent {
self.position = CGPointMake(agent.position.x, agent.position.y);
NSLog(@"did update");
}
我不知道我必须做些什么来实现我的简单目标:
• 我需要我OLevel
的,它GKComponentSystem *agentSystem
只是简单地初始化,将其添加enemyAgent
为一个组件,为它分配一个应该做什么的行为,然后更新连接到该代理的节点的移动。
如果有人能帮助我理解我做错了什么/错过了什么,我将不胜感激。先感谢您。
仅供参考,我已经关注了 Apple 的 AgentsCatalog iOS(目标 C)示例项目,了解它们如何GameplayKit
用于寻找、徘徊、植绒等,但我无法在我的 sprite 项目中重现它们的功能。