这是我的代码,用于从左到右移动的星星,我在线教程中使用它:
SKTexture* starsTexture = [SKTexture textureWithImageNamed:@"stars"];
starsTexture.filteringMode = SKTextureFilteringNearest;
SKAction* movestarsSprite = [SKAction moveByX:-starsTexture.size.width*2 y:0 duration:0.02 * starsTexture.size.width*2];
SKAction* resetstarsSprite = [SKAction moveByX:starsTexture.size.width*2 y:0 duration:0];
SKAction* movestarsSpritesForever = [SKAction repeatActionForever:[SKAction sequence:@[movestarsSprite, resetstarsSprite]]];
for( int i = 0; i < 2 + self.frame.size.width / ( starsTexture.size.width * 2 ); ++i ) {
SKSpriteNode* starsSprite = [SKSpriteNode spriteNodeWithTexture:starsTexture];
[starsSprite setScale:2.0];
starsSprite.position = CGPointMake(i * starsSprite.size.width, starsSprite.size.height / 2);
[starsSprite runAction:movestarsSpritesForever];
[self addChild:starsSprite];
我是编码的初学者,现在星星出现在屏幕底部重复,我希望它们像星星一样出现在顶部。我应该改变什么才能实现这一目标?请帮助!
答案 0 :(得分:1)
SpriteKit中的坐标系在屏幕左下方有0,0所以你需要增加节点位置的y坐标以使它们向上移动。
您目前将它们设置为:
starsSprite.position = CGPointMake(i * starsSprite.size.width, starsSprite.size.height / 2);
尝试:
starsSprite.position = CGPointMake(i * starsSprite.size.width, self.size.height - starsSprite.size.height / 2);