【问题标题】:Spritekit Screen/Background UpdateSpritekit 屏幕/背景更新
【发布时间】:2017-05-21 15:19:34
【问题描述】:

是否可以在-(void)update:(CFTimeInterval)currentTime 方法中从同一个SKSpriteNode 获得2 个位置?

我正在做一个演示。从屏幕外的随机点射出一个球。我想得到球的方向和位置数组,以便我可以根据它做一些事情。但是当我使用update: 方法时,它似乎在每次更新中填充了数组。如何每次更新只添加一个SKSpriteNode 到数组中?

NSMutableArray *nodePos;
int returnPosCount;

-(void)update:(CFTimeInterval)currentTime {

    if ([self childNodeWithName:@"//ball"]!=nil){
    returnPosCount++;
    if (returnPosCount == 15) {
        SKNode *node = [self childNodeWithName:@"//ball"];
        [nodePos addObject:node];

        if ([nodePos count]>4) {
             [nodePos removeObjectAtIndex:0];
            NSLog(@"%@",nodePos);


        }
        returnPosCount =0;
    }

NSLOG:    
<SKSpriteNode> name:'ball' (64 x 64)] position:{-127.63968658447266, 135.19813537597656} 
<SKSpriteNode> name:'ball' (64 x 64)] position:{-127.63968658447266, 135.19813537597656} 
<SKSpriteNode> name:'ball' (64 x 64)] position:{-127.63968658447266, 135.19813537597656} 

【问题讨论】:

    标签: objective-c sprite-kit


    【解决方案1】:

    您没有在每次更新中填充数组。您看到数组已满,因为您仅在数据已满时才打印数据。只需将您的 NSLog 移到 [nodePos count] 条件之外。

    现在您具有相同值的原因是因为您正在添加节点,而不是每次更新时的位置。由于节点是一个引用,因此您将拥有一个充满对同一对象的引用的数组,从而获得相同的位置。您应该构建一个 CGPoint 数组,而不是一个 SKNode 数组。

    为此,请尝试更改:

    [nodePos addObject:node];
    

    [nodePos addObject:node.position];
    

    【讨论】:

    • 非常感谢。将 CGPoint 添加到数组中有效。我竖起大拇指,但我没有足够的声誉来展示它。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 2019-11-15
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多