我有一个iOS无尽的赛跑游戏,路径从顶部出现并向下滚动。
我是这样做的:创建了2个精灵,当第一个精灵从屏幕上消失时,我从图像中加载另一个精灵并运行相同的逻辑。
问题:第一个精灵消失后会出现一个小的滞后现象。
编辑:只有在iPhone 5上运行时才会出现此问题.iPhone 4可以正常运行!
有人有什么想法吗?或者之前遇到过这个问题并解决了吗?
if (CurrentObstacle.frame.origin.y < -self.frame.size.height + 10) {
// clear old obstacle
CurrentObstacle = nil;
CurrentObstacle = nextObstacle;
currentObstacleImage = nextObstacleImage; // for pixel processing stuff
[self generateObstacle];
}
- (void)generateObstacle{
// genrate random image name
int i = rand()%10+1;
NSString *imageName = [NSString stringWithFormat:@"ob%i", i];
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
if(screenSize.height == 480)
{
//Load 3.5 size
imageName = [imageName stringByAppendingString:@"small"];
}
imageName = [imageName stringByAppendingString:@".png"];
// create obstacle
nextObstacle = [SKSpriteNode spriteNodeWithImageNamed:imageName];
nextObstacleImage = [imageDictionary objectForKey:imageName];
nextObstacle.size = self.size;
nextObstacle.position = CGPointMake(self.view.center.x, 1.48*nextObstacle.size.height);
// set speed to be the same as the other obstacles
nextObstacle.speed = CurrentObstacle.speed;
// show obstacle
[self addChild:nextObstacle];
// move obstacle
SKAction *moveAction = [SKAction moveByX:0 y:-2*self.size.height-1 duration:8];
[nextObstacle runAction:moveAction];
}
答案 0 :(得分:1)
好的,通过删除@ 2x图像解决了这个问题。显然处理大图像是造成滞后的原因。
希望这有助于某人!
答案 1 :(得分:0)
每次要添加障碍物时都不应加载图像。你应该做一个这样的方法,你可以在&#34; initWithSize&#34;
//MyScene.h
@interface MyScene : SKScene{
SKTexture * textureObstacle1;
SKTexture * textureObstacle2;
//...//
}
-(id)initWithSize;
-(void)LoadSprites;
@end
//MyScene.m
@implementation MyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
[self LoadSprites];
}
return self;
}
-(void)LoadSprites{
textureObstacle1=[[SKTexture alloc] initWithImageNamed:@"obstacle1.png"];
//...//
}
- (void)generateObstacle{
//...//
nextObstacle = [SKSpriteNode alloc]init];
[nextObstacle setTexture:textureObstacle1];
}
@end