我已经开始使用Sprite Kit了。我正在开发一款具有移动背景的游戏。现在我想知道如果我触摸屏幕以及如何在释放手指时如何移动背景,我将如何更快地移动背景。
我现在正在移动我的背景。
代码就像这样开始:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
[self addBackground]
};
return self;
}
-(void)addBackground{
SKTexture* background = [SKTexture textureWithImageNamed:@"backgroundImage"];
SKAction* moveBackground = [SKAction moveByX:-1704*2 y:0 duration:0.004 * background.size.width*2];
SKAction* putOnStart = [SKAction moveByX:1704*2 y:0 duration:0];
SKAction* moveForever = [SKAction repeatActionForever:[SKAction sequence:@[moveBackground, putOnStart]]];
for( int i = 0; i < 2 + self.frame.size.width / ( background.size.width * 2 ); ++i ) {
SKSpriteNode* sprite = [SKSpriteNode spriteNodeWithTexture:background size:CGSizeMake(1704 ,320)];
sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2);
[background runAction:moveForever];
[self addChild:sprite];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
touching = YES;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
touching = NO;
}
我在视图出现时初始化它。现在问题是我想更新SKAction moveBackround
的持续时间。
答案 0 :(得分:0)
对上一个答案感到抱歉。我完全误解了你的问题并喝了太多咖啡:)。
看看此代码移动背景。正如您所问,它具有以2种速度移动的能力,具体取决于是否注册了触摸。
#import "MyScene.h"
// this a from a project I did for an iPhone 5s
// the background image I used is 2048 wide by 640 high and is a @2x.png
static const float CONSTANT_MOVE_POINTS_PER_SEC = 300.0;
static inline CGPoint CGPointMultiplyScalar(const CGPoint a,
const CGFloat b)
{
return CGPointMake(a.x * b, a.y * b);
}
static inline CGPoint CGPointAdd(const CGPoint a,
const CGPoint b)
{
return CGPointMake(a.x + b.x, a.y + b.y);
}
@implementation MyScene {
SKNode *_bgLayer;
int _gameSpeedPlus;
NSTimeInterval _dt;
NSTimeInterval _lastUpdateTime;
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size])
{
_bgLayer = [SKNode node];
[self addChild:_bgLayer];
_gameSpeedPlus = 0;
for (int i = 0; i < 2; i++) {
SKSpriteNode * bg = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
bg.anchorPoint = CGPointZero;
bg.position = CGPointMake(i * bg.size.width, 0);
bg.name = @"bg";
[_bgLayer addChild:bg];
}
}
return self;
}
- (void)moveBg {
CGPoint bgVelocity = CGPointMake(-(CONSTANT_MOVE_POINTS_PER_SEC + _gameSpeedPlus), 0);
CGPoint amtToMove = CGPointMultiplyScalar(bgVelocity, _dt);
_bgLayer.position = CGPointAdd(_bgLayer.position, amtToMove);
[_bgLayer enumerateChildNodesWithName:@"bg"
usingBlock:^(SKNode *node, BOOL *stop){
SKSpriteNode * bg = (SKSpriteNode *) node;
CGPoint bgScreenPos = [_bgLayer convertPoint:bg.position
toNode:self];
if (bgScreenPos.x <= -bg.size.width) {
bg.position = CGPointMake(bg.position.x+bg.size.width*2,
bg.position.y);
}
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
_gameSpeedPlus = 200; // change value to increase or decrease speed
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
_gameSpeedPlus = 0;
}
-(void)update:(CFTimeInterval)currentTime {
if (_lastUpdateTime) {
_dt = currentTime - _lastUpdateTime;
} else {
_dt = 0;
}
_lastUpdateTime = currentTime;
[self moveBg];
}
@end