未被调用的GameplayKit委派方法

时间:2016-07-24 17:15:39

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

我目前正在开发一款大型SpriteKit游戏,它在游戏中使用了许多不同的角色和敌人 - 所有游戏都按计划运行得非常好。

随着关卡和游戏玩法变得更加复杂,我希望利用Apples GameplayKit功能来改善游戏的逻辑和易用性“思考”以及它的运行效率。

然而,我遇到的问题是,我似乎无法让我的agentWillUpdate:(GKAgent *)agentagentDidUpdate:(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");

}

我不知道我必须做些什么来实现我的简单目标:

•我需要我的OLevelGKComponentSystem *agentSystem来简单地初始化,添加enemyAgent作为组件,为其分配行为,然后更新节点的运动附在那个代理人身上。

如果有人能帮助我理解我做错了什么/错过了什么,我将非常感激。提前谢谢。

仅供参考我已经关注了Apple的AgentsCatalog iOS(Objective C)示例项目以及他们如何使用GameplayKit进行搜索,游荡,植绒等,但我无法重现他们的我的精灵项目中的功能..

0 个答案:

没有答案