CPU&记忆力稳步上升&由于矢量运动,FPS全部下降

时间:2015-06-14 20:04:07

标签: ios objective-c sprite-kit

我的CPU&在我当前的Sprite Kit游戏的游戏测试期间,内存在大约6分钟内稳定增加。我发现方法&#39 ;-(void)walkToward:(CGPoint)targetPosition'在Player类中负责(它将角色移动到屏幕上的抽头位置),但我不明白为什么。游戏是正交的,没有重力。 Player类在名为TesMap的SKScene中初始化。以下是Player类中的代码:

Player.h

#import <SpriteKit/SpriteKit.h>
#import "CharacterWalking.h"


@interface Player : CharacterWalking

//Player movement stuff.
@property NSTimeInterval lastUpdateTime; //Keep track of the last time update: was called.
@property NSTimeInterval dt; //Keep track of the Delta Time since the last update.
@property CGPoint velocity; //CGPoints represent positions. However, here it'll represent a 2D vector (direction & length) instead.
@property CGPoint touchLocation; //Where the player will move to.
@property CGPoint lastTouchLocation; //Will keep track of last position.
@property BOOL walking; //Will either stop or begin walking animation.
@property float distance;


-(void)walkToward:(CGPoint)targetPosition;
-(void)faceCurrentDirection;
-(void)GPS:(CFTimeInterval)cT;

@end

Player.m

-(void)GPS:(NSTimeInterval)cT
{
    //Keeps track of update methods update function: Actual update timing is slightly different/inexact every time.
    if (self.lastUpdateTime)
    {
        _dt = cT - _lastUpdateTime; //Calculate the time since the last call to update: & store it in _dt.
    }
    else
    {
        _dt = 0;
    }
    _lastUpdateTime = cT;
    //NSLog(@"%0.2f milliseconds since last update", _dt * 1000); //Log the time in milliseconds (1 second = 1000 milliseconds).


    /*Distance: fundamental for tracking the charatcer's last position*/
    CGPoint offset = CGPointSubtract(_lastTouchLocation, self.position); //Distance between previous location & current player position.
    _distance = CGPointLength(offset); //Converts CGPoint to CGFloat for the IF statement.

    if (_distance < 50 * _dt)
    {
        _walking = NO;
        self.position = _lastTouchLocation;
        self.physicsBody.velocity = CGVectorMake(0, 0);
        [self faceCurrentDirection]; //Update texture.
    }
}

-(void)faceCurrentDirection
{
    //Some direction facing code is in here.
}

//THIS IS WHERE THE PROBLEM SEEMS TO HIDE.
-(void)walkToward:(CGPoint)targetPosition
{
    _walking = YES;

    CGPoint targetVector = CGPointNormalize(CGPointSubtract(targetPosition, self.position)); //Gets distance between point A & B.

    targetVector = CGPointMultiplyScalar(targetVector, 50); //50 is interpreted as a speed: the larger the # the faster the player moves.

    //COMMENTED OUT. PLAYER CAN'T MOVE NOW. BUT CPU & MEMORY NOT STEADILY INCREASING... WTF
    //self.physicsBody.velocity = CGVectorMake(targetVector.x, targetVector.y); //Velocity vector measured in meters per second.

    [self faceCurrentDirection];
}

-(instancetype)init
{
    //BLA BLA BLA
    //JUST SOME CODE HERE THAT SETS UP THE PLAYER CHARACTER.

        _walking = NO;
    }
    return self;
}

以下是它在SKScene / map中的外观:

TestMap.h

//#import some more BLA BLA BLA
#import "Player.h"


@interface TestScene1 : SKScene

//BLA BLA BLA

@property Player *player;

//BLA BLA BLA

@end

TestMap.m

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    _player.touchLocation = [touch locationInNode:_worldNode];
    _player.lastTouchLocation = _player.touchLocation; //Save the previous position.
    [_player walkToward:_player.touchLocation]; //Tells the player sprite to move toward the location of the user’s tap.
}


-(void)update:(NSTimeInterval)currentTime
{
    /*Called before each frame is rendered*/

    [_player GPS:currentTime];
}

我无法理解为什么物理体系。物理速度&#39;在Player类中影响CPU和&amp;记忆力增加。游戏以60 FPS开始,但大约6分钟后FPS下降到30到15之间。如果没有人知道为什么会发生这种情况,那么有人可以告诉我如何为我的Player类正确设置向量移动吗?

1 个答案:

答案 0 :(得分:0)

我已经找到了自己的问题。在通过触摸进行角色移动时,SKScene的 - (void)更新:(NSTimeInterval)currentTime方法运行该字符类方法[_player GPS:currentTime];跟踪速度和距离。这是打嗝,该方法根据他面对的方向设置角色纹理。每个帧都将相同的纹理继续放入具有重复纹理的字符泛滥存储器中。我所要做的就是在角色站立的时候对这个纹理设置方法进行一个有条件的停止,这样就不会每帧调用它,而只调用一次。